MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / nios2nommu / kernel / semaphore.c
blob0c7d11bbd28f9a911365b717708b24f49bdbb56d
1 /*--------------------------------------------------------------------
3 * arch/nios2nommu/kernel/semaphore.c
5 * Derived from various works, Alpha, ix86, M68K, Sparc, ...et al
7 * Copyright (C) 2004 Microtronix Datacom Ltd
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 * Jan/20/2004 dgt NiosII
22 ---------------------------------------------------------------------*/
25 * Generic semaphore code. Buyer beware. Do your own
26 * specific changes in <asm/semaphore-helper.h>
29 #include <linux/sched.h>
30 #include <linux/err.h>
31 #include <asm/semaphore-helper.h>
33 #ifndef CONFIG_RMW_INSNS
34 spinlock_t semaphore_wake_lock;
35 #endif
38 * Semaphores are implemented using a two-way counter:
39 * The "count" variable is decremented for each process
40 * that tries to sleep, while the "waking" variable is
41 * incremented when the "up()" code goes to wake up waiting
42 * processes.
44 * Notably, the inline "up()" and "down()" functions can
45 * efficiently test if they need to do any extra work (up
46 * needs to do something only if count was negative before
47 * the increment operation.
49 * waking_non_zero() (from asm/semaphore.h) must execute
50 * atomically.
52 * When __up() is called, the count was negative before
53 * incrementing it, and we need to wake up somebody.
55 * This routine adds one to the count of processes that need to
56 * wake up and exit. ALL waiting processes actually wake up but
57 * only the one that gets to the "waking" field first will gate
58 * through and acquire the semaphore. The others will go back
59 * to sleep.
61 * Note that these functions are only called when there is
62 * contention on the lock, and as such all this is the
63 * "non-critical" part of the whole semaphore business. The
64 * critical part is the inline stuff in <asm/semaphore.h>
65 * where we want to avoid any extra jumps and calls.
67 asmlinkage void __up(struct semaphore *sem)
69 wake_one_more(sem);
70 wake_up(&sem->wait);
74 * Perform the "down" function. Return zero for semaphore acquired,
75 * return negative for signalled out of the function.
77 * If called from __down, the return is ignored and the wait loop is
78 * not interruptible. This means that a task waiting on a semaphore
79 * using "down()" cannot be killed until someone does an "up()" on
80 * the semaphore.
82 * If called from __down_interruptible, the return value gets checked
83 * upon return. If the return value is negative then the task continues
84 * with the negative value in the return register (it can be tested by
85 * the caller).
87 * Either form may be used in conjunction with "up()".
92 #define DOWN_HEAD(task_state) \
95 current->state = (task_state); \
96 add_wait_queue(&sem->wait, &wait); \
98 /* \
99 * Ok, we're set up. sem->count is known to be less than zero \
100 * so we must wait. \
102 * We can let go the lock for purposes of waiting. \
103 * We re-acquire it after awaking so as to protect \
104 * all semaphore operations. \
106 * If "up()" is called before we call waking_non_zero() then \
107 * we will catch it right away. If it is called later then \
108 * we will have to go through a wakeup cycle to catch it. \
110 * Multiple waiters contend for the semaphore lock to see \
111 * who gets to gate through and who has to wait some more. \
112 */ \
113 for (;;) {
115 #define DOWN_TAIL(task_state) \
116 current->state = (task_state); \
118 current->state = TASK_RUNNING; \
119 remove_wait_queue(&sem->wait, &wait);
121 void __sched __down(struct semaphore * sem)
123 DECLARE_WAITQUEUE(wait, current);
125 DOWN_HEAD(TASK_UNINTERRUPTIBLE)
126 if (waking_non_zero(sem))
127 break;
128 schedule();
129 DOWN_TAIL(TASK_UNINTERRUPTIBLE)
132 int __sched __down_interruptible(struct semaphore * sem)
134 DECLARE_WAITQUEUE(wait, current);
135 int ret = 0;
137 DOWN_HEAD(TASK_INTERRUPTIBLE)
139 ret = waking_non_zero_interruptible(sem, current);
140 if (ret)
142 if (ret == 1)
143 /* ret != 0 only if we get interrupted -arca */
144 ret = 0;
145 break;
147 schedule();
148 DOWN_TAIL(TASK_INTERRUPTIBLE)
149 return ret;
152 int __down_trylock(struct semaphore * sem)
154 return waking_non_zero_trylock(sem);