mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-21 04:49:04 +00:00
sync: don't use volatile in Mutex
Volatile loads/stors are only useful for communication with interrupts
or for memory-mapped I/O. They do not provide any sort of safety for
sync.Mutex, while making it *appear* as if it is more safe.
* `sync.Mutex` cannot be used safely inside interrupts, because any
blocking calls (including `Lock`) will cause a runtime panic.
* For multithreading, `volatile` is also the wrong choice. Atomic
operations should be used instead, and the current code would not
work for multithreaded programs anyway.
This commit is contained in:
+6
-23
@@ -3,12 +3,10 @@ package sync
|
|||||||
import (
|
import (
|
||||||
"internal/task"
|
"internal/task"
|
||||||
_ "unsafe"
|
_ "unsafe"
|
||||||
|
|
||||||
"runtime/volatile"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mutex struct {
|
type Mutex struct {
|
||||||
state uint8 // Set to non-zero if locked.
|
locked bool
|
||||||
blocked task.Stack
|
blocked task.Stack
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,18 +14,18 @@ type Mutex struct {
|
|||||||
func scheduleTask(*task.Task)
|
func scheduleTask(*task.Task)
|
||||||
|
|
||||||
func (m *Mutex) Lock() {
|
func (m *Mutex) Lock() {
|
||||||
if m.islocked() {
|
if m.locked {
|
||||||
// Push self onto stack of blocked tasks, and wait to be resumed.
|
// Push self onto stack of blocked tasks, and wait to be resumed.
|
||||||
m.blocked.Push(task.Current())
|
m.blocked.Push(task.Current())
|
||||||
task.Pause()
|
task.Pause()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
m.setlock(true)
|
m.locked = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mutex) Unlock() {
|
func (m *Mutex) Unlock() {
|
||||||
if !m.islocked() {
|
if !m.locked {
|
||||||
panic("sync: unlock of unlocked Mutex")
|
panic("sync: unlock of unlocked Mutex")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +33,7 @@ func (m *Mutex) Unlock() {
|
|||||||
if t := m.blocked.Pop(); t != nil {
|
if t := m.blocked.Pop(); t != nil {
|
||||||
scheduleTask(t)
|
scheduleTask(t)
|
||||||
} else {
|
} else {
|
||||||
m.setlock(false)
|
m.locked = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,28 +43,13 @@ func (m *Mutex) Unlock() {
|
|||||||
// and use of TryLock is often a sign of a deeper problem
|
// and use of TryLock is often a sign of a deeper problem
|
||||||
// in a particular use of mutexes.
|
// in a particular use of mutexes.
|
||||||
func (m *Mutex) TryLock() bool {
|
func (m *Mutex) TryLock() bool {
|
||||||
if m.islocked() {
|
if m.locked {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
m.Lock()
|
m.Lock()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mutex) islocked() bool {
|
|
||||||
return volatile.LoadUint8(&m.state) != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Mutex) setlock(b bool) {
|
|
||||||
volatile.StoreUint8(&m.state, boolToU8(b))
|
|
||||||
}
|
|
||||||
|
|
||||||
func boolToU8(b bool) uint8 {
|
|
||||||
if b {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
type RWMutex struct {
|
type RWMutex struct {
|
||||||
// waitingWriters are all of the tasks waiting for write locks.
|
// waitingWriters are all of the tasks waiting for write locks.
|
||||||
waitingWriters task.Stack
|
waitingWriters task.Stack
|
||||||
|
|||||||
Reference in New Issue
Block a user