mutex-cooperative.mx raw
1 //go:build moxie.unicore
2
3 package task
4
5 //go:linkname eventLoopTick runtime.eventLoopTick
6 func eventLoopTick()
7
8 type Mutex struct {
9 locked bool
10 depth int
11 blocked Stack
12 }
13
14 func (m *Mutex) Lock() {
15 // Single-threaded: allow reentrant locking (no contention possible).
16 m.depth++
17 m.locked = true
18 }
19
20 func (m *Mutex) Unlock() {
21 if !m.locked {
22 panic("sync: unlock of unlocked Mutex")
23 }
24 m.depth--
25 if m.depth == 0 {
26 m.locked = false
27 }
28 }
29
30 // TryLock tries to lock m and reports whether it succeeded.
31 //
32 // Note that while correct uses of TryLock do exist, they are rare,
33 // and use of TryLock is often a sign of a deeper problem
34 // in a particular use of mutexes.
35 func (m *Mutex) TryLock() bool {
36 if m.locked {
37 return false
38 }
39 m.Lock()
40 return true
41 }
42