mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-02-16 10:40:06 +00:00
os/threading: Add mutex recursive wrapper
This commit is contained in:
parent
72be3e95fe
commit
7d8ca2c162
|
@ -54,8 +54,10 @@ extern "C" {
|
|||
struct os_mutex
|
||||
{
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
#ifndef NDEBUG
|
||||
bool initialized;
|
||||
bool recursive;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -70,6 +72,7 @@ os_mutex_init(struct os_mutex *om)
|
|||
assert(!om->initialized);
|
||||
#ifndef NDEBUG
|
||||
om->initialized = true;
|
||||
om->recursive = false;
|
||||
#endif
|
||||
return pthread_mutex_init(&om->mutex, NULL);
|
||||
}
|
||||
|
@ -119,12 +122,58 @@ static inline void
|
|||
os_mutex_destroy(struct os_mutex *om)
|
||||
{
|
||||
assert(om->initialized);
|
||||
assert(!om->recursive);
|
||||
|
||||
pthread_mutex_destroy(&om->mutex);
|
||||
|
||||
#ifndef NDEBUG
|
||||
om->initialized = false;
|
||||
om->recursive = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*!
|
||||
* Init.
|
||||
*
|
||||
* @public @memberof os_mutex
|
||||
*/
|
||||
static inline int
|
||||
os_mutex_recursive_init(struct os_mutex *om)
|
||||
{
|
||||
assert(!om->initialized);
|
||||
|
||||
#ifndef NDEBUG
|
||||
om->initialized = true;
|
||||
om->recursive = true;
|
||||
#endif
|
||||
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
int ret = pthread_mutex_init(&om->mutex, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Clean up.
|
||||
*
|
||||
* @public @memberof os_mutex
|
||||
*/
|
||||
static inline void
|
||||
os_mutex_recursive_destroy(struct os_mutex *om)
|
||||
{
|
||||
assert(om->initialized);
|
||||
assert(om->recursive);
|
||||
|
||||
os_mutex_destroy(om);
|
||||
|
||||
#ifndef NDEBUG
|
||||
om->initialized = false;
|
||||
om->recursive = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue