On Wed, 2011-06-08 at 12:27 -0700, Linus Torvalds wrote: > Make some kind of > > void atomic_down(); > int atomic_down_trylock(); > void atomic_up(); atomic_down() is a tad iffy, it would have to wait for an actual semaphore owner, which might sleep etc.. So I skipped it. The other two are implemented here, and assume IRQs are disabled, we could add _irq and _irqsave versions of both, but since there are no users I avoided the effort. --- include/linux/semaphore.h | 3 +++ kernel/semaphore.c | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) Index: linux-2.6/include/linux/semaphore.h =================================================================== --- linux-2.6.orig/include/linux/semaphore.h +++ linux-2.6/include/linux/semaphore.h @@ -43,4 +43,7 @@ extern int __must_check down_trylock(str extern int __must_check down_timeout(struct semaphore *sem, long jiffies); extern void up(struct semaphore *sem); +extern int atomic_down_trylock(struct semaphore *sem); +extern void atomic_up(struct semaphore *sem); + #endif /* __LINUX_SEMAPHORE_H */ Index: linux-2.6/kernel/semaphore.c =================================================================== --- linux-2.6.orig/kernel/semaphore.c +++ linux-2.6/kernel/semaphore.c @@ -118,7 +118,7 @@ EXPORT_SYMBOL(down_killable); * 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 + * Try to acquire the semaphore atomically. Returns 0 if the semaphore has * been acquired successfully or 1 if it it cannot be acquired. * * NOTE: This return value is inverted from both spin_trylock and @@ -143,6 +143,29 @@ int down_trylock(struct semaphore *sem) EXPORT_SYMBOL(down_trylock); /** + * atomic_down_trylock - try to acquire the semaphore internal lock + * #sem: the semaphore to be acquired + * + * Try to acquire the semaphore internal lock, blocking all other semaphore + * operations. Returns 0 if the trylock has been acquired successfully or + * 1 if it cannot be acquired. + * + * NOTE: This return value is inverted from both spin_trylock and + * mutex_trylock! Be careful about this when converting code. + * + * NOTE: assumes IRQs are disabled. + */ +int atomic_down_trylock(struct semaphore *sem) +{ + spin_lock(&sem->lock); + if (sem->count > 0) + return 0; + + spin_unlock(&sem->lock); + return 1; +} + +/** * down_timeout - acquire the semaphore within a specified time * @sem: the semaphore to be acquired * @jiffies: how long to wait before failing @@ -188,6 +211,17 @@ void up(struct semaphore *sem) } EXPORT_SYMBOL(up); +/** + * atomic_up - release the semaphore internal lock + * @sem: the semaphore to release the internal lock of + * + * Release the semaphore internal lock. + */ +void atomic_up(struct semaphore *sem) +{ + spin_unlock(&sem->lock); +} + /* Functions for the contended case */ struct semaphore_waiter { -- To unsubscribe from this list: send the line "unsubscribe linux-tip-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html