forked from ClarkGuan/go-sdl2
/
mutex.go
203 lines (177 loc) · 4.81 KB
/
mutex.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package sdl
// #include "sdl_wrapper.h"
import "C"
import "unsafe"
// ThreadID is the thread identifier for a thread.
type ThreadID uint64
// Mutex is the SDL mutex structure.
type Mutex struct {
Recursive int
Owner ThreadID
Sem *Sem
}
// Sem is the SDL semaphore structure.
type Sem struct {
Count uint32
WaitersCount uint32
CountLock *Mutex
CountNonzero *Cond
}
// Cond is the SDL condition variable structure.
type Cond struct {
Lock *Mutex
Waiting int
Signals int
WaitSem *Sem
WaitDone *Sem
}
func (m *Mutex) cptr() *C.SDL_mutex {
return (*C.SDL_mutex)(unsafe.Pointer(m))
}
func (s *Sem) cptr() *C.SDL_sem {
return (*C.SDL_sem)(unsafe.Pointer(s))
}
func (c *Cond) cptr() *C.SDL_cond {
return (*C.SDL_cond)(unsafe.Pointer(c))
}
// CreateMutex creates a new mutex.
// (https://wiki.libsdl.org/SDL_CreateMutex)
func CreateMutex() (*Mutex, error) {
mutex := C.SDL_CreateMutex()
if mutex == nil {
return nil, GetError()
}
return (*Mutex)(unsafe.Pointer(mutex)), nil
}
// Lock locks a mutex created with CreateMutex().
// (https://wiki.libsdl.org/SDL_LockMutex)
func (mutex *Mutex) Lock() error {
ret := int(C.SDL_LockMutex(mutex.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// TryLock tries to lock a mutex without blocking.
// (https://wiki.libsdl.org/SDL_TryLockMutex)
func (mutex *Mutex) TryLock() error {
ret := int(C.SDL_TryLockMutex(mutex.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// Unlock unlocks a mutex created with CreateMutex().
// (https://wiki.libsdl.org/SDL_UnlockMutex)
func (mutex *Mutex) Unlock() error {
ret := int(C.SDL_UnlockMutex(mutex.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// Destroy destroys a mutex created with CreateMutex().
// (https://wiki.libsdl.org/SDL_DestroyMutex)
func (mutex *Mutex) Destroy() {
C.SDL_DestroyMutex(mutex.cptr())
}
// CreateSemaphore creates a semaphore.
// (https://wiki.libsdl.org/SDL_CreateSemaphore)
func CreateSemaphore(initialValue uint32) (*Sem, error) {
sem := C.SDL_CreateSemaphore(C.Uint32(initialValue))
if sem == nil {
return nil, GetError()
}
return (*Sem)(unsafe.Pointer(sem)), nil
}
// Destroy destroys a semaphore.
// (https://wiki.libsdl.org/SDL_DestroySemaphore)
func (sem *Sem) Destroy() {
C.SDL_DestroySemaphore(sem.cptr())
}
// Wait waits until a semaphore has a positive value and then decrements it.
// (https://wiki.libsdl.org/SDL_SemWait)
func (sem *Sem) Wait() error {
ret := int(C.SDL_SemWait(sem.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// TryWait sees if a semaphore has a positive value and decrement it if it does.
// (https://wiki.libsdl.org/SDL_SemTryWait)
func (sem *Sem) TryWait() error {
ret := int(C.SDL_SemTryWait(sem.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// WaitTimeout waits until a semaphore has a positive value and then decrements it.
// (https://wiki.libsdl.org/SDL_SemWaitTimeout)
func (sem *Sem) WaitTimeout(ms uint32) error {
ret := int(C.SDL_SemWaitTimeout(sem.cptr(), C.Uint32(ms)))
if ret != 0 {
return GetError()
}
return nil
}
// Post atomically increments a semaphore's value and wake waiting threads.
// (https://wiki.libsdl.org/SDL_SemPost)
func (sem *Sem) Post() error {
ret := int(C.SDL_SemPost(sem.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// Value returns the current value of a semaphore.
// (https://wiki.libsdl.org/SDL_SemValue)
func (sem *Sem) Value() uint32 {
return uint32(C.SDL_SemValue(sem.cptr()))
}
// CreateCond (https://wiki.libsdl.org/SDL_CreateCond)
func CreateCond() *Cond {
return (*Cond)(unsafe.Pointer(C.SDL_CreateCond()))
}
// Destroy creates a condition variable.
// (https://wiki.libsdl.org/SDL_DestroyCond)
func (cond *Cond) Destroy() {
C.SDL_DestroyCond(cond.cptr())
}
// Signal restarts one of the threads that are waiting on the condition variable.
// (https://wiki.libsdl.org/SDL_CondSignal)
func (cond *Cond) Signal() error {
ret := int(C.SDL_CondSignal(cond.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// Broadcast restarts all threads that are waiting on the condition variable.
// (https://wiki.libsdl.org/SDL_CondBroadcast)
func (cond *Cond) Broadcast() error {
ret := int(C.SDL_CondBroadcast(cond.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// Wait waits until a condition variable is signaled.
// (https://wiki.libsdl.org/SDL_CondWait)
func (cond *Cond) Wait(mutex *Mutex) error {
ret := int(C.SDL_CondWait(cond.cptr(), mutex.cptr()))
if ret != 0 {
return GetError()
}
return nil
}
// WaitTimeout waits until a condition variable is signaled or a specified amount of time has passed.
// (https://wiki.libsdl.org/SDL_CondWaitTimeout)
func (cond *Cond) WaitTimeout(mutex *Mutex, ms uint32) error {
ret := int(C.SDL_CondWaitTimeout(cond.cptr(), mutex.cptr(), C.Uint32(ms)))
if ret != 0 {
return GetError()
}
return nil
}