From 76cd6b5114fc41d5dd782de2fec94b2644745158 Mon Sep 17 00:00:00 2001 From: Sears Russell Date: Thu, 8 Mar 2007 07:36:02 +0000 Subject: [PATCH] Added tryreadlock() and trywritelock(). --- libdfa/rw.h | 2 ++ src/libdfa/rw.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/libdfa/rw.h b/libdfa/rw.h index 41d473f..ac2ceb3 100644 --- a/libdfa/rw.h +++ b/libdfa/rw.h @@ -32,7 +32,9 @@ typedef struct { rwl *initlock (void); void readlock (rwl *lock, int d); +int tryreadlock(rwl *lock, int d); void writelock (rwl *lock, int d); +int trywritelock(rwl *lock, int d); void downgradelock(rwl * lock); void unlock(rwl * lock); /** @deprecated in favor of unlock() */ diff --git a/src/libdfa/rw.c b/src/libdfa/rw.c index f98d8b6..448947c 100644 --- a/src/libdfa/rw.c +++ b/src/libdfa/rw.c @@ -56,6 +56,18 @@ void readlock (rwl *lock, int d) return; } +int tryreadlock (rwl *lock, int d) +{ + pthread_mutex_lock (lock->mut); + if (lock->writers || lock->waiting) { + pthread_mutex_unlock (lock->mut); + return 0; + } + lock->readers++; + pthread_mutex_unlock (lock->mut); + return 1; +} + void writelock (rwl *lock, int d) { @@ -78,6 +90,19 @@ void writelock (rwl *lock, int d) return; } +int trywritelock(rwl *lock, int d) { + /* printf("\nwritelock %d\n", d); + fflush(NULL); */ + pthread_mutex_lock (lock->mut); + if (lock->readers || lock->writers) { + pthread_mutex_unlock(lock->mut); + return 0; + } + lock->writers++; + pthread_mutex_unlock (lock->mut); + return 1; +} + void downgradelock(rwl * lock) { pthread_mutex_lock(lock->mut); assert(lock->writers);