-
Notifications
You must be signed in to change notification settings - Fork 14
/
skinny_mutex.h
62 lines (49 loc) · 1.22 KB
/
skinny_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef SKINNY_MUTEX_H
#define SKINNY_MUTEX_H
#include <pthread.h>
#include <errno.h>
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
void *val;
} skinny_mutex_t;
static __inline__ int skinny_mutex_init(skinny_mutex_t *m)
{
m->val = 0;
return 0;
}
static __inline__ int skinny_mutex_destroy(skinny_mutex_t *m)
{
return !m->val ? 0 : EBUSY;
}
#define SKINNY_MUTEX_INITIALIZER { (void *)0 }
int skinny_mutex_lock_slow(skinny_mutex_t *m);
static __inline__ int skinny_mutex_lock(skinny_mutex_t *m)
{
if (__builtin_expect(__sync_bool_compare_and_swap(&m->val,
(void *)0, (void *)1),
1))
return 0;
else
return skinny_mutex_lock_slow(m);
}
int skinny_mutex_unlock_slow(skinny_mutex_t *m);
static __inline__ int skinny_mutex_unlock(skinny_mutex_t *m)
{
if (__builtin_expect(__sync_bool_compare_and_swap(&m->val,
(void *)1, (void *)0),
1))
return 0;
else
return skinny_mutex_unlock_slow(m);
}
int skinny_mutex_trylock(skinny_mutex_t *m);
int skinny_mutex_cond_wait(pthread_cond_t *cond, skinny_mutex_t *m);
int skinny_mutex_cond_timedwait(pthread_cond_t *cond, skinny_mutex_t *m,
const struct timespec *abstime);
#ifdef __cplusplus
}
#endif
#endif /* SKINNY_MUTEX_H */