3 #include <ddekit/condvar.h>
4 #include <ddekit/lock.h>
5 #include <ddekit/memory.h>
7 #ifdef DDEBUG_LEVEL_CONDVAR
9 #define DDEBUG DDEBUG_LEVEL_CONDVAR
16 struct ddekit_condvar
{
17 ddekit_thread_t
* wait_queue
;
20 /*****************************************************************************/
21 /* ddekit_condvar_init */
22 /*****************************************************************************/
23 ddekit_condvar_t
* ddekit_condvar_init(void) {
25 cv
= (ddekit_condvar_t
*) ddekit_simple_malloc(sizeof(ddekit_condvar_t
));
26 DDEBUG_MSG_VERBOSE("cv: %p", 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();
51 ddekit_thread_t
*pos
= cv
->wait_queue
;
52 while(pos
->next
!= NULL
) {
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
);
65 /*****************************************************************************/
66 /* ddekit_condvar_wait_timed */
67 /*****************************************************************************/
68 int ddekit_condvar_wait_timed
69 (ddekit_condvar_t
*cvp
, ddekit_lock_t
*mp
, int timo
)
72 * Only used by ddefbsd, so not implemented
79 /*****************************************************************************/
80 /* ddekit_condvar_signal */
81 /*****************************************************************************/
82 void ddekit_condvar_signal(ddekit_condvar_t
*cv
)
85 DDEBUG_MSG_VERBOSE("cv: %p", cv
);
88 ddekit_thread_t
*th
= cv
->wait_queue
;
89 cv
->wait_queue
= th
->next
;
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
;
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();