1*433d6423SLionel Sambuc #include "common.h"
2*433d6423SLionel Sambuc
3*433d6423SLionel Sambuc #include <ddekit/condvar.h>
4*433d6423SLionel Sambuc #include <ddekit/lock.h>
5*433d6423SLionel Sambuc #include <ddekit/memory.h>
6*433d6423SLionel Sambuc
7*433d6423SLionel Sambuc #ifdef DDEBUG_LEVEL_CONDVAR
8*433d6423SLionel Sambuc #undef DDEBUG
9*433d6423SLionel Sambuc #define DDEBUG DDEBUG_LEVEL_CONDVAR
10*433d6423SLionel Sambuc #endif
11*433d6423SLionel Sambuc
12*433d6423SLionel Sambuc #include "debug.h"
13*433d6423SLionel Sambuc #include "util.h"
14*433d6423SLionel Sambuc #include "thread.h"
15*433d6423SLionel Sambuc
16*433d6423SLionel Sambuc struct ddekit_condvar {
17*433d6423SLionel Sambuc ddekit_thread_t * wait_queue;
18*433d6423SLionel Sambuc };
19*433d6423SLionel Sambuc
20*433d6423SLionel Sambuc /*****************************************************************************/
21*433d6423SLionel Sambuc /* ddekit_condvar_init */
22*433d6423SLionel Sambuc /*****************************************************************************/
ddekit_condvar_init(void)23*433d6423SLionel Sambuc ddekit_condvar_t * ddekit_condvar_init(void) {
24*433d6423SLionel Sambuc ddekit_condvar_t *cv;
25*433d6423SLionel Sambuc cv = (ddekit_condvar_t *) ddekit_simple_malloc(sizeof(ddekit_condvar_t));
26*433d6423SLionel Sambuc DDEBUG_MSG_VERBOSE("cv: %p", cv);
27*433d6423SLionel Sambuc return cv;
28*433d6423SLionel Sambuc }
29*433d6423SLionel Sambuc
30*433d6423SLionel Sambuc /*****************************************************************************/
31*433d6423SLionel Sambuc /* ddekit_condvar_deinit */
32*433d6423SLionel Sambuc /*****************************************************************************/
ddekit_condvar_deinit(ddekit_condvar_t * cvp)33*433d6423SLionel Sambuc void ddekit_condvar_deinit(ddekit_condvar_t *cvp) {
34*433d6423SLionel Sambuc DDEBUG_MSG_VERBOSE("cv: %p", cvp);
35*433d6423SLionel Sambuc ddekit_simple_free(cvp);
36*433d6423SLionel Sambuc }
37*433d6423SLionel Sambuc
38*433d6423SLionel Sambuc /*****************************************************************************/
39*433d6423SLionel Sambuc /* ddekit_condvar_wait */
40*433d6423SLionel Sambuc /*****************************************************************************/
ddekit_condvar_wait(ddekit_condvar_t * cv,ddekit_lock_t * mp)41*433d6423SLionel Sambuc void ddekit_condvar_wait(ddekit_condvar_t *cv, ddekit_lock_t *mp) {
42*433d6423SLionel Sambuc
43*433d6423SLionel Sambuc DDEBUG_MSG_VERBOSE("wait cv: %p, thread id: %d, name: %s",
44*433d6423SLionel Sambuc cv, ddekit_thread_myself()->id, ddekit_thread_myself()->name);
45*433d6423SLionel Sambuc
46*433d6423SLionel Sambuc ddekit_lock_unlock(mp);
47*433d6423SLionel Sambuc
48*433d6423SLionel Sambuc if(cv->wait_queue == NULL) {
49*433d6423SLionel Sambuc cv->wait_queue = ddekit_thread_myself();
50*433d6423SLionel Sambuc } else {
51*433d6423SLionel Sambuc ddekit_thread_t *pos = cv->wait_queue;
52*433d6423SLionel Sambuc while(pos->next != NULL) {
53*433d6423SLionel Sambuc pos = pos->next;
54*433d6423SLionel Sambuc }
55*433d6423SLionel Sambuc pos->next = ddekit_thread_myself();
56*433d6423SLionel Sambuc }
57*433d6423SLionel Sambuc
58*433d6423SLionel Sambuc _ddekit_thread_schedule();
59*433d6423SLionel Sambuc
60*433d6423SLionel Sambuc DDEBUG_MSG_VERBOSE("wakeup cv: %p, thread id: %d, name: %s",
61*433d6423SLionel Sambuc cv, ddekit_thread_myself()->id, ddekit_thread_myself()->name);
62*433d6423SLionel Sambuc
63*433d6423SLionel Sambuc ddekit_lock_lock(mp);
64*433d6423SLionel Sambuc }
65*433d6423SLionel Sambuc /*****************************************************************************/
66*433d6423SLionel Sambuc /* ddekit_condvar_wait_timed */
67*433d6423SLionel Sambuc /*****************************************************************************/
ddekit_condvar_wait_timed(ddekit_condvar_t * cvp,ddekit_lock_t * mp,int timo)68*433d6423SLionel Sambuc int ddekit_condvar_wait_timed
69*433d6423SLionel Sambuc (ddekit_condvar_t *cvp, ddekit_lock_t *mp, int timo)
70*433d6423SLionel Sambuc {
71*433d6423SLionel Sambuc /*
72*433d6423SLionel Sambuc * Only used by ddefbsd, so not implemented
73*433d6423SLionel Sambuc */
74*433d6423SLionel Sambuc WARN_UNIMPL;
75*433d6423SLionel Sambuc return 0;
76*433d6423SLionel Sambuc }
77*433d6423SLionel Sambuc
78*433d6423SLionel Sambuc
79*433d6423SLionel Sambuc /*****************************************************************************/
80*433d6423SLionel Sambuc /* ddekit_condvar_signal */
81*433d6423SLionel Sambuc /*****************************************************************************/
ddekit_condvar_signal(ddekit_condvar_t * cv)82*433d6423SLionel Sambuc void ddekit_condvar_signal(ddekit_condvar_t *cv)
83*433d6423SLionel Sambuc {
84*433d6423SLionel Sambuc
85*433d6423SLionel Sambuc DDEBUG_MSG_VERBOSE("cv: %p", cv);
86*433d6423SLionel Sambuc
87*433d6423SLionel Sambuc if(cv->wait_queue) {
88*433d6423SLionel Sambuc ddekit_thread_t *th = cv->wait_queue;
89*433d6423SLionel Sambuc cv->wait_queue = th->next;
90*433d6423SLionel Sambuc th->next = NULL;
91*433d6423SLionel Sambuc _ddekit_thread_enqueue(th);
92*433d6423SLionel Sambuc
93*433d6423SLionel Sambuc DDEBUG_MSG_VERBOSE("waking up cv: %p, thread id: %d, name: %s",
94*433d6423SLionel Sambuc cv, th->id, th->name);
95*433d6423SLionel Sambuc }
96*433d6423SLionel Sambuc ddekit_thread_schedule();
97*433d6423SLionel Sambuc }
98*433d6423SLionel Sambuc
99*433d6423SLionel Sambuc
100*433d6423SLionel Sambuc /*****************************************************************************/
101*433d6423SLionel Sambuc /* ddekit_condvar_broadcast */
102*433d6423SLionel Sambuc /*****************************************************************************/
ddekit_condvar_broadcast(ddekit_condvar_t * cv)103*433d6423SLionel Sambuc void ddekit_condvar_broadcast(ddekit_condvar_t *cv) {
104*433d6423SLionel Sambuc
105*433d6423SLionel Sambuc DDEBUG_MSG_VERBOSE("cv: %p", cv);
106*433d6423SLionel Sambuc
107*433d6423SLionel Sambuc while (cv->wait_queue) {
108*433d6423SLionel Sambuc ddekit_thread_t *th = cv->wait_queue;
109*433d6423SLionel Sambuc cv->wait_queue = th->next;
110*433d6423SLionel Sambuc th->next = NULL;
111*433d6423SLionel Sambuc _ddekit_thread_enqueue(th);
112*433d6423SLionel Sambuc
113*433d6423SLionel Sambuc DDEBUG_MSG_VERBOSE("waking up cv: %p, thread id: %d, name: %s",
114*433d6423SLionel Sambuc cv, th->id, th->name);
115*433d6423SLionel Sambuc
116*433d6423SLionel Sambuc }
117*433d6423SLionel Sambuc ddekit_thread_schedule();
118*433d6423SLionel Sambuc }
119*433d6423SLionel Sambuc
120