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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
#include <stdio.h>
#include <string.h>
#include <cthreads.h>
#include <time.h>
#include <error.h>
#include "ddekit/semaphore.h"
#include "list.h"
#include "ddekit/thread.h"
#define DDEKIT_THREAD_STACK_SIZE 0x2000 /* 8 KB */
static struct ddekit_slab *ddekit_stack_slab = NULL;
struct _ddekit_private_data {
struct list list;
condition_t sleep_cond;
/* point to the thread who has the private data. */
struct ddekit_thread *thread;
mach_msg_header_t wakeupmsg;
}
struct ddekit_thread {
struct cthread thread;
};
struct ddekit_sem
{
spin_lock_t lock;
/* A list of thread waiting for the semaphore. */
struct list head;
int value;
};
/* Prepare a wakeup message. */
static error_t _create_wakeupmsg (struct _ddekit_private_data *data)
{
kern_return_t err;
/* Build wakeup message. */
data->wakeupmsg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0);
data->wakeupmsg.msgh_size = 0;
err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE,
&data->wakeupmsg.msgh_remote_port);
if (err)
return EAGAIN;
data->wakeupmsg.msgh_local_port = MACH_PORT_NULL;
data->wakeupmsg.msgh_seqno = 0;
data->wakeupmsg.msgh_id = 0;
err = mach_port_insert_right (mach_task_self (),
data->wakeupmsg.msgh_remote_port,
data->wakeupmsg.msgh_remote_port,
MACH_MSG_TYPE_MAKE_SEND);
if (err) {
mach_port_destroy (mach_task_self (),
data->wakeupmsg.msgh_remote_port);
return EAGAIN;
}
return 0;
}
static void setup_thread (cthread_t *t, const char *name) {
error_t err;
struct _ddekit_private_data *private_data;
if (name) {
const char *cpy = NULL;
cpy = malloc (strlen (name) + 1);
if (cpy == NULL)
error (0, 0, "fail to allocate memory");
else
strcpy (cpy, name);
cthread_set_name (t, name);
}
/*
* ldata isn't used by cthread. Since cthread isn't exposed to
* the user of this library. It's very safe to store
* the condition variable in ldata.
*/
private_data = (struct _ddekit_private_data *)
ddekit_simple_malloc (sizeof (*private_data));
private_data->sleep_cond = condition_alloc ();
condition_init (private_data->sleep_cond);
private_data->list = {&private_data->list, &private_data->list};
private_data->thread = t;
err = _create_wakeupmsg (private_data);
// TODO I need to change this.
assert_perror (err);
cthread_set_ldata (t, private_data);
}
ddekit_thread_t *ddekit_thread_setup_myself(const char *name) {
ddekit_thread_t *td = ddekit_thread_myself();
setup_thread (&td->thread, name);
return td;
}
ddekit_thread_t *ddekit_thread_create(void (*fun)(void *), void *arg, const char *name) {
ddekit_thread_t *td;
// TODO I should let the thread suspend
// before initialization is completed.
td = (ddekit_thread_t *) cthread_fork (fun, arg);
cthread_detach (&td->thread);
setup_thread (&td->thread, name);
return td;
}
ddekit_thread_t *ddekit_thread_myself(void) {
return (ddekit_thread_t *) cthread_self ();
}
void ddekit_thread_set_data(ddekit_thread_t *thread, void *data) {
cthread_set_data ((cthread_t) thread, data);
}
void ddekit_thread_set_my_data(void *data) {
ddekit_thread_set_data(ddekit_thread_myself(), data);
}
void *ddekit_thread_get_data(ddekit_thread_t *thread) {
return cthread_data ((cthread_t) thread);
}
void *ddekit_thread_get_my_data() {
return ddekit_thread_get_data(ddekit_thread_myself());
}
void ddekit_thread_msleep(unsigned long msecs) {
int ret;
struct timespec rgt;
rgt.tv_sec = (time_t) (msecs / 1000);
rgt.tv_nsec = (msecs % 1000) * 1000 * 1000;
ret = nanosleep (&rgt , NULL);
if (ret < 0)
error (0, errno, "nanosleep");
}
void ddekit_thread_usleep(unsigned long usecs) {
int ret;
struct timespec rgt;
rgt.tv_sec = (time_t) (usecs / 1000 / 1000);
rgt.tv_nsec = (usecs % (1000 * 1000)) * 1000;
ret = nanosleep (&rgt , NULL);
if (ret < 0)
error (0, errno, "nanosleep");
}
void ddekit_thread_nsleep(unsigned long nsecs) {
int ret;
struct timespec rgt;
rgt.tv_sec = (time_t) (nsecs / 1000 / 1000 / 1000);
rgt.tv_nsec = nsecs % (1000 * 1000 * 1000);
ret = nanosleep (&rgt , NULL);
if (ret < 0)
error (0, errno, "nanosleep");
}
void ddekit_thread_sleep(ddekit_lock_t *lock) {
struct _ddekit_private_data *data = cthread_ldata (cthread_self ());
// TODO condition_wait cannot guarantee that the thread is
// woke up by another thread, maybe by signals.
// Does it matter here?
condition_wait (data->sleep_cond, lock);
}
void dekit_thread_wakeup(ddekit_thread_t *td) {
struct _ddekit_private_data *data = cthread_ldata (cthread_self ());
condition_signal (data->sleep_cond);
}
void ddekit_thread_exit() {
const char *name;
struct _ddekit_private_data *data;
cthread_t t = cthread_self ();
// TODO I hope I don't need a lock to protect ldata and name.
/* I have to free the sleep condition variable
* before the thread exits. */
data = cthread_ldata (t);
cthread_set_ldata (t, NULL);
condition_free (data->sleep_cond);
ddekit_simple_free (data);
name = cthread_name (t);
cthread_set_name (t, NULL);
free (name);
cthread_exit (0);
}
const char *ddekit_thread_get_name(ddekit_thread_t *thread) {
return cthread_name ((cthread_t) thread);
}
void ddekit_thread_schedule(void)
{
cthread_yield();
}
void ddekit_yield(void)
{
cthread_yield();
}
void ddekit_init_threads() {
// TODO maybe the name has already been set.
cthread_set_name (cthread_self (), "main");
}
/* Block THREAD. */
static error_t _timedblock (struct _ddekit_private_data *data,
const struct timespec *abstime)
{
error_t err;
mach_msg_header_t msg;
mach_msg_timeout_t timeout;
struct timeval now;
/* We have an absolute time and now we have to convert it to a
relative time. Arg. */
err = gettimeofday(&now, NULL);
assert (! err);
if (now.tv_sec > abstime->tv_sec
|| (now.tv_sec == abstime->tv_sec
&& now.tv_usec > ((abstime->tv_nsec + 999) / 1000)))
return ETIMEDOUT;
timeout = (abstime->tv_sec - now.tv_sec) * 1000;
if (((abstime->tv_nsec + 999) / 1000) >= now.tv_usec)
timeout -= (((abstime->tv_nsec + 999) / 1000)
- now.tv_usec + 999) / 1000;
else
/* Need to do a carry. */
timeout -= 1000 + ((abstime->tv_nsec + 999999) / 1000000)
- (now.tv_usec + 999) / 1000;
err = mach_msg (&msg, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0,
sizeof msg, data->wakeupmsg.msgh_remote_port,
timeout, MACH_PORT_NULL);
if (err == EMACH_RCV_TIMED_OUT)
return ETIMEDOUT;
assert_perror (err);
return 0;
}
/* Block THREAD. */
static void _block (struct _ddekit_private_data *data)
{
mach_msg_header_t msg;
error_t err;
err = mach_msg (&msg, MACH_RCV_MSG, 0, sizeof msg,
data->wakeupmsg.msgh_remote_port,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
assert_perror (err);
}
static int _sem_timedwait_internal (sem_t *restrict sem,
const struct timespec *restrict timeout)
{
struct ddekit_private_data *self_private_data;
spin_lock (&sem->lock);
if (sem->value > 0) {
/* Successful down. */
sem->value --;
spin_unlock (&sem->__lock);
return 0;
}
if (timeout && (timeout->tv_nsec < 0
|| timeout->tv_nsec >= 1000000000)) {
errno = EINVAL;
return -1;
}
/* Add ourselves to the queue. */
self_private_data = cthread_ldata (cthread_self ());
add_entry_head (&sem->head, (struct list *) self_private_data);
spin_unlock (&sem->lock);
/* Block the thread. */
if (timeout) {
error_t err;
err = _timedblock (self_private_data, timeout);
if (err) {
/* We timed out. We may need to disconnect ourself from the
waiter queue.
FIXME: What do we do if we get a wakeup message before we
disconnect ourself? It may remain until the next time we
block. */
assert (err == ETIMEDOUT);
spin_lock (&sem->lock);
remove_entry ((struct list *) self_private_data);
spin_unlock (&sem->lock);
errno = err;
return -1;
}
}
else
_block (self_private_data);
return 0;
}
/* Wakeup THREAD. */
static void _thread_wakeup (struct _ddekit_private_data *data)
{
error_t err;
err = mach_msg (&data->wakeupmsg, MACH_SEND_MSG,
sizeof (data->wakeupmsg), 0, MACH_PORT_NULL,
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
assert_perror (err);
}
ddekit_sem_t *ddekit_sem_init(int value) {
ddekit_sem_t *sem =
(ddekit_sem_t *) ddekit_simple_malloc (sizeof (*sem));
sem->lock = SPIN_LOCK_INITIALIZER;
sem->head = {&sem->head, &sem->head};
sem->value = value;
return sem;
}
void ddekit_sem_deinit(ddekit_sem_t *sem) {
if (!EMPTY_ENTRY (&sem->head)) {
error (0, EBUSY, "ddekit_sem_deinit");
}
else
ddekit_simple_free(sem);
}
void ddekit_sem_down(ddekit_sem_t *sem) {
_sem_timedwait_internal (sem, NULL);
}
/* returns 0 on success, != 0 when it would block */
int ddekit_sem_down_try(ddekit_sem_t *sem) {
spin_lock (&sem->lock);
if (sem->value > 0) {
/* Successful down. */
sem->value --;
spin_unlock (&sem->lock);
return 0;
}
spin_unlock (&sem->lock);
return -1;
}
/* returns 0 on success, != 0 on timeout */
int ddekit_sem_down_timed(ddekit_sem_t *sem, int timo) {
/* wait for up to timo milliseconds */
struct timespec timeout;
timeout.tv_sec = timo / 1000;
timeout.tv_nsec = (timo % 1000) * 1000 * 1000;
return __sem_timedwait_internal (sem, &timeout);
}
void ddekit_sem_up(ddekit_sem_t *sem) {
struct _ddekit_thread_data *wakeup;
spin_lock (&sem->lock);
if (sem->value > 0) {
/* Do a quick up. */
assert (EMPTY_LIST (&sem->head));
sem->value ++;
spin_unlock (&sem->lock);
return 0;
}
if (EMPTY_LIST (&sem->head)) {
/* No one waiting. */
sem->value = 1;
spin_unlock (&sem->lock);
return 0;
}
/* Wake someone up. */
/* First dequeue someone. */
wakeup = (struct _ddekit_private_data *) remove_entry_end (&sem->head);
/* Then drop the lock and transfer control. */
spin_unlock (&sem->lock);
if (wakeup)
_thread_wakeup (wakeup);
return 0;
}
|