Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 96342
b: refs/heads/master
c: 00b41ec
h: refs/heads/master
v: v3
  • Loading branch information
Linus Torvalds committed May 11, 2008
1 parent 0a484bc commit 9d78960
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 39f004ba27fcd2431030a3bb3c949fa3f93fa4ca
refs/heads/master: 00b41ec2611dc98f87f30753ee00a53db648d662
64 changes: 34 additions & 30 deletions trunk/kernel/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ void down(struct semaphore *sem)
unsigned long flags;

spin_lock_irqsave(&sem->lock, flags);
if (unlikely(!sem->count))
if (likely(sem->count > 0))
sem->count--;
else
__down(sem);
sem->count--;
spin_unlock_irqrestore(&sem->lock, flags);
}
EXPORT_SYMBOL(down);
Expand All @@ -76,10 +77,10 @@ int down_interruptible(struct semaphore *sem)
int result = 0;

spin_lock_irqsave(&sem->lock, flags);
if (unlikely(!sem->count))
result = __down_interruptible(sem);
if (!result)
if (likely(sem->count > 0))
sem->count--;
else
result = __down_interruptible(sem);
spin_unlock_irqrestore(&sem->lock, flags);

return result;
Expand All @@ -102,10 +103,10 @@ int down_killable(struct semaphore *sem)
int result = 0;

spin_lock_irqsave(&sem->lock, flags);
if (unlikely(!sem->count))
result = __down_killable(sem);
if (!result)
if (likely(sem->count > 0))
sem->count--;
else
result = __down_killable(sem);
spin_unlock_irqrestore(&sem->lock, flags);

return result;
Expand Down Expand Up @@ -156,10 +157,10 @@ int down_timeout(struct semaphore *sem, long jiffies)
int result = 0;

spin_lock_irqsave(&sem->lock, flags);
if (unlikely(!sem->count))
result = __down_timeout(sem, jiffies);
if (!result)
if (likely(sem->count > 0))
sem->count--;
else
result = __down_timeout(sem, jiffies);
spin_unlock_irqrestore(&sem->lock, flags);

return result;
Expand All @@ -178,8 +179,9 @@ void up(struct semaphore *sem)
unsigned long flags;

spin_lock_irqsave(&sem->lock, flags);
sem->count++;
if (unlikely(!list_empty(&sem->wait_list)))
if (likely(list_empty(&sem->wait_list)))
sem->count++;
else
__up(sem);
spin_unlock_irqrestore(&sem->lock, flags);
}
Expand All @@ -190,6 +192,7 @@ EXPORT_SYMBOL(up);
struct semaphore_waiter {
struct list_head list;
struct task_struct *task;
int up;
};

/*
Expand All @@ -202,34 +205,33 @@ static inline int __sched __down_common(struct semaphore *sem, long state,
{
struct task_struct *task = current;
struct semaphore_waiter waiter;
int ret = 0;

waiter.task = task;
list_add_tail(&waiter.list, &sem->wait_list);
waiter.task = task;
waiter.up = 0;

for (;;) {
if (state == TASK_INTERRUPTIBLE && signal_pending(task)) {
ret = -EINTR;
break;
}
if (state == TASK_KILLABLE && fatal_signal_pending(task)) {
ret = -EINTR;
break;
}
if (timeout <= 0) {
ret = -ETIME;
break;
}
if (state == TASK_INTERRUPTIBLE && signal_pending(task))
goto interrupted;
if (state == TASK_KILLABLE && fatal_signal_pending(task))
goto interrupted;
if (timeout <= 0)
goto timed_out;
__set_task_state(task, state);
spin_unlock_irq(&sem->lock);
timeout = schedule_timeout(timeout);
spin_lock_irq(&sem->lock);
if (sem->count > 0)
break;
if (waiter.up)
return 0;
}

timed_out:
list_del(&waiter.list);
return -ETIME;

interrupted:
list_del(&waiter.list);
return ret;
return -EINTR;
}

static noinline void __sched __down(struct semaphore *sem)
Expand All @@ -256,5 +258,7 @@ static noinline void __sched __up(struct semaphore *sem)
{
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);
}

0 comments on commit 9d78960

Please sign in to comment.