Re: [PATCH] trylock for rw_semaphores: 2.4.1

Ben LaHaise (bcrl@redhat.com)
Mon, 19 Feb 2001 23:53:29 -0500 (EST)


On Mon, 19 Feb 2001, Brian J. Watson wrote:

> Here is an x86 implementation of down_read_trylock() and down_write_trylock()
> for read/write semaphores. As with down_trylock() for exclusive semaphores, they
> don't block if they fail to get the lock. They just return 1, as opposed to 0 in
> the success case.

How about the following instead? Warning: compiled, not tested.

-ben

diff -ur v2.4.2-pre3/include/asm-i386/semaphore.h trylock/include/asm-i386/semaphore.h
--- v2.4.2-pre3/include/asm-i386/semaphore.h Mon Feb 12 16:04:59 2001
+++ trylock/include/asm-i386/semaphore.h Mon Feb 19 23:50:03 2001
@@ -382,5 +382,32 @@
__up_write(sem);
}

+/* returns 1 if it successfully obtained the semaphore for write */
+static inline int down_write_trylock(struct rw_semaphore *sem)
+{
+ int old = RW_LOCK_BIAS, new = 0;
+ int res;
+
+ res = cmpxchg(&sem->count.counter, old, new);
+ return (res == RW_LOCK_BIAS);
+}
+
+/* returns 1 if it successfully obtained the semaphore for read */
+static inline int down_read_trylock(struct rw_semaphore *sem)
+{
+ int ret = 1;
+ asm volatile(
+ LOCK "subl $1,%0
+ js 2f
+ 1:
+ .section .text.lock,\"ax\"
+ 2:" LOCK "inc %0
+ subl %1,%1
+ jmp 1b
+ .previous"
+ :"=m" (*(volatile int *)sem), "=r" (ret) : "1" (ret) : "memory");
+ return ret;
+}
+
#endif
#endif

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/