Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / frv / kernel / semaphore.c
blob5cba9c1f2b3dcf2c01579528f878f44ce88fbc90
1 /* semaphore.c: FR-V semaphores
3 * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from lib/rwsem-spinlock.c
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/config.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <asm/semaphore.h>
18 struct sem_waiter {
19 struct list_head list;
20 struct task_struct *task;
23 #if SEM_DEBUG
24 void semtrace(struct semaphore *sem, const char *str)
26 if (sem->debug)
27 printk("[%d] %s({%d,%d})\n",
28 current->pid,
29 str,
30 sem->counter,
31 list_empty(&sem->wait_list) ? 0 : 1);
33 #else
34 #define semtrace(SEM,STR) do { } while(0)
35 #endif
38 * wait for a token to be granted from a semaphore
39 * - entered with lock held and interrupts disabled
41 void __down(struct semaphore *sem, unsigned long flags)
43 struct task_struct *tsk = current;
44 struct sem_waiter waiter;
46 semtrace(sem, "Entering __down");
48 /* set up my own style of waitqueue */
49 waiter.task = tsk;
50 get_task_struct(tsk);
52 list_add_tail(&waiter.list, &sem->wait_list);
54 /* we don't need to touch the semaphore struct anymore */
55 spin_unlock_irqrestore(&sem->wait_lock, flags);
57 /* wait to be given the semaphore */
58 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
60 for (;;) {
61 if (list_empty(&waiter.list))
62 break;
63 schedule();
64 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
67 tsk->state = TASK_RUNNING;
68 semtrace(sem, "Leaving __down");
71 EXPORT_SYMBOL(__down);
74 * interruptibly wait for a token to be granted from a semaphore
75 * - entered with lock held and interrupts disabled
77 int __down_interruptible(struct semaphore *sem, unsigned long flags)
79 struct task_struct *tsk = current;
80 struct sem_waiter waiter;
81 int ret;
83 semtrace(sem,"Entering __down_interruptible");
85 /* set up my own style of waitqueue */
86 waiter.task = tsk;
87 get_task_struct(tsk);
89 list_add_tail(&waiter.list, &sem->wait_list);
91 /* we don't need to touch the semaphore struct anymore */
92 set_task_state(tsk, TASK_INTERRUPTIBLE);
94 spin_unlock_irqrestore(&sem->wait_lock, flags);
96 /* wait to be given the semaphore */
97 ret = 0;
98 for (;;) {
99 if (list_empty(&waiter.list))
100 break;
101 if (unlikely(signal_pending(current)))
102 goto interrupted;
103 schedule();
104 set_task_state(tsk, TASK_INTERRUPTIBLE);
107 out:
108 tsk->state = TASK_RUNNING;
109 semtrace(sem, "Leaving __down_interruptible");
110 return ret;
112 interrupted:
113 spin_lock_irqsave(&sem->wait_lock, flags);
115 if (!list_empty(&waiter.list)) {
116 list_del(&waiter.list);
117 ret = -EINTR;
120 spin_unlock_irqrestore(&sem->wait_lock, flags);
121 if (ret == -EINTR)
122 put_task_struct(current);
123 goto out;
126 EXPORT_SYMBOL(__down_interruptible);
129 * release a single token back to a semaphore
130 * - entered with lock held and interrupts disabled
132 void __up(struct semaphore *sem)
134 struct task_struct *tsk;
135 struct sem_waiter *waiter;
137 semtrace(sem,"Entering __up");
139 /* grant the token to the process at the front of the queue */
140 waiter = list_entry(sem->wait_list.next, struct sem_waiter, list);
142 /* We must be careful not to touch 'waiter' after we set ->task = NULL.
143 * It is an allocated on the waiter's stack and may become invalid at
144 * any time after that point (due to a wakeup from another source).
146 list_del_init(&waiter->list);
147 tsk = waiter->task;
148 mb();
149 waiter->task = NULL;
150 wake_up_process(tsk);
151 put_task_struct(tsk);
153 semtrace(sem,"Leaving __up");
156 EXPORT_SYMBOL(__up);