@@ -14,10 +14,12 @@ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "objfw-defs.h" + +#include #include "platform.h" #if !defined(OF_HAVE_THREADS) || \ (!defined(OF_HAVE_PTHREADS) && !defined(OF_WINDOWS) && !defined(OF_AMIGAOS)) @@ -63,20 +65,20 @@ #endif #ifdef __cplusplus extern "C" { #endif -extern bool of_mutex_new(of_mutex_t *mutex); -extern bool of_mutex_lock(of_mutex_t *mutex); -extern bool of_mutex_trylock(of_mutex_t *mutex); -extern bool of_mutex_unlock(of_mutex_t *mutex); -extern bool of_mutex_free(of_mutex_t *mutex); -extern bool of_rmutex_new(of_rmutex_t *rmutex); -extern bool of_rmutex_lock(of_rmutex_t *rmutex); -extern bool of_rmutex_trylock(of_rmutex_t *rmutex); -extern bool of_rmutex_unlock(of_rmutex_t *rmutex); -extern bool of_rmutex_free(of_rmutex_t *rmutex); +extern int of_mutex_new(of_mutex_t *mutex); +extern int of_mutex_lock(of_mutex_t *mutex); +extern int of_mutex_trylock(of_mutex_t *mutex); +extern int of_mutex_unlock(of_mutex_t *mutex); +extern int of_mutex_free(of_mutex_t *mutex); +extern int of_rmutex_new(of_rmutex_t *rmutex); +extern int of_rmutex_lock(of_rmutex_t *rmutex); +extern int of_rmutex_trylock(of_rmutex_t *rmutex); +extern int of_rmutex_unlock(of_rmutex_t *rmutex); +extern int of_rmutex_free(of_rmutex_t *rmutex); #ifdef __cplusplus } #endif /* Spinlocks are inlined for performance. */ @@ -89,83 +91,83 @@ #elif defined(OF_WINDOWS) Sleep(0); #endif } -static OF_INLINE bool +static OF_INLINE int of_spinlock_new(of_spinlock_t *spinlock) { #if defined(OF_HAVE_ATOMIC_OPS) *spinlock = 0; - return true; + return 0; #elif defined(OF_HAVE_PTHREAD_SPINLOCKS) - return (pthread_spin_init(spinlock, 0) == 0); + return pthread_spin_init(spinlock, 0); #else return of_mutex_new(spinlock); #endif } -static OF_INLINE bool +static OF_INLINE int of_spinlock_trylock(of_spinlock_t *spinlock) { #if defined(OF_HAVE_ATOMIC_OPS) if (of_atomic_int_cmpswap(spinlock, 0, 1)) { of_memory_barrier_acquire(); - return true; + return 0; } - return false; + return EBUSY; #elif defined(OF_HAVE_PTHREAD_SPINLOCKS) - return (pthread_spin_trylock(spinlock) == 0); + return pthread_spin_trylock(spinlock); #else return of_mutex_trylock(spinlock); #endif } -static OF_INLINE bool +static OF_INLINE int of_spinlock_lock(of_spinlock_t *spinlock) { #if defined(OF_HAVE_ATOMIC_OPS) size_t i; for (i = 0; i < OF_SPINCOUNT; i++) - if (of_spinlock_trylock(spinlock)) - return true; + if (of_spinlock_trylock(spinlock) == 0) + return 0; - while (!of_spinlock_trylock(spinlock)) + while (of_spinlock_trylock(spinlock) == EBUSY) of_thread_yield(); - return true; + return 0; #elif defined(OF_HAVE_PTHREAD_SPINLOCKS) - return (pthread_spin_lock(spinlock) == 0); + return pthread_spin_lock(spinlock); #else return of_mutex_lock(spinlock); #endif } -static OF_INLINE bool +static OF_INLINE int of_spinlock_unlock(of_spinlock_t *spinlock) { #if defined(OF_HAVE_ATOMIC_OPS) bool ret = of_atomic_int_cmpswap(spinlock, 1, 0); of_memory_barrier_release(); - return ret; + return (ret ? 0 : EINVAL); #elif defined(OF_HAVE_PTHREAD_SPINLOCKS) - return (pthread_spin_unlock(spinlock) == 0); + return pthread_spin_unlock(spinlock); #else return of_mutex_unlock(spinlock); #endif } -static OF_INLINE bool +static OF_INLINE int of_spinlock_free(of_spinlock_t *spinlock) { #if defined(OF_HAVE_ATOMIC_OPS) - return true; + return 0; #elif defined(OF_HAVE_PTHREAD_SPINLOCKS) - return (pthread_spin_destroy(spinlock) == 0); + return pthread_spin_destroy(spinlock); #else return of_mutex_free(spinlock); #endif }