2008-03-08 09:55:58 +07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Intel Corporation
|
|
|
|
* Author: Matthew Wilcox <willy@linux.intel.com>
|
|
|
|
*
|
|
|
|
* Distributed under the terms of the GNU GPL, version 2
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/compiler.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/semaphore.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some notes on the implementation:
|
|
|
|
*
|
|
|
|
* down_trylock() and up() can be called from interrupt context.
|
|
|
|
* So we have to disable interrupts when taking the lock.
|
|
|
|
*
|
2008-03-15 01:35:22 +07:00
|
|
|
* The ->count variable defines how many more tasks can acquire the
|
|
|
|
* semaphore. If it's zero, there may be tasks waiting on the list.
|
2008-03-08 09:55:58 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
static noinline void __down(struct semaphore *sem);
|
|
|
|
static noinline int __down_interruptible(struct semaphore *sem);
|
2008-03-15 00:19:33 +07:00
|
|
|
static noinline int __down_killable(struct semaphore *sem);
|
2008-03-15 00:43:13 +07:00
|
|
|
static noinline int __down_timeout(struct semaphore *sem, long jiffies);
|
2008-03-08 09:55:58 +07:00
|
|
|
static noinline void __up(struct semaphore *sem);
|
|
|
|
|
|
|
|
void down(struct semaphore *sem)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&sem->lock, flags);
|
2008-03-15 01:35:22 +07:00
|
|
|
if (likely(sem->count > 0))
|
|
|
|
sem->count--;
|
|
|
|
else
|
2008-03-08 09:55:58 +07:00
|
|
|
__down(sem);
|
|
|
|
spin_unlock_irqrestore(&sem->lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(down);
|
|
|
|
|
|
|
|
int down_interruptible(struct semaphore *sem)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&sem->lock, flags);
|
2008-03-15 01:35:22 +07:00
|
|
|
if (likely(sem->count > 0))
|
|
|
|
sem->count--;
|
|
|
|
else
|
2008-03-08 09:55:58 +07:00
|
|
|
result = __down_interruptible(sem);
|
|
|
|
spin_unlock_irqrestore(&sem->lock, flags);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(down_interruptible);
|
|
|
|
|
2008-03-15 00:19:33 +07:00
|
|
|
int down_killable(struct semaphore *sem)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&sem->lock, flags);
|
2008-03-15 01:35:22 +07:00
|
|
|
if (likely(sem->count > 0))
|
|
|
|
sem->count--;
|
|
|
|
else
|
2008-03-15 00:19:33 +07:00
|
|
|
result = __down_killable(sem);
|
|
|
|
spin_unlock_irqrestore(&sem->lock, flags);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(down_killable);
|
|
|
|
|
2008-03-08 09:55:58 +07:00
|
|
|
/**
|
|
|
|
* down_trylock - try to acquire the semaphore, without waiting
|
|
|
|
* @sem: the semaphore to be acquired
|
|
|
|
*
|
|
|
|
* Try to acquire the semaphore atomically. Returns 0 if the mutex has
|
|
|
|
* been acquired successfully and 1 if it is contended.
|
|
|
|
*
|
|
|
|
* NOTE: This return value is inverted from both spin_trylock and
|
|
|
|
* mutex_trylock! Be careful about this when converting code.
|
|
|
|
*
|
|
|
|
* Unlike mutex_trylock, this function can be used from interrupt context,
|
|
|
|
* and the semaphore can be released by any task or interrupt.
|
|
|
|
*/
|
|
|
|
int down_trylock(struct semaphore *sem)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&sem->lock, flags);
|
|
|
|
count = sem->count - 1;
|
|
|
|
if (likely(count >= 0))
|
|
|
|
sem->count = count;
|
|
|
|
spin_unlock_irqrestore(&sem->lock, flags);
|
|
|
|
|
|
|
|
return (count < 0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(down_trylock);
|
|
|
|
|
2008-03-15 00:43:13 +07:00
|
|
|
int down_timeout(struct semaphore *sem, long jiffies)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&sem->lock, flags);
|
2008-03-15 01:35:22 +07:00
|
|
|
if (likely(sem->count > 0))
|
|
|
|
sem->count--;
|
|
|
|
else
|
2008-03-15 00:43:13 +07:00
|
|
|
result = __down_timeout(sem, jiffies);
|
|
|
|
spin_unlock_irqrestore(&sem->lock, flags);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(down_timeout);
|
|
|
|
|
2008-03-08 09:55:58 +07:00
|
|
|
void up(struct semaphore *sem)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&sem->lock, flags);
|
2008-03-15 01:35:22 +07:00
|
|
|
if (likely(list_empty(&sem->wait_list)))
|
2008-03-08 09:55:58 +07:00
|
|
|
sem->count++;
|
|
|
|
else
|
|
|
|
__up(sem);
|
|
|
|
spin_unlock_irqrestore(&sem->lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(up);
|
|
|
|
|
|
|
|
/* Functions for the contended case */
|
|
|
|
|
|
|
|
struct semaphore_waiter {
|
|
|
|
struct list_head list;
|
|
|
|
struct task_struct *task;
|
|
|
|
int up;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2008-03-15 00:43:13 +07:00
|
|
|
* Because this function is inlined, the 'state' parameter will be
|
|
|
|
* constant, and thus optimised away by the compiler. Likewise the
|
|
|
|
* 'timeout' parameter for the cases without timeouts.
|
2008-03-08 09:55:58 +07:00
|
|
|
*/
|
2008-03-15 00:43:13 +07:00
|
|
|
static inline int __sched __down_common(struct semaphore *sem, long state,
|
|
|
|
long timeout)
|
2008-03-08 09:55:58 +07:00
|
|
|
{
|
|
|
|
struct task_struct *task = current;
|
|
|
|
struct semaphore_waiter waiter;
|
|
|
|
|
|
|
|
list_add_tail(&waiter.list, &sem->wait_list);
|
|
|
|
waiter.task = task;
|
|
|
|
waiter.up = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (state == TASK_INTERRUPTIBLE && signal_pending(task))
|
|
|
|
goto interrupted;
|
2008-03-15 00:19:33 +07:00
|
|
|
if (state == TASK_KILLABLE && fatal_signal_pending(task))
|
|
|
|
goto interrupted;
|
2008-03-15 00:43:13 +07:00
|
|
|
if (timeout <= 0)
|
|
|
|
goto timed_out;
|
2008-03-08 09:55:58 +07:00
|
|
|
__set_task_state(task, state);
|
|
|
|
spin_unlock_irq(&sem->lock);
|
2008-03-15 00:43:13 +07:00
|
|
|
timeout = schedule_timeout(timeout);
|
2008-03-08 09:55:58 +07:00
|
|
|
spin_lock_irq(&sem->lock);
|
|
|
|
if (waiter.up)
|
2008-03-15 01:35:22 +07:00
|
|
|
return 0;
|
2008-03-08 09:55:58 +07:00
|
|
|
}
|
|
|
|
|
2008-03-15 00:43:13 +07:00
|
|
|
timed_out:
|
|
|
|
list_del(&waiter.list);
|
2008-03-15 01:35:22 +07:00
|
|
|
return -ETIME;
|
|
|
|
|
2008-03-08 09:55:58 +07:00
|
|
|
interrupted:
|
|
|
|
list_del(&waiter.list);
|
2008-03-15 01:35:22 +07:00
|
|
|
return -EINTR;
|
2008-03-08 09:55:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static noinline void __sched __down(struct semaphore *sem)
|
|
|
|
{
|
2008-03-15 00:43:13 +07:00
|
|
|
__down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
|
2008-03-08 09:55:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static noinline int __sched __down_interruptible(struct semaphore *sem)
|
|
|
|
{
|
2008-03-15 00:43:13 +07:00
|
|
|
return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
|
2008-03-08 09:55:58 +07:00
|
|
|
}
|
|
|
|
|
2008-03-15 00:19:33 +07:00
|
|
|
static noinline int __sched __down_killable(struct semaphore *sem)
|
|
|
|
{
|
2008-03-15 00:43:13 +07:00
|
|
|
return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static noinline int __sched __down_timeout(struct semaphore *sem, long jiffies)
|
|
|
|
{
|
|
|
|
return __down_common(sem, TASK_UNINTERRUPTIBLE, jiffies);
|
2008-03-15 00:19:33 +07:00
|
|
|
}
|
|
|
|
|
2008-03-08 09:55:58 +07:00
|
|
|
static noinline void __sched __up(struct semaphore *sem)
|
|
|
|
{
|
2008-03-15 01:35:22 +07:00
|
|
|
struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
|
|
|
|
struct semaphore_waiter, list);
|
|
|
|
list_del(&waiter->list);
|
|
|
|
waiter->up = 1;
|
|
|
|
wake_up_process(waiter->task);
|
2008-03-08 09:55:58 +07:00
|
|
|
}
|