Remove building with NOCRYPTO option
[minix.git] / minix / lib / libddekit / src / condvar.c
blobffea3571785f884f3b8220726a98671c0e9e0c3b
1 #include "common.h"
3 #include <ddekit/condvar.h>
4 #include <ddekit/lock.h>
5 #include <ddekit/memory.h>
7 #ifdef DDEBUG_LEVEL_CONDVAR
8 #undef DDEBUG
9 #define DDEBUG DDEBUG_LEVEL_CONDVAR
10 #endif
12 #include "debug.h"
13 #include "util.h"
14 #include "thread.h"
16 struct ddekit_condvar {
17 ddekit_thread_t * wait_queue;
20 /*****************************************************************************/
21 /* ddekit_condvar_init */
22 /*****************************************************************************/
23 ddekit_condvar_t * ddekit_condvar_init(void) {
24 ddekit_condvar_t *cv;
25 cv = (ddekit_condvar_t *) ddekit_simple_malloc(sizeof(ddekit_condvar_t));
26 DDEBUG_MSG_VERBOSE("cv: %p", cv);
27 return cv;
30 /*****************************************************************************/
31 /* ddekit_condvar_deinit */
32 /*****************************************************************************/
33 void ddekit_condvar_deinit(ddekit_condvar_t *cvp) {
34 DDEBUG_MSG_VERBOSE("cv: %p", cvp);
35 ddekit_simple_free(cvp);
38 /*****************************************************************************/
39 /* ddekit_condvar_wait */
40 /*****************************************************************************/
41 void ddekit_condvar_wait(ddekit_condvar_t *cv, ddekit_lock_t *mp) {
43 DDEBUG_MSG_VERBOSE("wait cv: %p, thread id: %d, name: %s",
44 cv, ddekit_thread_myself()->id, ddekit_thread_myself()->name);
46 ddekit_lock_unlock(mp);
48 if(cv->wait_queue == NULL) {
49 cv->wait_queue = ddekit_thread_myself();
50 } else {
51 ddekit_thread_t *pos = cv->wait_queue;
52 while(pos->next != NULL) {
53 pos = pos->next;
55 pos->next = ddekit_thread_myself();
58 _ddekit_thread_schedule();
60 DDEBUG_MSG_VERBOSE("wakeup cv: %p, thread id: %d, name: %s",
61 cv, ddekit_thread_myself()->id, ddekit_thread_myself()->name);
63 ddekit_lock_lock(mp);
65 /*****************************************************************************/
66 /* ddekit_condvar_wait_timed */
67 /*****************************************************************************/
68 int ddekit_condvar_wait_timed
69 (ddekit_condvar_t *cvp, ddekit_lock_t *mp, int timo)
71 /*
72 * Only used by ddefbsd, so not implemented
74 WARN_UNIMPL;
75 return 0;
79 /*****************************************************************************/
80 /* ddekit_condvar_signal */
81 /*****************************************************************************/
82 void ddekit_condvar_signal(ddekit_condvar_t *cv)
85 DDEBUG_MSG_VERBOSE("cv: %p", cv);
87 if(cv->wait_queue) {
88 ddekit_thread_t *th = cv->wait_queue;
89 cv->wait_queue = th->next;
90 th->next = NULL;
91 _ddekit_thread_enqueue(th);
93 DDEBUG_MSG_VERBOSE("waking up cv: %p, thread id: %d, name: %s",
94 cv, th->id, th->name);
96 ddekit_thread_schedule();
100 /*****************************************************************************/
101 /* ddekit_condvar_broadcast */
102 /*****************************************************************************/
103 void ddekit_condvar_broadcast(ddekit_condvar_t *cv) {
105 DDEBUG_MSG_VERBOSE("cv: %p", cv);
107 while (cv->wait_queue) {
108 ddekit_thread_t *th = cv->wait_queue;
109 cv->wait_queue = th->next;
110 th->next = NULL;
111 _ddekit_thread_enqueue(th);
113 DDEBUG_MSG_VERBOSE("waking up cv: %p, thread id: %d, name: %s",
114 cv, th->id, th->name);
117 ddekit_thread_schedule();