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
;
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
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
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
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
)
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
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
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); \
99 * Ok, we're set up. sem->count is known to be less than zero \
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. \
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
))
129 DOWN_TAIL(TASK_UNINTERRUPTIBLE
)
132 int __sched
__down_interruptible(struct semaphore
* sem
)
134 DECLARE_WAITQUEUE(wait
, current
);
137 DOWN_HEAD(TASK_INTERRUPTIBLE
)
139 ret
= waking_non_zero_interruptible(sem
, current
);
143 /* ret != 0 only if we get interrupted -arca */
148 DOWN_TAIL(TASK_INTERRUPTIBLE
)
152 int __down_trylock(struct semaphore
* sem
)
154 return waking_non_zero_trylock(sem
);