File: mutex.go

package info (click to toggle)
golang-github-gopherjs-gopherjs 0.0~git20170927.0.4152256-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,468 kB
  • sloc: cpp: 91; makefile: 7
file content (85 lines) | stat: -rw-r--r-- 2,073 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package nosync

// Mutex is a dummy which is non-blocking.
type Mutex struct {
	locked bool
}

// Lock locks m. It is a run-time error if m is already locked.
func (m *Mutex) Lock() {
	if m.locked {
		panic("nosync: mutex is already locked")
	}
	m.locked = true
}

// Unlock unlocks m. It is a run-time error if m is not locked.
func (m *Mutex) Unlock() {
	if !m.locked {
		panic("nosync: unlock of unlocked mutex")
	}
	m.locked = false
}

// RWMutex is a dummy which is non-blocking.
type RWMutex struct {
	writeLocked     bool
	readLockCounter int
}

// Lock locks m for writing. It is a run-time error if rw is already locked for reading or writing.
func (rw *RWMutex) Lock() {
	if rw.readLockCounter != 0 || rw.writeLocked {
		panic("nosync: mutex is already locked")
	}
	rw.writeLocked = true
}

// Unlock unlocks rw for writing. It is a run-time error if rw is not locked for writing.
func (rw *RWMutex) Unlock() {
	if !rw.writeLocked {
		panic("nosync: unlock of unlocked mutex")
	}
	rw.writeLocked = false
}

// RLock locks m for reading. It is a run-time error if rw is already locked for reading or writing.
func (rw *RWMutex) RLock() {
	if rw.writeLocked {
		panic("nosync: mutex is already locked")
	}
	rw.readLockCounter++
}

// RUnlock undoes a single RLock call; it does not affect other simultaneous readers. It is a run-time error if rw is not locked for reading.
func (rw *RWMutex) RUnlock() {
	if rw.readLockCounter == 0 {
		panic("nosync: unlock of unlocked mutex")
	}
	rw.readLockCounter--
}

// WaitGroup is a dummy which is non-blocking.
type WaitGroup struct {
	counter int
}

// Add adds delta, which may be negative, to the WaitGroup If the counter goes negative, Add panics.
func (wg *WaitGroup) Add(delta int) {
	wg.counter += delta
	if wg.counter < 0 {
		panic("sync: negative WaitGroup counter")
	}
}

// Done decrements the WaitGroup counter.
func (wg *WaitGroup) Done() {
	wg.Add(-1)
}

// Wait panics if the WaitGroup counter is not zero.
func (wg *WaitGroup) Wait() {
	if wg.counter != 0 {
		panic("sync: WaitGroup counter not zero")
	}
}