Skip to content

Commit

Permalink
ipc/sem.c: optimize single sops when semval is zero
Browse files Browse the repository at this point in the history
If multiple simple decrements on the same semaphore are pending, then the
current code scans all decrement operations, even if the semaphore value
is already 0.

The patch optimizes that: if the semaphore value is 0, then there is no
need to scan the q->alter entries.

Note that this is a common case: It happens if 100 decrements by one are
pending and now an increment by one increases the semaphore value from 0
to 1.  Without this patch, all 100 entries are scanned.  With the patch,
only one entry is scanned, then woken up.  Then the new rule triggers and
the scanning is aborted, without looking at the remaining 99 tasks.

With this patch, single sop increment/decrement by 1 are now O(1).
(same as with Nick's patch)

Signed-off-by: Manfred Spraul <manfred@colorfullife.com>
Cc: Nick Piggin <npiggin@suse.de>
Cc: Pierre Peiffer <peifferp@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Manfred Spraul authored and Linus Torvalds committed Dec 16, 2009
1 parent 636c6be commit d987f8b
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ipc/sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,17 @@ static void update_queue(struct sem_array *sma, int semnum)
q = (struct sem_queue *)((char *)walk - offset);
walk = walk->next;

/* If we are scanning the single sop, per-semaphore list of
* one semaphore and that semaphore is 0, then it is not
* necessary to scan the "alter" entries: simple increments
* that affect only one entry succeed immediately and cannot
* be in the per semaphore pending queue, and decrements
* cannot be successful if the value is already 0.
*/
if (semnum != -1 && sma->sem_base[semnum].semval == 0 &&
q->alter)
break;

error = try_atomic_semop(sma, q->sops, q->nsops,
q->undo, q->pid);

Expand Down

0 comments on commit d987f8b

Please sign in to comment.