ia64/xen-unstable

view extras/mini-os/include/wait.h @ 16838:945820bfedb6

minios: POSIX fixes
Fixes some functions which are POSIX. Also make them ifndef HAVE_LIBC.

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jan 22 14:20:22 2008 +0000 (2008-01-22)
parents 1101ca828ad9
children 8a0656950b1c
line source
1 #ifndef __WAIT_H__
2 #define __WAIT_H__
4 #include <sched.h>
5 #include <list.h>
6 #include <lib.h>
7 #include <os.h>
9 struct wait_queue
10 {
11 struct thread *thread;
12 struct list_head thread_list;
13 };
15 struct wait_queue_head
16 {
17 /* TODO - lock required? */
18 struct list_head thread_list;
19 };
21 #define DECLARE_WAIT_QUEUE_HEAD(name) \
22 struct wait_queue_head name = \
23 { .thread_list = { &(name).thread_list, &(name).thread_list} }
25 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
26 .thread_list = { &(name).thread_list, &(name).thread_list } }
29 #define DEFINE_WAIT(name) \
30 struct wait_queue name = { \
31 .thread = current, \
32 .thread_list = LIST_HEAD_INIT((name).thread_list), \
33 }
36 static inline void init_waitqueue_head(struct wait_queue_head *h)
37 {
38 INIT_LIST_HEAD(&h->thread_list);
39 }
41 static inline void init_waitqueue_entry(struct wait_queue *q, struct thread *thread)
42 {
43 q->thread = thread;
44 INIT_LIST_HEAD(&q->thread_list);
45 }
48 static inline void add_wait_queue(struct wait_queue_head *h, struct wait_queue *q)
49 {
50 if (list_empty(&q->thread_list))
51 list_add(&q->thread_list, &h->thread_list);
52 }
54 static inline void remove_wait_queue(struct wait_queue *q)
55 {
56 list_del(&q->thread_list);
57 }
59 static inline void wake_up(struct wait_queue_head *head)
60 {
61 unsigned long flags;
62 struct list_head *tmp, *next;
63 local_irq_save(flags);
64 list_for_each_safe(tmp, next, &head->thread_list)
65 {
66 struct wait_queue *curr;
67 curr = list_entry(tmp, struct wait_queue, thread_list);
68 wake(curr->thread);
69 }
70 local_irq_restore(flags);
71 }
73 #define add_waiter(w, wq) do { \
74 unsigned long flags; \
75 local_irq_save(flags); \
76 add_wait_queue(&wq, &w); \
77 block(current); \
78 local_irq_restore(flags); \
79 } while (0)
81 #define remove_waiter(w) do { \
82 unsigned long flags; \
83 local_irq_save(flags); \
84 remove_wait_queue(&w); \
85 local_irq_restore(flags); \
86 } while (0)
88 #define wait_event_deadline(wq, condition, deadline) do { \
89 unsigned long flags; \
90 if(condition) \
91 break; \
92 DEFINE_WAIT(__wait); \
93 for(;;) \
94 { \
95 /* protect the list */ \
96 local_irq_save(flags); \
97 add_wait_queue(&wq, &__wait); \
98 current->wakeup_time = deadline; \
99 clear_runnable(current); \
100 local_irq_restore(flags); \
101 if((condition) || (deadline && NOW() >= deadline)) \
102 break; \
103 schedule(); \
104 } \
105 local_irq_save(flags); \
106 /* need to wake up */ \
107 wake(current); \
108 remove_wait_queue(&__wait); \
109 local_irq_restore(flags); \
110 } while(0)
112 #define wait_event(wq, condition) wait_event_deadline(wq, condition, 0)
116 #endif /* __WAIT_H__ */