2 * linux/arch/s390/kernel/semaphore.c
5 * Copyright (C) 1998-2000 IBM Corporation
6 * Author(s): Martin Schwidefsky
8 * Derived from "linux/arch/i386/kernel/semaphore.c
9 * Copyright (C) 1999, Linus Torvalds
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
16 #include <asm/semaphore.h>
19 * Atomically update sem->count. Equivalent to:
20 * old_val = sem->count.counter;
21 * new_val = ((old_val >= 0) ? old_val : 0) + incr;
22 * sem->count.counter = new_val;
25 static inline int __sem_update_count(struct semaphore
*sem
, int incr
)
29 __asm__
__volatile__(" l %0,0(%3)\n"
36 : "=&d" (old_val
), "=&d" (new_val
),
38 : "a" (&sem
->count
), "d" (incr
), "m" (sem
->count
)
44 * The inline function up() incremented count but the result
45 * was <= 0. This indicates that some process is waiting on
46 * the semaphore. The semaphore is free and we'll wake the
47 * first sleeping process, so we set count to 1 unless some
48 * other cpu has called up in the meantime in which case
49 * we just increment count by 1.
51 void __up(struct semaphore
*sem
)
53 __sem_update_count(sem
, 1);
58 * The inline function down() decremented count and the result
59 * was < 0. The wait loop will atomically test and update the
60 * semaphore counter following the rules:
61 * count > 0: decrement count, wake up queue and exit.
62 * count <= 0: set count to -1, go to sleep.
64 void __sched
__down(struct semaphore
* sem
)
66 struct task_struct
*tsk
= current
;
67 DECLARE_WAITQUEUE(wait
, tsk
);
69 __set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
70 add_wait_queue_exclusive(&sem
->wait
, &wait
);
71 while (__sem_update_count(sem
, -1) <= 0) {
73 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
75 remove_wait_queue(&sem
->wait
, &wait
);
76 __set_task_state(tsk
, TASK_RUNNING
);
81 * Same as __down() with an additional test for signals.
82 * If a signal is pending the count is updated as follows:
83 * count > 0: wake up queue and exit.
84 * count <= 0: set count to 0, wake up queue and exit.
86 int __sched
__down_interruptible(struct semaphore
* sem
)
89 struct task_struct
*tsk
= current
;
90 DECLARE_WAITQUEUE(wait
, tsk
);
92 __set_task_state(tsk
, TASK_INTERRUPTIBLE
);
93 add_wait_queue_exclusive(&sem
->wait
, &wait
);
94 while (__sem_update_count(sem
, -1) <= 0) {
95 if (signal_pending(current
)) {
96 __sem_update_count(sem
, 0);
101 set_task_state(tsk
, TASK_INTERRUPTIBLE
);
103 remove_wait_queue(&sem
->wait
, &wait
);
104 __set_task_state(tsk
, TASK_RUNNING
);