Rev 3482 | Rev 5056 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3482 | Rev 4292 | ||
---|---|---|---|
1 | #ifndef _LINUX_WAIT_H |
1 | #ifndef _LINUX_WAIT_H |
2 | #define _LINUX_WAIT_H |
2 | #define _LINUX_WAIT_H |
- | 3 | ||
3 | 4 | ||
4 | #include |
5 | #include |
5 | #include |
6 | #include |
6 | 7 | ||
7 | typedef struct __wait_queue wait_queue_t; |
8 | typedef struct __wait_queue wait_queue_t; |
8 | typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key); |
9 | typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key); |
- | 10 | int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key); |
|
9 | 11 | ||
10 | typedef struct __wait_queue_head wait_queue_head_t; |
12 | typedef struct __wait_queue_head wait_queue_head_t; |
11 | 13 | ||
12 | struct __wait_queue |
14 | struct __wait_queue |
13 | { |
15 | { |
14 | wait_queue_func_t func; |
16 | wait_queue_func_t func; |
15 | struct list_head task_list; |
17 | struct list_head task_list; |
16 | evhandle_t evnt; |
18 | evhandle_t evnt; |
17 | }; |
19 | }; |
18 | 20 | ||
19 | struct __wait_queue_head |
21 | struct __wait_queue_head |
20 | { |
22 | { |
21 | spinlock_t lock; |
23 | spinlock_t lock; |
22 | struct list_head task_list; |
24 | struct list_head task_list; |
23 | }; |
25 | }; |
- | 26 | static inline int waitqueue_active(wait_queue_head_t *q) |
|
- | 27 | { |
|
- | 28 | return !list_empty(&q->task_list); |
|
- | 29 | } |
|
24 | 30 | ||
25 | static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) |
31 | static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) |
26 | { |
32 | { |
27 | list_add(&new->task_list, &head->task_list); |
33 | list_add(&new->task_list, &head->task_list); |
28 | } |
34 | } |
29 | 35 | ||
30 | /* |
36 | /* |
31 | #define __wait_event(wq, condition) \ |
37 | #define __wait_event(wq, condition) \ |
32 | do { \ |
38 | do { \ |
33 | DEFINE_WAIT(__wait); \ |
39 | DEFINE_WAIT(__wait); \ |
34 | \ |
40 | \ |
35 | for (;;) { \ |
41 | for (;;) { \ |
36 | prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ |
42 | prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ |
37 | if (condition) \ |
43 | if (condition) \ |
38 | break; \ |
44 | break; \ |
39 | schedule(); \ |
45 | schedule(); \ |
40 | } \ |
46 | } \ |
41 | finish_wait(&wq, &__wait); \ |
47 | finish_wait(&wq, &__wait); \ |
42 | } while (0) |
48 | } while (0) |
43 | 49 | ||
44 | */ |
50 | */ |
45 | 51 | ||
46 | #define wait_event_timeout(wq, condition, timeout) \ |
52 | #define wait_event_timeout(wq, condition, timeout) \ |
47 | ({ \ |
53 | ({ \ |
48 | long __ret = timeout; \ |
54 | long __ret = timeout; \ |
49 | do{ \ |
55 | do{ \ |
50 | wait_queue_t __wait = { \ |
56 | wait_queue_t __wait = { \ |
51 | .task_list = LIST_HEAD_INIT(__wait.task_list), \ |
57 | .task_list = LIST_HEAD_INIT(__wait.task_list), \ |
52 | .evnt = CreateEvent(NULL, MANUAL_DESTROY), \ |
58 | .evnt = CreateEvent(NULL, MANUAL_DESTROY), \ |
53 | }; \ |
59 | }; \ |
54 | unsigned long flags; \ |
60 | unsigned long flags; \ |
55 | \ |
61 | \ |
56 | spin_lock_irqsave(&wq.lock, flags); \ |
62 | spin_lock_irqsave(&wq.lock, flags); \ |
57 | if (list_empty(&__wait.task_list)) \ |
63 | if (list_empty(&__wait.task_list)) \ |
58 | __add_wait_queue(&wq, &__wait); \ |
64 | __add_wait_queue(&wq, &__wait); \ |
59 | spin_unlock_irqrestore(&wq.lock, flags); \ |
65 | spin_unlock_irqrestore(&wq.lock, flags); \ |
60 | \ |
66 | \ |
61 | for(;;){ \ |
67 | for(;;){ \ |
62 | if (condition) \ |
68 | if (condition) \ |
63 | break; \ |
69 | break; \ |
64 | WaitEventTimeout(__wait.evnt, timeout); \ |
70 | WaitEventTimeout(__wait.evnt, timeout); \ |
65 | }; \ |
71 | }; \ |
66 | if (!list_empty(&__wait.task_list)) { \ |
72 | if (!list_empty(&__wait.task_list)) { \ |
67 | spin_lock_irqsave(&wq.lock, flags); \ |
73 | spin_lock_irqsave(&wq.lock, flags); \ |
68 | list_del_init(&__wait.task_list); \ |
74 | list_del_init(&__wait.task_list); \ |
69 | spin_unlock_irqrestore(&wq.lock, flags); \ |
75 | spin_unlock_irqrestore(&wq.lock, flags); \ |
70 | }; \ |
76 | }; \ |
71 | DestroyEvent(__wait.evnt); \ |
77 | DestroyEvent(__wait.evnt); \ |
72 | } while (0); \ |
78 | } while (0); \ |
73 | __ret; \ |
79 | __ret; \ |
74 | }) |
80 | }) |
75 | 81 | ||
76 | #define wait_event_interruptible_timeout(wq, condition, timeout) \ |
82 | #define wait_event_interruptible_timeout(wq, condition, timeout) \ |
77 | wait_event_timeout(wq, condition, timeout) |
83 | wait_event_timeout(wq, condition, timeout) |
78 | 84 | ||
79 | 85 | ||
80 | #define wait_event(wq, condition) \ |
86 | #define wait_event(wq, condition) \ |
81 | do{ \ |
87 | do{ \ |
82 | wait_queue_t __wait = { \ |
88 | wait_queue_t __wait = { \ |
83 | .task_list = LIST_HEAD_INIT(__wait.task_list), \ |
89 | .task_list = LIST_HEAD_INIT(__wait.task_list), \ |
84 | .evnt = CreateEvent(NULL, MANUAL_DESTROY), \ |
90 | .evnt = CreateEvent(NULL, MANUAL_DESTROY), \ |
85 | }; \ |
91 | }; \ |
86 | unsigned long flags; \ |
92 | unsigned long flags; \ |
87 | \ |
93 | \ |
88 | spin_lock_irqsave(&wq.lock, flags); \ |
94 | spin_lock_irqsave(&wq.lock, flags); \ |
89 | if (list_empty(&__wait.task_list)) \ |
95 | if (list_empty(&__wait.task_list)) \ |
90 | __add_wait_queue(&wq, &__wait); \ |
96 | __add_wait_queue(&wq, &__wait); \ |
91 | spin_unlock_irqrestore(&wq.lock, flags); \ |
97 | spin_unlock_irqrestore(&wq.lock, flags); \ |
92 | \ |
98 | \ |
93 | for(;;){ \ |
99 | for(;;){ \ |
94 | if (condition) \ |
100 | if (condition) \ |
95 | break; \ |
101 | break; \ |
96 | WaitEvent(__wait.evnt); \ |
102 | WaitEvent(__wait.evnt); \ |
97 | }; \ |
103 | }; \ |
98 | if (!list_empty_careful(&__wait.task_list)) { \ |
104 | if (!list_empty_careful(&__wait.task_list)) { \ |
99 | spin_lock_irqsave(&wq.lock, flags); \ |
105 | spin_lock_irqsave(&wq.lock, flags); \ |
100 | list_del_init(&__wait.task_list); \ |
106 | list_del_init(&__wait.task_list); \ |
101 | spin_unlock_irqrestore(&wq.lock, flags); \ |
107 | spin_unlock_irqrestore(&wq.lock, flags); \ |
102 | }; \ |
108 | }; \ |
103 | DestroyEvent(__wait.evnt); \ |
109 | DestroyEvent(__wait.evnt); \ |
104 | } while (0) |
110 | } while (0) |
105 | 111 | ||
106 | 112 | ||
107 | 113 | ||
108 | 114 | ||
109 | static inline |
115 | static inline |
110 | void wake_up_all(wait_queue_head_t *q) |
116 | void wake_up_all(wait_queue_head_t *q) |
111 | { |
117 | { |
112 | wait_queue_t *curr; |
118 | wait_queue_t *curr; |
113 | unsigned long flags; |
119 | unsigned long flags; |
114 | 120 | ||
115 | spin_lock_irqsave(&q->lock, flags); |
121 | spin_lock_irqsave(&q->lock, flags); |
116 | list_for_each_entry(curr, &q->task_list, task_list) |
122 | list_for_each_entry(curr, &q->task_list, task_list) |
117 | { |
123 | { |
118 | // printf("raise event \n"); |
124 | // printf("raise event \n"); |
119 | 125 | ||
120 | kevent_t event; |
126 | kevent_t event; |
121 | event.code = -1; |
127 | event.code = -1; |
122 | RaiseEvent(curr->evnt, 0, &event); |
128 | RaiseEvent(curr->evnt, 0, &event); |
123 | } |
129 | } |
124 | spin_unlock_irqrestore(&q->lock, flags); |
130 | spin_unlock_irqrestore(&q->lock, flags); |
125 | } |
131 | } |
126 | 132 | ||
127 | 133 | ||
128 | static inline void |
134 | static inline void |
129 | init_waitqueue_head(wait_queue_head_t *q) |
135 | init_waitqueue_head(wait_queue_head_t *q) |
130 | { |
136 | { |
131 | spin_lock_init(&q->lock); |
137 | spin_lock_init(&q->lock); |
132 | INIT_LIST_HEAD(&q->task_list); |
138 | INIT_LIST_HEAD(&q->task_list); |
133 | }; |
139 | }; |
134 | 140 | ||
135 | 141 | ||
136 | struct completion { |
142 | struct completion { |
137 | unsigned int done; |
143 | unsigned int done; |
138 | wait_queue_head_t wait; |
144 | wait_queue_head_t wait; |
139 | }; |
145 | }; |
140 | 146 | ||
141 | int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
147 | int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
142 | 148 | ||
143 | 149 | ||
144 | #define DEFINE_WAIT_FUNC(name, function) \ |
150 | #define DEFINE_WAIT_FUNC(name, function) \ |
145 | wait_queue_t name = { \ |
151 | wait_queue_t name = { \ |
146 | .func = function, \ |
152 | .func = function, \ |
147 | .task_list = LIST_HEAD_INIT((name).task_list), \ |
153 | .task_list = LIST_HEAD_INIT((name).task_list), \ |
148 | .evnt = CreateEvent(NULL, MANUAL_DESTROY), \ |
154 | .evnt = CreateEvent(NULL, MANUAL_DESTROY), \ |
149 | } |
155 | } |
150 | 156 | ||
151 | #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) |
157 | #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) |
152 | 158 | ||
153 | 159 | ||
154 | #endif |
160 | #endif |