add assertunlocked() call

This commit is contained in:
Sears Russell 2009-05-30 02:22:04 +00:00
parent 8a95f5859d
commit 25c6be64b4
2 changed files with 21 additions and 17 deletions

View file

@ -13,15 +13,15 @@ rwl *initlock (void)
if (lock == NULL) return (NULL);
lock->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
if (lock->mut == NULL) { free (lock); return (NULL); }
lock->writeOK =
lock->writeOK =
(pthread_cond_t *) malloc (sizeof (pthread_cond_t));
if (lock->writeOK == NULL) { free (lock->mut); free (lock);
if (lock->writeOK == NULL) { free (lock->mut); free (lock);
return (NULL); }
lock->readOK =
lock->readOK =
(pthread_cond_t *) malloc (sizeof (pthread_cond_t));
if (lock->writeOK == NULL) { free (lock->mut); free (lock->writeOK);
if (lock->writeOK == NULL) { free (lock->mut); free (lock->writeOK);
free (lock); return (NULL); }
pthread_mutex_init (lock->mut, NULL);
pthread_cond_init (lock->writeOK, NULL);
pthread_cond_init (lock->readOK, NULL);
@ -36,15 +36,17 @@ rwl *initlock (void)
writelock(lock, d);
}*/
void assertlocked(rwl * lock) {
void assertlocked(rwl * lock) {
assert(lock->writers || lock->readers);
}
void assertunlocked(rwl * lock) {
assert(!(lock->writers || lock->readers));
}
void readlock (rwl *lock, int d)
{
/* printf("reader %d\n", d);
/* printf("reader %d\n", d);
fflush(NULL); */
pthread_mutex_lock (lock->mut);
if (lock->writers) { // XXX avoids deadlock; lets writers starve... || lock->waiting) {
do {
@ -55,9 +57,9 @@ void readlock (rwl *lock, int d)
}
lock->readers++;
pthread_mutex_unlock (lock->mut);
/* printf("reader %d done\n", d);
/* printf("reader %d done\n", d);
fflush(NULL); */
return;
}
int tryreadlock (rwl *lock, int d)
@ -88,9 +90,9 @@ void writelock (rwl *lock, int d)
lock->writers++;
pthread_mutex_unlock (lock->mut);
/* printf("\nwritelock %d done\n", d);
/* printf("\nwritelock %d done\n", d);
fflush(NULL); */
return;
}
@ -105,7 +107,7 @@ int trywritelock(rwl *lock, int d) {
lock->writers++;
pthread_mutex_unlock (lock->mut);
return 1;
}
}
void downgradelock(rwl * lock) {
pthread_mutex_lock(lock->mut);
@ -113,7 +115,7 @@ void downgradelock(rwl * lock) {
lock->writers--;
lock->readers++;
if(lock->waiting) {
pthread_cond_signal (lock->writeOK);
pthread_cond_signal (lock->writeOK);
} else {
pthread_cond_broadcast(lock->readOK);
}
@ -132,7 +134,7 @@ void unlock(rwl * lock) {
lock->writers--;
/* Need this as well (in case there's another writer, which is blocking the all of the readers. */
if(lock->waiting) {
pthread_cond_signal (lock->writeOK);
pthread_cond_signal (lock->writeOK);
} else {
pthread_cond_broadcast (lock->readOK);
}

View file

@ -11,7 +11,7 @@
*
* Date : 18 September 1997
*
* Revised : 4-7-04 Shamelessly stolen and adapted by Rusty Sears.
* Revised : 4-7-04 Shamelessly stolen and adapted by Rusty Sears.
* Found the code at this url:
* http://www.cs.nmsu.edu/~jcook/Tools/pthreads/rw.c
*/
@ -48,6 +48,8 @@ void writelock (rwl *lock, int d);
int trywritelock(rwl *lock, int d);
/** aborts if called when no thread holds this latch. */
void assertlocked(rwl *lock);
/** aborts if called when a thread holds this latch. */
void assertunlocked(rwl *lock);
void downgradelock(rwl * lock);
void unlock(rwl * lock);
/** @deprecated in favor of unlock() */