PageRenderTime 28ms CodeModel.GetById 8ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/ppc/kernel/semaphore.c

https://bitbucket.org/evzijst/gittest
C | 131 lines | 63 code | 12 blank | 56 comment | 3 complexity | 06017a2615d67449c0284c16a54007f4 MD5 | raw file
  1/*
  2 * PowerPC-specific semaphore code.
  3 *
  4 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version
  9 * 2 of the License, or (at your option) any later version.
 10 *
 11 * April 2001 - Reworked by Paul Mackerras <paulus@samba.org>
 12 * to eliminate the SMP races in the old version between the updates
 13 * of `count' and `waking'.  Now we use negative `count' values to
 14 * indicate that some process(es) are waiting for the semaphore.
 15 */
 16
 17#include <linux/sched.h>
 18#include <linux/init.h>
 19#include <asm/atomic.h>
 20#include <asm/semaphore.h>
 21#include <asm/errno.h>
 22
 23/*
 24 * Atomically update sem->count.
 25 * This does the equivalent of the following:
 26 *
 27 *	old_count = sem->count;
 28 *	tmp = MAX(old_count, 0) + incr;
 29 *	sem->count = tmp;
 30 *	return old_count;
 31 */
 32static inline int __sem_update_count(struct semaphore *sem, int incr)
 33{
 34	int old_count, tmp;
 35
 36	__asm__ __volatile__("\n"
 37"1:	lwarx	%0,0,%3\n"
 38"	srawi	%1,%0,31\n"
 39"	andc	%1,%0,%1\n"
 40"	add	%1,%1,%4\n"
 41	PPC405_ERR77(0,%3)
 42"	stwcx.	%1,0,%3\n"
 43"	bne	1b"
 44	: "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
 45	: "r" (&sem->count), "r" (incr), "m" (sem->count)
 46	: "cc");
 47
 48	return old_count;
 49}
 50
 51void __up(struct semaphore *sem)
 52{
 53	/*
 54	 * Note that we incremented count in up() before we came here,
 55	 * but that was ineffective since the result was <= 0, and
 56	 * any negative value of count is equivalent to 0.
 57	 * This ends up setting count to 1, unless count is now > 0
 58	 * (i.e. because some other cpu has called up() in the meantime),
 59	 * in which case we just increment count.
 60	 */
 61	__sem_update_count(sem, 1);
 62	wake_up(&sem->wait);
 63}
 64
 65/*
 66 * Note that when we come in to __down or __down_interruptible,
 67 * we have already decremented count, but that decrement was
 68 * ineffective since the result was < 0, and any negative value
 69 * of count is equivalent to 0.
 70 * Thus it is only when we decrement count from some value > 0
 71 * that we have actually got the semaphore.
 72 */
 73void __sched __down(struct semaphore *sem)
 74{
 75	struct task_struct *tsk = current;
 76	DECLARE_WAITQUEUE(wait, tsk);
 77
 78	tsk->state = TASK_UNINTERRUPTIBLE;
 79	add_wait_queue_exclusive(&sem->wait, &wait);
 80	smp_wmb();
 81
 82	/*
 83	 * Try to get the semaphore.  If the count is > 0, then we've
 84	 * got the semaphore; we decrement count and exit the loop.
 85	 * If the count is 0 or negative, we set it to -1, indicating
 86	 * that we are asleep, and then sleep.
 87	 */
 88	while (__sem_update_count(sem, -1) <= 0) {
 89		schedule();
 90		tsk->state = TASK_UNINTERRUPTIBLE;
 91	}
 92	remove_wait_queue(&sem->wait, &wait);
 93	tsk->state = TASK_RUNNING;
 94
 95	/*
 96	 * If there are any more sleepers, wake one of them up so
 97	 * that it can either get the semaphore, or set count to -1
 98	 * indicating that there are still processes sleeping.
 99	 */
100	wake_up(&sem->wait);
101}
102
103int __sched __down_interruptible(struct semaphore * sem)
104{
105	int retval = 0;
106	struct task_struct *tsk = current;
107	DECLARE_WAITQUEUE(wait, tsk);
108
109	tsk->state = TASK_INTERRUPTIBLE;
110	add_wait_queue_exclusive(&sem->wait, &wait);
111	smp_wmb();
112
113	while (__sem_update_count(sem, -1) <= 0) {
114		if (signal_pending(current)) {
115			/*
116			 * A signal is pending - give up trying.
117			 * Set sem->count to 0 if it is negative,
118			 * since we are no longer sleeping.
119			 */
120			__sem_update_count(sem, 0);
121			retval = -EINTR;
122			break;
123		}
124		schedule();
125		tsk->state = TASK_INTERRUPTIBLE;
126	}
127	tsk->state = TASK_RUNNING;
128	remove_wait_queue(&sem->wait, &wait);
129	wake_up(&sem->wait);
130	return retval;
131}