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!
33 #if defined(OF_HAVE_PTHREADS)
35 typedef pthread_mutex_t OFPlainMutex;
36 #elif defined(OF_WINDOWS)
38 typedef CRITICAL_SECTION OFPlainMutex;
39 #elif defined(OF_AMIGAOS)
40 # include <exec/semaphores.h>
41 typedef struct SignalSemaphore OFPlainMutex;
44 #if defined(OF_HAVE_ATOMIC_OPS)
46 typedef volatile int OFSpinlock;
47 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
48 typedef pthread_spinlock_t OFSpinlock;
50 typedef OFPlainMutex OFSpinlock;
53 #ifdef OF_HAVE_SCHED_YIELD
57 #if defined(OF_HAVE_RECURSIVE_PTHREAD_MUTEXES) || defined(OF_WINDOWS) || \
59 # define OFPlainRecursiveMutex OFPlainMutex
65 } OFPlainRecursiveMutex;
71 extern int OFPlainMutexNew(OFPlainMutex *mutex);
72 extern int OFPlainMutexLock(OFPlainMutex *mutex);
73 extern int OFPlainMutexTryLock(OFPlainMutex *mutex);
74 extern int OFPlainMutexUnlock(OFPlainMutex *mutex);
75 extern int OFPlainMutexFree(OFPlainMutex *mutex);
76 extern int OFPlainRecursiveMutexNew(OFPlainRecursiveMutex *rmutex);
77 extern int OFPlainRecursiveMutexLock(OFPlainRecursiveMutex *rmutex);
78 extern int OFPlainRecursiveMutexTryLock(OFPlainRecursiveMutex *rmutex);
79 extern int OFPlainRecursiveMutexUnlock(OFPlainRecursiveMutex *rmutex);
80 extern int OFPlainRecursiveMutexFree(OFPlainRecursiveMutex *rmutex);
90 #if defined(OF_HAVE_SCHED_YIELD)
92 #elif defined(OF_WINDOWS)
98 OFSpinlockNew(OFSpinlock *spinlock)
100 #if defined(OF_HAVE_ATOMIC_OPS)
103 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
104 return pthread_spin_init(spinlock, 0);
106 return OFPlainMutexNew(spinlock);
111 OFSpinlockTryLock(OFSpinlock *spinlock)
113 #if defined(OF_HAVE_ATOMIC_OPS)
114 if (OFAtomicIntCompareAndSwap(spinlock, 0, 1)) {
115 OFAcquireMemoryBarrier();
120 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
121 return pthread_spin_trylock(spinlock);
123 return OFPlainMutexTryLock(spinlock);
128 OFSpinlockLock(OFSpinlock *spinlock)
130 #if defined(OF_HAVE_ATOMIC_OPS)
133 for (i = 0; i < 10; i++)
134 if (OFSpinlockTryLock(spinlock) == 0)
137 while (OFSpinlockTryLock(spinlock) == EBUSY)
141 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
142 return pthread_spin_lock(spinlock);
144 return OFPlainMutexLock(spinlock);
149 OFSpinlockUnlock(OFSpinlock *spinlock)
151 #if defined(OF_HAVE_ATOMIC_OPS)
152 bool ret = OFAtomicIntCompareAndSwap(spinlock, 1, 0);
154 OFReleaseMemoryBarrier();
156 return (ret ? 0 : EINVAL);
157 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
158 return pthread_spin_unlock(spinlock);
160 return OFPlainMutexUnlock(spinlock);
165 OFSpinlockFree(OFSpinlock *spinlock)
167 #if defined(OF_HAVE_ATOMIC_OPS)
169 #elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
170 return pthread_spin_destroy(spinlock);
172 return OFPlainMutexFree(spinlock);