-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathwait.h
95 lines (88 loc) · 1.91 KB
/
wait.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef __WAIT_H
#define __WAIT_H
#include "lib.h"
#include "compile.h"
#include <pthread.h>
/* simulating thread blocking(used for _accept(), _read(), _recv()) */
struct tapip_wait {
pthread_mutex_t mutex;
pthread_cond_t cond;
int notified; /* log whether wait is waken up already */
int dead; /* for safe exiting wait state */
int sleep;
};
/*
* 1. @notified avoid double calling of wake_up()
* 2. If wake_up() is called before sleep_on(),
* @notified can skip next waiting of sleep_on().
*/
#ifdef WAIT_DEBUG
#define wake_up(w) \
do { \
dbg("[*]wake_up " #w); \
_wake_up(w); \
} while (0)
static _inline int _wake_up(struct tapip_wait *w)
#else
static _inline int wake_up(struct tapip_wait *w)
#endif
{
pthread_mutex_lock(&w->mutex);
/* Should we put this code before locking? */
if (w->dead)
goto unlock;
if (!w->notified) {
w->notified = 1;
if (w->sleep)
pthread_cond_signal(&w->cond);
}
unlock:
pthread_mutex_unlock(&w->mutex);
return -(w->dead);
}
#ifdef WAIT_DEBUG
#define sleep_on(w) \
({ int ret;\
dbg("[+]sleep on " #w); \
ret = _sleep_on(w); \
dbg("[-]Be waken up " #w); \
ret; \
})
static _inline int _sleep_on(struct tapip_wait *w)
#else
static _inline int sleep_on(struct tapip_wait *w)
#endif
{
pthread_mutex_lock(&w->mutex);
if (w->dead)
goto unlock;
w->sleep = 1;
if (!w->notified)
pthread_cond_wait(&w->cond, &w->mutex);
w->notified = 0;
w->sleep = 0;
unlock:
pthread_mutex_unlock(&w->mutex);
return -(w->dead);
}
static _inline void wait_init(struct tapip_wait *w)
{
/* XXX: Should it need error checking? */
pthread_cond_init(&w->cond, NULL);
pthread_mutex_init(&w->mutex, NULL);
w->dead = 0;
w->notified = 0;
w->sleep = 0;
}
static _inline void wait_exit(struct tapip_wait *w)
{
pthread_mutex_lock(&w->mutex);
if (w->dead)
goto unlock;
w->dead = 1;
if (w->sleep)
pthread_cond_broadcast(&w->cond);
unlock:
pthread_mutex_unlock(&w->mutex);
}
#endif /* wait.h */