2011-03-22 00:08:45 +00:00
|
|
|
/**
|
|
|
|
* libobjc requires recursive mutexes. These are delegated to the underlying
|
|
|
|
* threading implementation. This file contains a VERY thin wrapper over the
|
|
|
|
* Windows and POSIX mutex APIs.
|
|
|
|
*/
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
#ifndef __LIBOBJC_LOCK_H_INCLUDED__
|
|
|
|
#define __LIBOBJC_LOCK_H_INCLUDED__
|
|
|
|
#ifdef WIN32
|
2011-08-09 12:57:20 +00:00
|
|
|
#define BOOL _WINBOOL
|
2009-09-08 16:18:59 +00:00
|
|
|
# include <windows.h>
|
2011-08-09 12:57:20 +00:00
|
|
|
#undef BOOL
|
2009-09-08 16:18:59 +00:00
|
|
|
typedef HANDLE mutex_t;
|
|
|
|
# define INIT_LOCK(x) x = CreateMutex(NULL, FALSE, NULL)
|
|
|
|
# define LOCK(x) WaitForSingleObject(*x, INFINITE)
|
|
|
|
# define UNLOCK(x) ReleaseMutex(*x)
|
|
|
|
# define DESTROY_LOCK(x) CloseHandle(*x)
|
|
|
|
#else
|
|
|
|
|
|
|
|
# include <pthread.h>
|
|
|
|
|
|
|
|
typedef pthread_mutex_t mutex_t;
|
|
|
|
// If this pthread implementation has a static initializer for recursive
|
|
|
|
// mutexes, use that, otherwise fall back to the portable version
|
|
|
|
# ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
|
2013-05-28 10:00:11 +00:00
|
|
|
# define INIT_LOCK(x) x = (pthread_mutex_t)PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
|
2009-09-08 16:18:59 +00:00
|
|
|
# elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
|
2013-04-04 09:35:21 +00:00
|
|
|
# define INIT_LOCK(x) x = (pthread_mutex_t)PTHREAD_RECURSIVE_MUTEX_INITIALIZER
|
2009-09-08 16:18:59 +00:00
|
|
|
# else
|
|
|
|
# define INIT_LOCK(x) init_recursive_mutex(&(x))
|
|
|
|
|
|
|
|
static inline void init_recursive_mutex(pthread_mutex_t *x)
|
|
|
|
{
|
|
|
|
pthread_mutexattr_t recursiveAttributes;
|
|
|
|
pthread_mutexattr_init(&recursiveAttributes);
|
|
|
|
pthread_mutexattr_settype(&recursiveAttributes, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
pthread_mutex_init(x, &recursiveAttributes);
|
|
|
|
pthread_mutexattr_destroy(&recursiveAttributes);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# define LOCK(x) pthread_mutex_lock(x)
|
|
|
|
# define UNLOCK(x) pthread_mutex_unlock(x)
|
|
|
|
# define DESTROY_LOCK(x) pthread_mutex_destroy(x)
|
|
|
|
#endif
|
|
|
|
|
2010-05-14 21:47:35 +00:00
|
|
|
__attribute__((unused)) static void objc_release_lock(void *x)
|
|
|
|
{
|
|
|
|
mutex_t *lock = *(mutex_t**)x;
|
|
|
|
UNLOCK(lock);
|
|
|
|
}
|
2010-06-01 12:59:57 +00:00
|
|
|
/**
|
|
|
|
* Acquires the lock and automatically releases it at the end of the current
|
|
|
|
* scope.
|
|
|
|
*/
|
2011-04-14 16:44:34 +00:00
|
|
|
#define LOCK_FOR_SCOPE(lock) \
|
2010-05-14 21:47:35 +00:00
|
|
|
__attribute__((cleanup(objc_release_lock)))\
|
|
|
|
__attribute__((unused)) mutex_t *lock_pointer = lock;\
|
|
|
|
LOCK(lock)
|
|
|
|
|
2010-06-01 12:59:57 +00:00
|
|
|
/**
|
|
|
|
* The global runtime mutex.
|
|
|
|
*/
|
2011-04-14 16:44:34 +00:00
|
|
|
extern mutex_t runtime_mutex;
|
|
|
|
|
|
|
|
#define LOCK_RUNTIME() LOCK(&runtime_mutex)
|
|
|
|
#define UNLOCK_RUNTIME() UNLOCK(&runtime_mutex)
|
|
|
|
#define LOCK_RUNTIME_FOR_SCOPE() LOCK_FOR_SCOPE(&runtime_mutex)
|
2010-06-01 12:59:57 +00:00
|
|
|
|
2009-09-08 16:18:59 +00:00
|
|
|
#endif // __LIBOBJC_LOCK_H_INCLUDED__
|