diff options
Diffstat (limited to 'arch/sh64/kernel/semaphore.c')
-rw-r--r-- | arch/sh64/kernel/semaphore.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/arch/sh64/kernel/semaphore.c b/arch/sh64/kernel/semaphore.c new file mode 100644 index 000000000000..72c16533436e --- /dev/null +++ b/arch/sh64/kernel/semaphore.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /* | ||
2 | * Just taken from alpha implementation. | ||
3 | * This can't work well, perhaps. | ||
4 | */ | ||
5 | /* | ||
6 | * Generic semaphore code. Buyer beware. Do your own | ||
7 | * specific changes in <asm/semaphore-helper.h> | ||
8 | */ | ||
9 | |||
10 | #include <linux/errno.h> | ||
11 | #include <linux/rwsem.h> | ||
12 | #include <linux/sched.h> | ||
13 | #include <linux/wait.h> | ||
14 | #include <linux/init.h> | ||
15 | #include <asm/semaphore.h> | ||
16 | #include <asm/semaphore-helper.h> | ||
17 | |||
18 | spinlock_t semaphore_wake_lock; | ||
19 | |||
20 | /* | ||
21 | * Semaphores are implemented using a two-way counter: | ||
22 | * The "count" variable is decremented for each process | ||
23 | * that tries to sleep, while the "waking" variable is | ||
24 | * incremented when the "up()" code goes to wake up waiting | ||
25 | * processes. | ||
26 | * | ||
27 | * Notably, the inline "up()" and "down()" functions can | ||
28 | * efficiently test if they need to do any extra work (up | ||
29 | * needs to do something only if count was negative before | ||
30 | * the increment operation. | ||
31 | * | ||
32 | * waking_non_zero() (from asm/semaphore.h) must execute | ||
33 | * atomically. | ||
34 | * | ||
35 | * When __up() is called, the count was negative before | ||
36 | * incrementing it, and we need to wake up somebody. | ||
37 | * | ||
38 | * This routine adds one to the count of processes that need to | ||
39 | * wake up and exit. ALL waiting processes actually wake up but | ||
40 | * only the one that gets to the "waking" field first will gate | ||
41 | * through and acquire the semaphore. The others will go back | ||
42 | * to sleep. | ||
43 | * | ||
44 | * Note that these functions are only called when there is | ||
45 | * contention on the lock, and as such all this is the | ||
46 | * "non-critical" part of the whole semaphore business. The | ||
47 | * critical part is the inline stuff in <asm/semaphore.h> | ||
48 | * where we want to avoid any extra jumps and calls. | ||
49 | */ | ||
50 | void __up(struct semaphore *sem) | ||
51 | { | ||
52 | wake_one_more(sem); | ||
53 | wake_up(&sem->wait); | ||
54 | } | ||
55 | |||
56 | /* | ||
57 | * Perform the "down" function. Return zero for semaphore acquired, | ||
58 | * return negative for signalled out of the function. | ||
59 | * | ||
60 | * If called from __down, the return is ignored and the wait loop is | ||
61 | * not interruptible. This means that a task waiting on a semaphore | ||
62 | * using "down()" cannot be killed until someone does an "up()" on | ||
63 | * the semaphore. | ||
64 | * | ||
65 | * If called from __down_interruptible, the return value gets checked | ||
66 | * upon return. If the return value is negative then the task continues | ||
67 | * with the negative value in the return register (it can be tested by | ||
68 | * the caller). | ||
69 | * | ||
70 | * Either form may be used in conjunction with "up()". | ||
71 | * | ||
72 | */ | ||
73 | |||
74 | #define DOWN_VAR \ | ||
75 | struct task_struct *tsk = current; \ | ||
76 | wait_queue_t wait; \ | ||
77 | init_waitqueue_entry(&wait, tsk); | ||
78 | |||
79 | #define DOWN_HEAD(task_state) \ | ||
80 | \ | ||
81 | \ | ||
82 | tsk->state = (task_state); \ | ||
83 | add_wait_queue(&sem->wait, &wait); \ | ||
84 | \ | ||
85 | /* \ | ||
86 | * Ok, we're set up. sem->count is known to be less than zero \ | ||
87 | * so we must wait. \ | ||
88 | * \ | ||
89 | * We can let go the lock for purposes of waiting. \ | ||
90 | * We re-acquire it after awaking so as to protect \ | ||
91 | * all semaphore operations. \ | ||
92 | * \ | ||
93 | * If "up()" is called before we call waking_non_zero() then \ | ||
94 | * we will catch it right away. If it is called later then \ | ||
95 | * we will have to go through a wakeup cycle to catch it. \ | ||
96 | * \ | ||
97 | * Multiple waiters contend for the semaphore lock to see \ | ||
98 | * who gets to gate through and who has to wait some more. \ | ||
99 | */ \ | ||
100 | for (;;) { | ||
101 | |||
102 | #define DOWN_TAIL(task_state) \ | ||
103 | tsk->state = (task_state); \ | ||
104 | } \ | ||
105 | tsk->state = TASK_RUNNING; \ | ||
106 | remove_wait_queue(&sem->wait, &wait); | ||
107 | |||
108 | void __sched __down(struct semaphore * sem) | ||
109 | { | ||
110 | DOWN_VAR | ||
111 | DOWN_HEAD(TASK_UNINTERRUPTIBLE) | ||
112 | if (waking_non_zero(sem)) | ||
113 | break; | ||
114 | schedule(); | ||
115 | DOWN_TAIL(TASK_UNINTERRUPTIBLE) | ||
116 | } | ||
117 | |||
118 | int __sched __down_interruptible(struct semaphore * sem) | ||
119 | { | ||
120 | int ret = 0; | ||
121 | DOWN_VAR | ||
122 | DOWN_HEAD(TASK_INTERRUPTIBLE) | ||
123 | |||
124 | ret = waking_non_zero_interruptible(sem, tsk); | ||
125 | if (ret) | ||
126 | { | ||
127 | if (ret == 1) | ||
128 | /* ret != 0 only if we get interrupted -arca */ | ||
129 | ret = 0; | ||
130 | break; | ||
131 | } | ||
132 | schedule(); | ||
133 | DOWN_TAIL(TASK_INTERRUPTIBLE) | ||
134 | return ret; | ||
135 | } | ||
136 | |||
137 | int __down_trylock(struct semaphore * sem) | ||
138 | { | ||
139 | return waking_non_zero_trylock(sem); | ||
140 | } | ||