Index: src/OFThread.h ================================================================== --- src/OFThread.h +++ src/OFThread.h @@ -133,10 +133,18 @@ /** * Locks the mutex. */ - lock; +/** + * Tries to lock the mutex and returns a boolean whether the mutex could be + * acquired. + * + * \return A boolean whether the mutex could be acquired + */ +- (BOOL)tryLock; + /** * Unlocks the mutex. */ - unlock; @end Index: src/OFThread.m ================================================================== --- src/OFThread.m +++ src/OFThread.m @@ -218,10 +218,15 @@ if (!of_mutex_lock(&mutex)) @throw [OFMutexLockFailedException newWithClass: isa]; return self; } + +- (BOOL)tryLock +{ + return of_mutex_trylock(&mutex); +} - unlock { if (!of_mutex_unlock(&mutex)) @throw [OFMutexUnlockFailedException newWithClass: isa]; Index: src/threading.h ================================================================== --- src/threading.h +++ src/threading.h @@ -114,10 +114,20 @@ #elif defined(_WIN32) EnterCriticalSection(mutex); return YES; #endif } + +static OF_INLINE BOOL +of_mutex_trylock(of_mutex_t *mutex) +{ +#if defined(OF_HAVE_PTHREADS) + return (pthread_mutex_trylock(mutex) ? NO : YES); +#elif defined(_WIN32) + return (TryEnterCriticalSection(mutex) ? YES : NO); +#endif +} static OF_INLINE BOOL of_mutex_unlock(of_mutex_t *mutex) { #if defined(OF_HAVE_PTHREADS)