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/sched.h>
14 #include <linux/module.h>
15 #include <asm/semaphore.h>
18 struct list_head list
;
19 struct task_struct
*task
;
22 #ifdef CONFIG_DEBUG_SEMAPHORE
23 void semtrace(struct semaphore
*sem
, const char *str
)
26 printk("[%d] %s({%d,%d})\n",
30 list_empty(&sem
->wait_list
) ? 0 : 1);
33 #define semtrace(SEM,STR) do { } while(0)
37 * wait for a token to be granted from a semaphore
38 * - entered with lock held and interrupts disabled
40 void __down(struct semaphore
*sem
, unsigned long flags
)
42 struct task_struct
*tsk
= current
;
43 struct sem_waiter waiter
;
45 semtrace(sem
, "Entering __down");
47 /* set up my own style of waitqueue */
51 list_add_tail(&waiter
.list
, &sem
->wait_list
);
53 /* we don't need to touch the semaphore struct anymore */
54 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
56 /* wait to be given the semaphore */
57 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
60 if (list_empty(&waiter
.list
))
63 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
66 tsk
->state
= TASK_RUNNING
;
67 semtrace(sem
, "Leaving __down");
70 EXPORT_SYMBOL(__down
);
73 * interruptibly wait for a token to be granted from a semaphore
74 * - entered with lock held and interrupts disabled
76 int __down_interruptible(struct semaphore
*sem
, unsigned long flags
)
78 struct task_struct
*tsk
= current
;
79 struct sem_waiter waiter
;
82 semtrace(sem
,"Entering __down_interruptible");
84 /* set up my own style of waitqueue */
88 list_add_tail(&waiter
.list
, &sem
->wait_list
);
90 /* we don't need to touch the semaphore struct anymore */
91 set_task_state(tsk
, TASK_INTERRUPTIBLE
);
93 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
95 /* wait to be given the semaphore */
98 if (list_empty(&waiter
.list
))
100 if (unlikely(signal_pending(current
)))
103 set_task_state(tsk
, TASK_INTERRUPTIBLE
);
107 tsk
->state
= TASK_RUNNING
;
108 semtrace(sem
, "Leaving __down_interruptible");
112 spin_lock_irqsave(&sem
->wait_lock
, flags
);
114 if (!list_empty(&waiter
.list
)) {
115 list_del(&waiter
.list
);
119 spin_unlock_irqrestore(&sem
->wait_lock
, flags
);
121 put_task_struct(current
);
125 EXPORT_SYMBOL(__down_interruptible
);
128 * release a single token back to a semaphore
129 * - entered with lock held and interrupts disabled
131 void __up(struct semaphore
*sem
)
133 struct task_struct
*tsk
;
134 struct sem_waiter
*waiter
;
136 semtrace(sem
,"Entering __up");
138 /* grant the token to the process at the front of the queue */
139 waiter
= list_entry(sem
->wait_list
.next
, struct sem_waiter
, list
);
141 /* We must be careful not to touch 'waiter' after we set ->task = NULL.
142 * It is an allocated on the waiter's stack and may become invalid at
143 * any time after that point (due to a wakeup from another source).
145 list_del_init(&waiter
->list
);
149 wake_up_process(tsk
);
150 put_task_struct(tsk
);
152 semtrace(sem
,"Leaving __up");