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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#include "conf.h"
#include "log.h"
#include "thread.h"
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
RCSID("$Id: thread.c,v 1.6 2000/03/21 19:18:24 shmit Exp $");
int
thread_new(thread_t *thread, void *(*thread_func)(void *), void *arg)
{
int rc;
thread->thread_func = thread_func;
rc = pthread_create(&thread->id, NULL, thread_func, arg);
if (rc != 0) {
log_err("Couldn't create thread: %s.\n", strerror(rc));
return -1;
}
return 0;
}
void
thread_kill(thread_t *thread)
{
}
int
thread_reload(thread_t *thread)
{
return 0;
}
int
mutex_new(mutex_t *lock)
{
return pthread_mutex_init(lock, NULL);
}
int
mutex_delete(mutex_t *lock)
{
return pthread_mutex_destroy(lock);
}
int
mutex_lock(mutex_t *lock)
{
return pthread_mutex_lock(lock);
}
int
mutex_unlock(mutex_t *lock)
{
return pthread_mutex_unlock(lock);
}
int
rw_mutex_new(rw_mutex_t *lock)
{
int rc;
rc = mutex_new(&lock->lock);
if (rc)
return rc;
rc = pthread_cond_init(&lock->read_sig, NULL);
if (rc) {
mutex_delete(&lock->lock);
return rc;
}
rc = pthread_cond_init(&lock->write_sig, NULL);
if (rc) {
mutex_delete(&lock->lock);
pthread_cond_destroy(&lock->read_sig);
return rc;
}
lock->state = 0;
lock->blocked_writers = 0;
return 0;
}
int
rw_mutex_read_lock(rw_mutex_t *lock)
{
int rc;
if (lock == NULL)
return EINVAL;
rc = mutex_lock(&lock->lock);
if (rc)
return rc;
/* Make sure the writers go before the readers. */
while (lock->blocked_writers || lock->state < 0) {
rc = pthread_cond_wait(&lock->read_sig, &lock->lock);
if (rc) {
mutex_unlock(&lock->lock);
return rc;
}
}
lock->state++;
mutex_unlock(&lock->lock);
return rc;
}
int
rw_mutex_write_lock(rw_mutex_t *lock)
{
int rc;
if (lock == NULL)
return EINVAL;
rc = mutex_lock(&lock->lock);
if (rc)
return rc;
/* Wait for no readers on the lock. */
while (lock->state != 0) {
lock->blocked_writers++;
rc = pthread_cond_wait(&lock->write_sig, &lock->lock);
lock->blocked_writers--;
if (rc) {
mutex_unlock(&lock->lock);
return rc;
}
}
lock->state = -1;
mutex_unlock(&lock->lock);
return rc;
}
int
rw_mutex_unlock(rw_mutex_t *lock)
{
int rc;
if (lock == NULL)
return EINVAL;
rc = mutex_lock(&lock->lock);
if (rc)
return rc;
if (lock->state > 0) {
/* We have an open read lock. */
if (--lock->state == 0 && lock->blocked_writers)
rc = pthread_cond_signal(&lock->write_sig);
} else if (lock->state < 0) {
/* We have an open writer lock. */
lock->state = 0;
if (lock->blocked_writers)
rc = pthread_cond_signal(&lock->write_sig);
else
rc = pthread_cond_broadcast(&lock->read_sig);
} else
rc = EINVAL;
mutex_unlock(&lock->lock);
return rc;
}
int
cond_new(cond_t *cond)
{
int rc;
if (cond == NULL)
return EINVAL;
rc = pthread_cond_init(&cond->id, NULL);
if (rc)
return rc;
rc = mutex_new(&cond->lock);
if (rc)
pthread_cond_destroy(&cond->id);
return rc;
}
int
cond_signal(cond_t *cond)
{
return pthread_cond_signal(&cond->id);
}
int
cond_wait(cond_t *cond)
{
return pthread_cond_wait(&cond->id, &cond->lock);
}
int
cond_timedwait(cond_t *cond, const struct timespec *abstime)
{
return pthread_cond_timedwait(&cond->id, &cond->lock, abstime);
}
int
cond_destroy(cond_t *cond)
{
return pthread_cond_destroy(&cond->id);
}
|