1 | #ifndef _SEMAPHORE_H_ |
---|
2 | #define _SEMAPHORE_H_ |
---|
3 | |
---|
4 | #include <wait.h> |
---|
5 | #include <spinlock.h> |
---|
6 | |
---|
7 | /* |
---|
8 | * Implementation of semaphore in Mini-os is simple, because |
---|
9 | * there are no preemptive threads, the atomicity is guaranteed. |
---|
10 | */ |
---|
11 | |
---|
12 | struct semaphore |
---|
13 | { |
---|
14 | int count; |
---|
15 | struct wait_queue_head wait; |
---|
16 | }; |
---|
17 | |
---|
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 | }; |
---|
27 | |
---|
28 | #define __SEMAPHORE_INITIALIZER(name, n) \ |
---|
29 | { \ |
---|
30 | .count = n, \ |
---|
31 | .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ |
---|
32 | } |
---|
33 | |
---|
34 | #define __MUTEX_INITIALIZER(name) \ |
---|
35 | __SEMAPHORE_INITIALIZER(name,1) |
---|
36 | |
---|
37 | #define __DECLARE_SEMAPHORE_GENERIC(name,count) \ |
---|
38 | struct semaphore name = __SEMAPHORE_INITIALIZER(name,count) |
---|
39 | |
---|
40 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1) |
---|
41 | |
---|
42 | #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0) |
---|
43 | |
---|
44 | static inline void init_MUTEX(struct semaphore *sem) |
---|
45 | { |
---|
46 | sem->count = 1; |
---|
47 | init_waitqueue_head(&sem->wait); |
---|
48 | } |
---|
49 | |
---|
50 | static void inline down(struct semaphore *sem) |
---|
51 | { |
---|
52 | wait_event(sem->wait, sem->count > 0); |
---|
53 | sem->count--; |
---|
54 | } |
---|
55 | |
---|
56 | static void inline up(struct semaphore *sem) |
---|
57 | { |
---|
58 | sem->count++; |
---|
59 | wake_up(&sem->wait); |
---|
60 | } |
---|
61 | |
---|
62 | /* FIXME! Thre read/write semaphores are unimplemented! */ |
---|
63 | static inline void init_rwsem(struct rw_semaphore *sem) |
---|
64 | { |
---|
65 | sem->count = 1; |
---|
66 | } |
---|
67 | |
---|
68 | static inline void down_read(struct rw_semaphore *sem) |
---|
69 | { |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | static inline void up_read(struct rw_semaphore *sem) |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | static inline void up_write(struct rw_semaphore *sem) |
---|
78 | { |
---|
79 | } |
---|
80 | |
---|
81 | static inline void down_write(struct rw_semaphore *sem) |
---|
82 | { |
---|
83 | } |
---|
84 | |
---|
85 | #endif /* _SEMAPHORE_H */ |
---|