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)
35typedef pthread_mutex_t OFPlainMutex;
36#elif defined(OF_WINDOWS)
38typedef CRITICAL_SECTION OFPlainMutex;
39#elif defined(OF_AMIGAOS)
40# include <exec/semaphores.h>
41typedef struct SignalSemaphore OFPlainMutex;
44#if defined(OF_HAVE_ATOMIC_OPS)
46typedef volatile int OFSpinlock;
47#elif defined(OF_HAVE_PTHREAD_SPINLOCKS)
48typedef pthread_spinlock_t OFSpinlock;
50typedef 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;
71extern int OFPlainMutexNew(OFPlainMutex *mutex);
72extern int OFPlainMutexLock(OFPlainMutex *mutex);
73extern int OFPlainMutexTryLock(OFPlainMutex *mutex);
74extern int OFPlainMutexUnlock(OFPlainMutex *mutex);
75extern int OFPlainMutexFree(OFPlainMutex *mutex);
76extern int OFPlainRecursiveMutexNew(OFPlainRecursiveMutex *rmutex);
77extern int OFPlainRecursiveMutexLock(OFPlainRecursiveMutex *rmutex);
78extern int OFPlainRecursiveMutexTryLock(OFPlainRecursiveMutex *rmutex);
79extern int OFPlainRecursiveMutexUnlock(OFPlainRecursiveMutex *rmutex);
80extern int OFPlainRecursiveMutexFree(OFPlainRecursiveMutex *rmutex);
90#if defined(OF_HAVE_SCHED_YIELD)
92#elif defined(OF_WINDOWS)
98OFSpinlockNew(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);
111OFSpinlockTryLock(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);
128OFSpinlockLock(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);
149OFSpinlockUnlock(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);
165OFSpinlockFree(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);