Skip to content

Commit 6a5f9c4

Browse files
committed
sdl: mutex.go: Change Mutex, Sem, Cond to have methods instead of functions
Signed-off-by: Lilis Iskandar <[email protected]>
1 parent ce485b4 commit 6a5f9c4

File tree

1 file changed

+85
-41
lines changed

1 file changed

+85
-41
lines changed

sdl/mutex.go

+85-41
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,39 @@ func CreateMutex() (*Mutex, error) {
5353
return (*Mutex)(unsafe.Pointer(mutex)), nil
5454
}
5555

56-
// LockMutex locks a mutex created with CreateMutex().
56+
// Lock locks a mutex created with CreateMutex().
5757
// (https://wiki.libsdl.org/SDL_LockMutex)
58-
func LockMutex(mutex *Mutex) int {
59-
return int(C.SDL_LockMutex(mutex.cptr()))
58+
func (mutex *Mutex) Lock() error {
59+
ret := int(C.SDL_LockMutex(mutex.cptr()))
60+
if ret != 0 {
61+
return GetError()
62+
}
63+
return nil
6064
}
6165

62-
// TryLockMutex tries to lock a mutex without blocking.
66+
// TryLock tries to lock a mutex without blocking.
6367
// (https://wiki.libsdl.org/SDL_TryLockMutex)
64-
func TryLockMutex(mutex *Mutex) int {
65-
return int(C.SDL_TryLockMutex(mutex.cptr()))
68+
func (mutex *Mutex) TryLock() error {
69+
ret := int(C.SDL_TryLockMutex(mutex.cptr()))
70+
if ret != 0 {
71+
return GetError()
72+
}
73+
return nil
6674
}
6775

68-
// UnlockMutex unlocks a mutex created with CreateMutex().
76+
// Unlock unlocks a mutex created with CreateMutex().
6977
// (https://wiki.libsdl.org/SDL_UnlockMutex)
70-
func UnlockMutex(mutex *Mutex) int {
71-
return int(C.SDL_UnlockMutex(mutex.cptr()))
78+
func (mutex *Mutex) Unlock() error {
79+
ret := int(C.SDL_UnlockMutex(mutex.cptr()))
80+
if ret != 0 {
81+
return GetError()
82+
}
83+
return nil
7284
}
7385

74-
// DestroyMutex destroys a mutex created with CreateMutex().
86+
// Destroy destroys a mutex created with CreateMutex().
7587
// (https://wiki.libsdl.org/SDL_DestroyMutex)
76-
func DestroyMutex(mutex *Mutex) {
88+
func (mutex *Mutex) Destroy() {
7789
C.SDL_DestroyMutex(mutex.cptr())
7890
}
7991

@@ -87,39 +99,55 @@ func CreateSemaphore(initialValue uint32) (*Sem, error) {
8799
return (*Sem)(unsafe.Pointer(sem)), nil
88100
}
89101

90-
// DestroySemaphore destroys a semaphore.
102+
// Destroy destroys a semaphore.
91103
// (https://wiki.libsdl.org/SDL_DestroySemaphore)
92-
func DestroySemaphore(sem *Sem) {
104+
func (sem *Sem) Destroy() {
93105
C.SDL_DestroySemaphore(sem.cptr())
94106
}
95107

96-
// SemWait waits until a semaphore has a positive value and then decrements it.
108+
// Wait waits until a semaphore has a positive value and then decrements it.
97109
// (https://wiki.libsdl.org/SDL_SemWait)
98-
func SemWait(sem *Sem) int {
99-
return int(C.SDL_SemWait(sem.cptr()))
110+
func (sem *Sem) Wait() error {
111+
ret := int(C.SDL_SemWait(sem.cptr()))
112+
if ret != 0 {
113+
return GetError()
114+
}
115+
return nil
100116
}
101117

102-
// SemTryWait sees if a semaphore has a positive value and decrement it if it does.
118+
// TryWait sees if a semaphore has a positive value and decrement it if it does.
103119
// (https://wiki.libsdl.org/SDL_SemTryWait)
104-
func SemTryWait(sem *Sem) int {
105-
return int(C.SDL_SemTryWait(sem.cptr()))
120+
func (sem *Sem) TryWait() error {
121+
ret := int(C.SDL_SemTryWait(sem.cptr()))
122+
if ret != 0 {
123+
return GetError()
124+
}
125+
return nil
106126
}
107127

108-
// SemWaitTimeout waits until a semaphore has a positive value and then decrements it.
128+
// WaitTimeout waits until a semaphore has a positive value and then decrements it.
109129
// (https://wiki.libsdl.org/SDL_SemWaitTimeout)
110-
func SemWaitTimeout(sem *Sem, ms uint32) int {
111-
return int(C.SDL_SemWaitTimeout(sem.cptr(), C.Uint32(ms)))
130+
func (sem *Sem) WaitTimeout(ms uint32) error {
131+
ret := int(C.SDL_SemWaitTimeout(sem.cptr(), C.Uint32(ms)))
132+
if ret != 0 {
133+
return GetError()
134+
}
135+
return nil
112136
}
113137

114-
// SemPost atomically increments a semaphore's value and wake waiting threads.
138+
// Post atomically increments a semaphore's value and wake waiting threads.
115139
// (https://wiki.libsdl.org/SDL_SemPost)
116-
func SemPost(sem *Sem) int {
117-
return int(C.SDL_SemPost(sem.cptr()))
140+
func (sem *Sem) Post() error {
141+
ret := int(C.SDL_SemPost(sem.cptr()))
142+
if ret != 0 {
143+
return GetError()
144+
}
145+
return nil
118146
}
119147

120-
// SemValue returns the current value of a semaphore.
148+
// Value returns the current value of a semaphore.
121149
// (https://wiki.libsdl.org/SDL_SemValue)
122-
func SemValue(sem *Sem) uint32 {
150+
func (sem *Sem) Value() uint32 {
123151
return uint32(C.SDL_SemValue(sem.cptr()))
124152
}
125153

@@ -128,32 +156,48 @@ func CreateCond() *Cond {
128156
return (*Cond)(unsafe.Pointer(C.SDL_CreateCond()))
129157
}
130158

131-
// DestroyCond creates a condition variable.
159+
// Destroy creates a condition variable.
132160
// (https://wiki.libsdl.org/SDL_DestroyCond)
133-
func DestroyCond(cond *Cond) {
161+
func (cond *Cond) Destroy() {
134162
C.SDL_DestroyCond(cond.cptr())
135163
}
136164

137-
// CondSignal restarts one of the threads that are waiting on the condition variable.
165+
// Signal restarts one of the threads that are waiting on the condition variable.
138166
// (https://wiki.libsdl.org/SDL_CondSignal)
139-
func CondSignal(cond *Cond) int {
140-
return int(C.SDL_CondSignal(cond.cptr()))
167+
func (cond *Cond) Signal() error {
168+
ret := int(C.SDL_CondSignal(cond.cptr()))
169+
if ret != 0 {
170+
return GetError()
171+
}
172+
return nil
141173
}
142174

143-
// CondBroadcast restarts all threads that are waiting on the condition variable.
175+
// Broadcast restarts all threads that are waiting on the condition variable.
144176
// (https://wiki.libsdl.org/SDL_CondBroadcast)
145-
func CondBroadcast(cond *Cond) int {
146-
return int(C.SDL_CondBroadcast(cond.cptr()))
177+
func (cond *Cond) Broadcast() error {
178+
ret := int(C.SDL_CondBroadcast(cond.cptr()))
179+
if ret != 0 {
180+
return GetError()
181+
}
182+
return nil
147183
}
148184

149-
// CondWait waits until a condition variable is signaled.
185+
// Wait waits until a condition variable is signaled.
150186
// (https://wiki.libsdl.org/SDL_CondWait)
151-
func CondWait(cond *Cond, mutex *Mutex) int {
152-
return int(C.SDL_CondWait(cond.cptr(), mutex.cptr()))
187+
func (cond *Cond) Wait(mutex *Mutex) error {
188+
ret := int(C.SDL_CondWait(cond.cptr(), mutex.cptr()))
189+
if ret != 0 {
190+
return GetError()
191+
}
192+
return nil
153193
}
154194

155-
// CondWaitTimeout waits until a condition variable is signaled or a specified amount of time has passed.
195+
// WaitTimeout waits until a condition variable is signaled or a specified amount of time has passed.
156196
// (https://wiki.libsdl.org/SDL_CondWaitTimeout)
157-
func CondWaitTimeout(cond *Cond, mutex *Mutex, ms uint32) int {
158-
return int(C.SDL_CondWaitTimeout(cond.cptr(), mutex.cptr(), C.Uint32(ms)))
197+
func (cond *Cond) WaitTimeout(mutex *Mutex, ms uint32) error {
198+
ret := int(C.SDL_CondWaitTimeout(cond.cptr(), mutex.cptr(), C.Uint32(ms)))
199+
if ret != 0 {
200+
return GetError()
201+
}
202+
return nil
159203
}

0 commit comments

Comments
 (0)