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>
19 struct list_head list;
20 struct task_struct *task;
24 void semtrace(struct semaphore *sem, const char *str)
27 printk("[%d] %s({%d,%d})\n",
31 list_empty(&sem->wait_list) ? 0 : 1);
34 #define semtrace(SEM,STR) do { } while(0)
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 */
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);
61 if (list_empty(&waiter.list))
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;
83 semtrace(sem,"Entering __down_interruptible");
85 /* set up my own style of waitqueue */
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 */
99 if (list_empty(&waiter.list))
101 if (unlikely(signal_pending(current)))
104 set_task_state(tsk, TASK_INTERRUPTIBLE);
108 tsk->state = TASK_RUNNING;
109 semtrace(sem, "Leaving __down_interruptible");
113 spin_lock_irqsave(&sem->wait_lock, flags);
115 if (!list_empty(&waiter.list)) {
116 list_del(&waiter.list);
120 spin_unlock_irqrestore(&sem->wait_lock, flags);
122 put_task_struct(current);
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);
150 wake_up_process(tsk);
151 put_task_struct(tsk);
153 semtrace(sem,"Leaving __up");