| 1 | /* |
| 2 | * arch/ubicom32/kernel/semaphore.c |
| 3 | * Ubicom32 architecture semaphore implementation. |
| 4 | * |
| 5 | * (C) Copyright 2009, Ubicom, Inc. |
| 6 | * |
| 7 | * This file is part of the Ubicom32 Linux Kernel Port. |
| 8 | * |
| 9 | * The Ubicom32 Linux Kernel Port is free software: you can redistribute |
| 10 | * it and/or modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation, either version 2 of the |
| 12 | * License, or (at your option) any later version. |
| 13 | * |
| 14 | * The Ubicom32 Linux Kernel Port is distributed in the hope that it |
| 15 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 16 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with the Ubicom32 Linux Kernel Port. If not, |
| 21 | * see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | * Ubicom32 implementation derived from (with many thanks): |
| 24 | * arch/m68knommu |
| 25 | * arch/blackfin |
| 26 | * arch/parisc |
| 27 | */ |
| 28 | /* |
| 29 | * Generic semaphore code. Buyer beware. Do your own |
| 30 | * specific changes in <asm/semaphore-helper.h> |
| 31 | */ |
| 32 | |
| 33 | #include <linux/sched.h> |
| 34 | #include <linux/err.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <asm/semaphore-helper.h> |
| 37 | |
| 38 | #ifndef CONFIG_RMW_INSNS |
| 39 | spinlock_t semaphore_wake_lock; |
| 40 | #endif |
| 41 | |
| 42 | /* |
| 43 | * Semaphores are implemented using a two-way counter: |
| 44 | * The "count" variable is decremented for each process |
| 45 | * that tries to sleep, while the "waking" variable is |
| 46 | * incremented when the "up()" code goes to wake up waiting |
| 47 | * processes. |
| 48 | * |
| 49 | * Notably, the inline "up()" and "down()" functions can |
| 50 | * efficiently test if they need to do any extra work (up |
| 51 | * needs to do something only if count was negative before |
| 52 | * the increment operation. |
| 53 | * |
| 54 | * waking_non_zero() (from asm/semaphore.h) must execute |
| 55 | * atomically. |
| 56 | * |
| 57 | * When __up() is called, the count was negative before |
| 58 | * incrementing it, and we need to wake up somebody. |
| 59 | * |
| 60 | * This routine adds one to the count of processes that need to |
| 61 | * wake up and exit. ALL waiting processes actually wake up but |
| 62 | * only the one that gets to the "waking" field first will gate |
| 63 | * through and acquire the semaphore. The others will go back |
| 64 | * to sleep. |
| 65 | * |
| 66 | * Note that these functions are only called when there is |
| 67 | * contention on the lock, and as such all this is the |
| 68 | * "non-critical" part of the whole semaphore business. The |
| 69 | * critical part is the inline stuff in <asm/semaphore.h> |
| 70 | * where we want to avoid any extra jumps and calls. |
| 71 | */ |
| 72 | void __up(struct semaphore *sem) |
| 73 | { |
| 74 | wake_one_more(sem); |
| 75 | wake_up(&sem->wait); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Perform the "down" function. Return zero for semaphore acquired, |
| 80 | * return negative for signalled out of the function. |
| 81 | * |
| 82 | * If called from __down, the return is ignored and the wait loop is |
| 83 | * not interruptible. This means that a task waiting on a semaphore |
| 84 | * using "down()" cannot be killed until someone does an "up()" on |
| 85 | * the semaphore. |
| 86 | * |
| 87 | * If called from __down_interruptible, the return value gets checked |
| 88 | * upon return. If the return value is negative then the task continues |
| 89 | * with the negative value in the return register (it can be tested by |
| 90 | * the caller). |
| 91 | * |
| 92 | * Either form may be used in conjunction with "up()". |
| 93 | * |
| 94 | */ |
| 95 | |
| 96 | |
| 97 | #define DOWN_HEAD(task_state) \ |
| 98 | \ |
| 99 | \ |
| 100 | current->state = (task_state); \ |
| 101 | add_wait_queue(&sem->wait, &wait); \ |
| 102 | \ |
| 103 | /* \ |
| 104 | * Ok, we're set up. sem->count is known to be less than zero \ |
| 105 | * so we must wait. \ |
| 106 | * \ |
| 107 | * We can let go the lock for purposes of waiting. \ |
| 108 | * We re-acquire it after awaking so as to protect \ |
| 109 | * all semaphore operations. \ |
| 110 | * \ |
| 111 | * If "up()" is called before we call waking_non_zero() then \ |
| 112 | * we will catch it right away. If it is called later then \ |
| 113 | * we will have to go through a wakeup cycle to catch it. \ |
| 114 | * \ |
| 115 | * Multiple waiters contend for the semaphore lock to see \ |
| 116 | * who gets to gate through and who has to wait some more. \ |
| 117 | */ \ |
| 118 | for (;;) { |
| 119 | |
| 120 | #define DOWN_TAIL(task_state) \ |
| 121 | current->state = (task_state); \ |
| 122 | } \ |
| 123 | current->state = TASK_RUNNING; \ |
| 124 | remove_wait_queue(&sem->wait, &wait); |
| 125 | |
| 126 | void __sched __down(struct semaphore *sem) |
| 127 | { |
| 128 | DECLARE_WAITQUEUE(wait, current); |
| 129 | |
| 130 | DOWN_HEAD(TASK_UNINTERRUPTIBLE) |
| 131 | if (waking_non_zero(sem)) |
| 132 | break; |
| 133 | schedule(); |
| 134 | DOWN_TAIL(TASK_UNINTERRUPTIBLE) |
| 135 | } |
| 136 | |
| 137 | int __sched __down_interruptible(struct semaphore *sem) |
| 138 | { |
| 139 | DECLARE_WAITQUEUE(wait, current); |
| 140 | int ret = 0; |
| 141 | |
| 142 | DOWN_HEAD(TASK_INTERRUPTIBLE) |
| 143 | |
| 144 | ret = waking_non_zero_interruptible(sem, current); |
| 145 | if (ret) { |
| 146 | if (ret == 1) |
| 147 | /* ret != 0 only if we get interrupted -arca */ |
| 148 | ret = 0; |
| 149 | break; |
| 150 | } |
| 151 | schedule(); |
| 152 | DOWN_TAIL(TASK_INTERRUPTIBLE) |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | int __down_trylock(struct semaphore *sem) |
| 157 | { |
| 158 | return waking_non_zero_trylock(sem); |
| 159 | } |
| 160 | |