-
Notifications
You must be signed in to change notification settings - Fork 8
/
threads.c
74 lines (51 loc) · 1.36 KB
/
threads.c
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
63
64
65
66
67
68
69
70
71
72
73
74
#include "private/threads.h"
#include "private/errors.h"
#include <pthread.h>
#include <stdlib.h>
#ifndef NDEBUG
#include <stdio.h>
#include <errno.h>
#define ENSURE(expr)\
int ret = expr;\
if (ret != 0) {\
errno = ret;\
perror(#expr);\
abort();\
}
#else
#define ENSURE(expr)\
if (expr) abort();
#endif
int bp__mutex_init(bp__mutex_t* mutex) {
return pthread_mutex_init(mutex, NULL) == 0 ? BP_OK : BP_EMUTEX;
}
void bp__mutex_destroy(bp__mutex_t* mutex) {
ENSURE(pthread_mutex_destroy(mutex));
}
void bp__mutex_lock(bp__mutex_t* mutex) {
ENSURE(pthread_mutex_lock(mutex));
}
void bp__mutex_unlock(bp__mutex_t* mutex) {
ENSURE(pthread_mutex_unlock(mutex));
}
int bp__rwlock_init(bp__rwlock_t* rwlock) {
return pthread_rwlock_init(rwlock, NULL) == 0 ? BP_OK : BP_ERWLOCK;
}
void bp__rwlock_destroy(bp__rwlock_t* rwlock) {
ENSURE(pthread_rwlock_destroy(rwlock));
}
void bp__rwlock_rdlock(bp__rwlock_t* rwlock) {
ENSURE(pthread_rwlock_rdlock(rwlock));
}
int bp__rwlock_tryrdlock(bp__rwlock_t* rwlock) {
return pthread_rwlock_tryrdlock(rwlock);
}
void bp__rwlock_wrlock(bp__rwlock_t* rwlock) {
ENSURE(pthread_rwlock_wrlock(rwlock));
}
int bp__rwlock_trywrlock(bp__rwlock_t* rwlock) {
return pthread_rwlock_trywrlock(rwlock);
}
void bp__rwlock_unlock(bp__rwlock_t* rwlock) {
ENSURE(pthread_rwlock_unlock(rwlock));
}