20 #include "objfw-defs.h"
26 #if !defined(OF_HAVE_THREADS) || \
27 (!defined(OF_HAVE_PTHREADS) && !defined(OF_WINDOWS) && !defined(OF_AMIGAOS))
28 # error No mutexes available!
35 #if defined(OF_HAVE_PTHREADS)
37 typedef pthread_mutex_t OFPlainMutex;
38 #elif defined(OF_WINDOWS)
40 typedef CRITICAL_SECTION OFPlainMutex;
41 #elif defined(OF_AMIGAOS)
42 # include <exec/semaphores.h>
43 typedef struct SignalSemaphore OFPlainMutex;
46 #if defined(OF_HAVE_ATOMIC_OPS)
48 typedef volatile int OFSpinlock;
49 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
50 typedef pthread_spinlock_t OFSpinlock;
52 typedef OFPlainMutex OFSpinlock;
55 #ifdef OF_HAVE_SCHED_YIELD
59 #if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(OF_WINDOWS) || \
61 # define OFPlainRecursiveMutex OFPlainMutex
67 } OFPlainRecursiveMutex;
170 static OF_INLINE
void
173 #if defined(OF_HAVE_SCHED_YIELD)
175 #elif defined(OF_WINDOWS)
189 #if defined(OF_HAVE_ATOMIC_OPS)
192 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
193 return pthread_spin_init(spinlock, 0);
208 #if defined(OF_HAVE_ATOMIC_OPS)
209 if (OFAtomicIntCompareAndSwap(spinlock, 0, 1)) {
210 OFAcquireMemoryBarrier();
215 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
216 return pthread_spin_trylock(spinlock);
231 #if defined(OF_HAVE_ATOMIC_OPS)
234 for (i = 0; i < 10; i++)
242 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
243 return pthread_spin_lock(spinlock);
258 #if defined(OF_HAVE_ATOMIC_OPS)
259 bool ret = OFAtomicIntCompareAndSwap(spinlock, 1, 0);
261 OFReleaseMemoryBarrier();
263 return (ret ? 0 : EINVAL);
264 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
265 return pthread_spin_unlock(spinlock);
280 #if defined(OF_HAVE_ATOMIC_OPS)
282 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
283 return pthread_spin_destroy(spinlock);
int OFPlainRecursiveMutexUnlock(OFPlainRecursiveMutex *rmutex)
Unlocks the specified recursive mutex.
static OF_INLINE int OFSpinlockFree(OFSpinlock *spinlock)
Destroys a spinlock.
Definition: OFPlainMutex.h:278
static OF_INLINE int OFSpinlockUnlock(OFSpinlock *spinlock)
Unlocks a spinlock.
Definition: OFPlainMutex.h:256
int OFPlainMutexTryLock(OFPlainMutex *mutex)
Tries to lock the specified mutex without blocking.
int OFPlainRecursiveMutexFree(OFPlainRecursiveMutex *rmutex)
Destroys the specified recursive mutex.
int OFPlainRecursiveMutexTryLock(OFPlainRecursiveMutex *rmutex)
Tries to lock the specified recursive mutex without blocking.
static OF_INLINE int OFSpinlockNew(OFSpinlock *spinlock)
Creates a new spinlock.
Definition: OFPlainMutex.h:187
int OFPlainMutexLock(OFPlainMutex *mutex)
Locks the specified mutex.
int OFPlainMutexNew(OFPlainMutex *mutex)
Creates a new plain mutex.
static OF_INLINE int OFSpinlockTryLock(OFSpinlock *spinlock)
Tries to lock a spinlock.
Definition: OFPlainMutex.h:206
static OF_INLINE int OFSpinlockLock(OFSpinlock *spinlock)
Locks a spinlock.
Definition: OFPlainMutex.h:229
int OFPlainRecursiveMutexNew(OFPlainRecursiveMutex *rmutex)
Creates a new plain recursive mutex.
int OFPlainMutexUnlock(OFPlainMutex *mutex)
Unlocks the specified mutex.
int OFPlainRecursiveMutexLock(OFPlainRecursiveMutex *rmutex)
Locks the specified recursive mutex.
static OF_INLINE void OFYieldThread(void)
Yield the current thread, indicating to the OS that another thread should execute instead.
Definition: OFPlainMutex.h:171
int OFPlainMutexFree(OFPlainMutex *mutex)
Destroys the specified mutex.