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