Re: semaphore: lockless fastpath using atomic_{inc,dec}_return

From: Arjan van de Ven
Date: Wed Jul 09 2008 - 16:05:34 EST


> >
> From your words if we do:
>
> spin_lock()
> val = --foo;
> spin_unlock();
>
> Has the same cost than:
>
> val = atomic_dec_return(&foo);

yes


> How come I have the same number of atomic ops?
>
> Let's consider the fast case scenario (semaphore is unlocked for the
> 'down' and has no waiters for 'up') in x86:
> - with the spinlock only approach we have 2 atomic ops, xadd for
> lock, inc for unlock. The unlock doesn't come for free in x86 after
> all.

the unlock does not use an atomic operation.


--
If you want to reach me at my work email, use arjan@xxxxxxxxxxxxxxx
For development, discussion and tips for power savings,
visit http://www.lesswatts.org
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/