qio: Allow changing limits dynamically
[akaros.git] / kern / src / completion.c
1 /* Copyright (c) 2015 Google Inc
2  * Davide Libenzi <dlibenzi@google.com>
3  * See LICENSE for details.
4  */
5
6 #include <kthread.h>
7 #include <completion.h>
8
9 void completion_init(struct completion *comp, int count)
10 {
11         cv_init_irqsave(&comp->cv);
12         comp->count = count;
13 }
14
15 void completion_complete(struct completion *comp, int how_much)
16 {
17         int8_t state = 0;
18
19         cv_lock_irqsave(&comp->cv, &state);
20         comp->count -= how_much;
21         if (comp->count <= 0)
22                 __cv_broadcast(&comp->cv);
23         cv_unlock_irqsave(&comp->cv, &state);
24 }
25
26 void completion_wait(struct completion *comp)
27 {
28         int8_t state = 0;
29
30         cv_lock_irqsave(&comp->cv, &state);
31         if (comp->count > 0) {
32                 cv_wait_and_unlock(&comp->cv);
33                 enable_irqsave(&state);
34         } else {
35                 cv_unlock_irqsave(&comp->cv, &state);
36         }
37 }