github.com/aykevl/tinygo@v0.5.0/src/sync/mutex.go (about) 1 package sync 2 3 // These mutexes assume there is only one thread of operation: no goroutines, 4 // interrupts or anything else. 5 6 type Mutex struct { 7 locked bool 8 } 9 10 func (m *Mutex) Lock() { 11 if m.locked { 12 panic("todo: block on locked mutex") 13 } 14 m.locked = true 15 } 16 17 func (m *Mutex) Unlock() { 18 if !m.locked { 19 panic("sync: unlock of unlocked Mutex") 20 } 21 m.locked = false 22 } 23 24 type RWMutex struct { 25 m Mutex 26 readers uint32 27 } 28 29 func (rw *RWMutex) Lock() { 30 rw.m.Lock() 31 } 32 33 func (rw *RWMutex) Unlock() { 34 rw.m.Unlock() 35 } 36 37 func (rw *RWMutex) RLock() { 38 if rw.readers == 0 { 39 rw.m.Lock() 40 } 41 rw.readers++ 42 } 43 44 func (rw *RWMutex) RUnlock() { 45 if rw.readers == 0 { 46 panic("sync: unlock of unlocked RWMutex") 47 } 48 rw.readers-- 49 if rw.readers == 0 { 50 rw.m.Unlock() 51 } 52 }