-
Notifications
You must be signed in to change notification settings - Fork 3
/
mutex.h
49 lines (39 loc) · 956 Bytes
/
mutex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef __ZUNKFS_MUTEX_H__
#define __ZUNKFS_MUTEX_H__
#define _GNU_SOURCE
#include <pthread.h>
/*
* Mutex wrappers.
*/
struct mutex {
pthread_mutex_t mutex;
pthread_t owner;
};
#if !defined(NDEBUG) && defined(PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP)
#define INIT_MUTEX { PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, (pthread_t)-1 }
#else
#define INIT_MUTEX { PTHREAD_MUTEX_INITIALIZER, (pthread_t)-1 }
#endif
#define DECLARE_MUTEX(name) \
struct mutex name = INIT_MUTEX
void init_mutex(struct mutex *m);
void lock(struct mutex *m);
void unlock(struct mutex *m);
int trylock(struct mutex *m);
static inline int have_mutex(const struct mutex *m)
{
return m->owner == pthread_self();
}
#define locked_inc(value, mutex) do { \
lock(mutex); \
++ *(value); \
assert(*(value) != 0); \
unlock(mutex); \
} while(0)
#define locked_dec(value, mutex) do { \
lock(mutex); \
assert(*(value) != 0); \
-- *(value); \
unlock(mutex); \
} while(0)
#endif