]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/frv/kernel/semaphore.c
Linux-2.6.12-rc2
[mirror_ubuntu-artful-kernel.git] / arch / frv / kernel / semaphore.c
1 /* semaphore.c: FR-V semaphores
2 *
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
6 *
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.
11 */
12
13 #include <linux/config.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <asm/semaphore.h>
17
18 struct sem_waiter {
19 struct list_head list;
20 struct task_struct *task;
21 };
22
23 #if SEM_DEBUG
24 void semtrace(struct semaphore *sem, const char *str)
25 {
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);
32 }
33 #else
34 #define semtrace(SEM,STR) do { } while(0)
35 #endif
36
37 /*
38 * wait for a token to be granted from a semaphore
39 * - entered with lock held and interrupts disabled
40 */
41 void __down(struct semaphore *sem, unsigned long flags)
42 {
43 struct task_struct *tsk = current;
44 struct sem_waiter waiter;
45
46 semtrace(sem, "Entering __down");
47
48 /* set up my own style of waitqueue */
49 waiter.task = tsk;
50 get_task_struct(tsk);
51
52 list_add_tail(&waiter.list, &sem->wait_list);
53
54 /* we don't need to touch the semaphore struct anymore */
55 spin_unlock_irqrestore(&sem->wait_lock, flags);
56
57 /* wait to be given the semaphore */
58 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
59
60 for (;;) {
61 if (list_empty(&waiter.list))
62 break;
63 schedule();
64 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
65 }
66
67 tsk->state = TASK_RUNNING;
68 semtrace(sem, "Leaving __down");
69 }
70
71 EXPORT_SYMBOL(__down);
72
73 /*
74 * interruptibly wait for a token to be granted from a semaphore
75 * - entered with lock held and interrupts disabled
76 */
77 int __down_interruptible(struct semaphore *sem, unsigned long flags)
78 {
79 struct task_struct *tsk = current;
80 struct sem_waiter waiter;
81 int ret;
82
83 semtrace(sem,"Entering __down_interruptible");
84
85 /* set up my own style of waitqueue */
86 waiter.task = tsk;
87 get_task_struct(tsk);
88
89 list_add_tail(&waiter.list, &sem->wait_list);
90
91 /* we don't need to touch the semaphore struct anymore */
92 set_task_state(tsk, TASK_INTERRUPTIBLE);
93
94 spin_unlock_irqrestore(&sem->wait_lock, flags);
95
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);
105 }
106
107 out:
108 tsk->state = TASK_RUNNING;
109 semtrace(sem, "Leaving __down_interruptible");
110 return ret;
111
112 interrupted:
113 spin_lock_irqsave(&sem->wait_lock, flags);
114
115 if (!list_empty(&waiter.list)) {
116 list_del(&waiter.list);
117 ret = -EINTR;
118 }
119
120 spin_unlock_irqrestore(&sem->wait_lock, flags);
121 if (ret == -EINTR)
122 put_task_struct(current);
123 goto out;
124 }
125
126 EXPORT_SYMBOL(__down_interruptible);
127
128 /*
129 * release a single token back to a semaphore
130 * - entered with lock held and interrupts disabled
131 */
132 void __up(struct semaphore *sem)
133 {
134 struct task_struct *tsk;
135 struct sem_waiter *waiter;
136
137 semtrace(sem,"Entering __up");
138
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);
141
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).
145 */
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);
152
153 semtrace(sem,"Leaving __up");
154 }
155
156 EXPORT_SYMBOL(__up);