ia64/xen-unstable

view extras/mini-os/include/semaphore.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 a26aee4a1522
children 707e10c9a487
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 inline int trydown(struct semaphore *sem)
53 {
54 unsigned long flags;
55 int ret = 0;
56 local_irq_save(flags);
57 if (sem->count > 0) {
58 ret = 1;
59 sem->count--;
60 }
61 local_irq_restore(flags);
62 return ret;
63 }
65 static void inline down(struct semaphore *sem)
66 {
67 unsigned long flags;
68 while (1) {
69 wait_event(sem->wait, sem->count > 0);
70 local_irq_save(flags);
71 if (sem->count > 0)
72 break;
73 local_irq_restore(flags);
74 }
75 sem->count--;
76 local_irq_restore(flags);
77 }
79 static void inline up(struct semaphore *sem)
80 {
81 unsigned long flags;
82 local_irq_save(flags);
83 sem->count++;
84 wake_up(&sem->wait);
85 local_irq_restore(flags);
86 }
88 /* FIXME! Thre read/write semaphores are unimplemented! */
89 static inline void init_rwsem(struct rw_semaphore *sem)
90 {
91 sem->count = 1;
92 }
94 static inline void down_read(struct rw_semaphore *sem)
95 {
96 }
99 static inline void up_read(struct rw_semaphore *sem)
100 {
101 }
103 static inline void up_write(struct rw_semaphore *sem)
104 {
105 }
107 static inline void down_write(struct rw_semaphore *sem)
108 {
109 }
111 #endif /* _SEMAPHORE_H */