ia64/xen-unstable

view extras/mini-os/include/semaphore.h @ 16520:a583f3a7eafc

Revert 16498:d2bef6551c1263e457aef75ce403ba53652a803f.
author Keir Fraser <keir.fraser@citrix.com>
date Tue Dec 04 22:54:58 2007 +0000 (2007-12-04)
parents ebb61551565b
children a26aee4a1522
line source
1 #ifndef _SEMAPHORE_H_
2 #define _SEMAPHORE_H_
4 #include <wait.h>
5 #include <spinlock.h>
7 /*
8 * Implementation of semaphore in Mini-os is simple, because
9 * there are no preemptive threads, the atomicity is guaranteed.
10 */
12 struct semaphore
13 {
14 int count;
15 struct wait_queue_head wait;
16 };
18 /*
19 * the semaphore definition
20 */
21 struct rw_semaphore {
22 signed long count;
23 spinlock_t wait_lock;
24 struct list_head wait_list;
25 int debug;
26 };
28 #define __SEMAPHORE_INITIALIZER(name, n) \
29 { \
30 .count = n, \
31 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
32 }
34 #define __MUTEX_INITIALIZER(name) \
35 __SEMAPHORE_INITIALIZER(name,1)
37 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
38 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
40 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
42 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
44 static inline void init_SEMAPHORE(struct semaphore *sem, int count)
45 {
46 sem->count = count;
47 init_waitqueue_head(&sem->wait);
48 }
50 #define init_MUTEX(sem) init_SEMAPHORE(sem, 1)
52 static void inline down(struct semaphore *sem)
53 {
54 unsigned long flags;
55 while (1) {
56 wait_event(sem->wait, sem->count > 0);
57 local_irq_save(flags);
58 if (sem->count > 0)
59 break;
60 local_irq_restore(flags);
61 }
62 sem->count--;
63 local_irq_restore(flags);
64 }
66 static void inline up(struct semaphore *sem)
67 {
68 unsigned long flags;
69 local_irq_save(flags);
70 sem->count++;
71 wake_up(&sem->wait);
72 local_irq_restore(flags);
73 }
75 /* FIXME! Thre read/write semaphores are unimplemented! */
76 static inline void init_rwsem(struct rw_semaphore *sem)
77 {
78 sem->count = 1;
79 }
81 static inline void down_read(struct rw_semaphore *sem)
82 {
83 }
86 static inline void up_read(struct rw_semaphore *sem)
87 {
88 }
90 static inline void up_write(struct rw_semaphore *sem)
91 {
92 }
94 static inline void down_write(struct rw_semaphore *sem)
95 {
96 }
98 #endif /* _SEMAPHORE_H */