Added tryreadlock() and trywritelock().

This commit is contained in:
Sears Russell 2007-03-08 07:36:02 +00:00
parent 8f3d503ea8
commit 76cd6b5114
2 changed files with 27 additions and 0 deletions

View file

@ -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() */

View file

@ -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);