mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-20 12:29:03 +00:00
sync: move Mutex to internal/task
The mutex implementation needs a different implementation once support for threading lands. This implementation just moves code to the internal/task package to centralize these algorithms.
This commit is contained in:
committed by
Ron Evans
parent
b15adf23f8
commit
5a1b885024
@@ -0,0 +1,43 @@
|
|||||||
|
package task
|
||||||
|
|
||||||
|
type Mutex struct {
|
||||||
|
locked bool
|
||||||
|
blocked Stack
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Mutex) Lock() {
|
||||||
|
if m.locked {
|
||||||
|
// Push self onto stack of blocked tasks, and wait to be resumed.
|
||||||
|
m.blocked.Push(Current())
|
||||||
|
Pause()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
m.locked = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Mutex) Unlock() {
|
||||||
|
if !m.locked {
|
||||||
|
panic("sync: unlock of unlocked Mutex")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wake up a blocked task, if applicable.
|
||||||
|
if t := m.blocked.Pop(); t != nil {
|
||||||
|
scheduleTask(t)
|
||||||
|
} else {
|
||||||
|
m.locked = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TryLock tries to lock m and reports whether it succeeded.
|
||||||
|
//
|
||||||
|
// Note that while correct uses of TryLock do exist, they are rare,
|
||||||
|
// and use of TryLock is often a sign of a deeper problem
|
||||||
|
// in a particular use of mutexes.
|
||||||
|
func (m *Mutex) TryLock() bool {
|
||||||
|
if m.locked {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m.Lock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -89,3 +89,6 @@ func (c *Cond) Wait() {
|
|||||||
// signal.
|
// signal.
|
||||||
task.Pause()
|
task.Pause()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:linkname scheduleTask runtime.scheduleTask
|
||||||
|
func scheduleTask(*task.Task)
|
||||||
|
|||||||
+1
-45
@@ -2,53 +2,9 @@ package sync
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"internal/task"
|
"internal/task"
|
||||||
_ "unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mutex struct {
|
type Mutex = task.Mutex
|
||||||
locked bool
|
|
||||||
blocked task.Stack
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:linkname scheduleTask runtime.scheduleTask
|
|
||||||
func scheduleTask(*task.Task)
|
|
||||||
|
|
||||||
func (m *Mutex) Lock() {
|
|
||||||
if m.locked {
|
|
||||||
// Push self onto stack of blocked tasks, and wait to be resumed.
|
|
||||||
m.blocked.Push(task.Current())
|
|
||||||
task.Pause()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
m.locked = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Mutex) Unlock() {
|
|
||||||
if !m.locked {
|
|
||||||
panic("sync: unlock of unlocked Mutex")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wake up a blocked task, if applicable.
|
|
||||||
if t := m.blocked.Pop(); t != nil {
|
|
||||||
scheduleTask(t)
|
|
||||||
} else {
|
|
||||||
m.locked = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TryLock tries to lock m and reports whether it succeeded.
|
|
||||||
//
|
|
||||||
// Note that while correct uses of TryLock do exist, they are rare,
|
|
||||||
// and use of TryLock is often a sign of a deeper problem
|
|
||||||
// in a particular use of mutexes.
|
|
||||||
func (m *Mutex) TryLock() bool {
|
|
||||||
if m.locked {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
m.Lock()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
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.
|
||||||
|
|||||||
Reference in New Issue
Block a user