blob: 531637242dc8ace9fb2295ca98f4f1bfdd82a536 (
plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#include <assert.h>
#include <errno.h>
#include <pthread.h>
static void test_write_lock_unlock() {
int res;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
res = pthread_rwlock_wrlock(&rw);
assert(!res);
res = pthread_rwlock_unlock(&rw);
assert(!res);
}
static void test_read_lock_unlock() {
int res;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
res = pthread_rwlock_rdlock(&rw);
assert(!res);
res = pthread_rwlock_unlock(&rw);
assert(!res);
}
static void test_write_trylock_unlock() {
int res;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
res = pthread_rwlock_trywrlock(&rw);
assert(!res);
res = pthread_rwlock_unlock(&rw);
assert(!res);
}
static void test_read_trylock_unlock() {
int res;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
res = pthread_rwlock_tryrdlock(&rw);
assert(!res);
res = pthread_rwlock_unlock(&rw);
assert(!res);
}
static void test_write_prevents_read() {
int res;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
res = pthread_rwlock_wrlock(&rw);
assert(!res);
res = pthread_rwlock_tryrdlock(&rw);
assert(res == EBUSY);
res = pthread_rwlock_unlock(&rw);
assert(!res);
}
static void test_write_prevents_write() {
int res;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
res = pthread_rwlock_wrlock(&rw);
assert(!res);
res = pthread_rwlock_trywrlock(&rw);
assert(res == EBUSY);
res = pthread_rwlock_unlock(&rw);
assert(!res);
}
static void test_read_prevents_write() {
int res;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
res = pthread_rwlock_rdlock(&rw);
assert(!res);
res = pthread_rwlock_trywrlock(&rw);
assert(res == EBUSY);
res = pthread_rwlock_unlock(&rw);
assert(!res);
}
static void test_read_allows_read() {
int res;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
res = pthread_rwlock_rdlock(&rw);
assert(!res);
res = pthread_rwlock_tryrdlock(&rw);
assert(!res);
res = pthread_rwlock_unlock(&rw);
assert(!res);
}
static void test_attr() {
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
int pshared;
pthread_rwlockattr_getpshared(&attr, &pshared);
assert(pshared == PTHREAD_PROCESS_PRIVATE);
pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_rwlockattr_getpshared(&attr, &pshared);
assert(pshared == PTHREAD_PROCESS_SHARED);
pthread_rwlockattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
pthread_rwlockattr_getpshared(&attr, &pshared);
assert(pshared == PTHREAD_PROCESS_PRIVATE);
pthread_rwlockattr_destroy(&attr);
}
int main() {
test_write_lock_unlock();
test_read_lock_unlock();
test_write_trylock_unlock();
test_read_trylock_unlock();
test_write_prevents_read();
test_write_prevents_write();
test_read_prevents_write();
test_read_allows_read();
test_attr();
}
|