]> git.lizzy.rs Git - dragonstd.git/blob - flag.c
a8bb87888f5ee67f3b7752f80269718fe1d108ea
[dragonstd.git] / flag.c
1 #include "flag.h"
2
3 void flag_ini(Flag *flag)
4 {
5         flag->set = false;
6         pthread_cond_init(&flag->cnd, NULL);
7         pthread_mutex_init(&flag->mtx, NULL);
8         list_ini(&flag->cvs);
9         flag_sub(flag, &flag->cnd);
10 }
11
12 void flag_dst(Flag *flag)
13 {
14         pthread_cond_destroy(&flag->cnd);
15         pthread_mutex_destroy(&flag->mtx);
16         list_clr(&flag->cvs, NULL, NULL, NULL);
17 }
18
19 void flag_sub(Flag *flag, pthread_cond_t *cnd)
20 {
21         pthread_mutex_lock(&flag->mtx);
22         list_add(&flag->cvs, cnd, &cmp_ref, NULL);
23         pthread_mutex_unlock(&flag->mtx);
24 }
25
26 void flag_uns(Flag *flag, pthread_cond_t *cnd)
27 {
28         pthread_mutex_lock(&flag->mtx);
29         list_del(&flag->cvs, cnd, &cmp_ref, NULL);
30         pthread_mutex_unlock(&flag->mtx);
31 }
32
33 void flag_set(Flag *flag)
34 {
35         pthread_mutex_lock(&flag->mtx);
36         flag->set = true;
37
38         LIST_ITERATE(&flag->cvs, node)
39                 pthread_cond_broadcast(node->dat);
40
41         pthread_mutex_unlock(&flag->mtx);
42 }
43
44 void flag_slp(Flag *flag)
45 {
46         pthread_mutex_lock(&flag->mtx);
47         if (! flag->set)
48                 pthread_cond_wait(&flag->cnd, &flag->mtx);
49         pthread_mutex_unlock(&flag->mtx);
50 }