2008-11-07 03:44:41 +00:00
|
|
|
#include <stasis/rw.h>
|
2004-07-20 00:15:17 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#undef pthread_cond_wait
|
|
|
|
#undef pthread_cond_timedwait
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
rwl *initlock (void)
|
|
|
|
{
|
|
|
|
rwl *lock;
|
|
|
|
|
|
|
|
lock = (rwl *)malloc (sizeof (rwl));
|
|
|
|
if (lock == NULL) return (NULL);
|
|
|
|
lock->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
|
|
|
|
if (lock->mut == NULL) { free (lock); return (NULL); }
|
2009-05-30 02:22:04 +00:00
|
|
|
lock->writeOK =
|
2004-06-24 21:10:31 +00:00
|
|
|
(pthread_cond_t *) malloc (sizeof (pthread_cond_t));
|
2009-05-30 02:22:04 +00:00
|
|
|
if (lock->writeOK == NULL) { free (lock->mut); free (lock);
|
2004-06-24 21:10:31 +00:00
|
|
|
return (NULL); }
|
2009-05-30 02:22:04 +00:00
|
|
|
lock->readOK =
|
2004-06-24 21:10:31 +00:00
|
|
|
(pthread_cond_t *) malloc (sizeof (pthread_cond_t));
|
2009-05-30 02:22:04 +00:00
|
|
|
if (lock->writeOK == NULL) { free (lock->mut); free (lock->writeOK);
|
2004-06-24 21:10:31 +00:00
|
|
|
free (lock); return (NULL); }
|
2009-05-30 02:22:04 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
pthread_mutex_init (lock->mut, NULL);
|
|
|
|
pthread_cond_init (lock->writeOK, NULL);
|
|
|
|
pthread_cond_init (lock->readOK, NULL);
|
|
|
|
lock->readers = 0;
|
|
|
|
lock->writers = 0;
|
|
|
|
lock->waiting = 0;
|
|
|
|
|
|
|
|
return (lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*void readlock(rwl *lock, int d) {
|
|
|
|
writelock(lock, d);
|
|
|
|
}*/
|
|
|
|
|
2009-05-30 02:22:04 +00:00
|
|
|
void assertlocked(rwl * lock) {
|
2007-06-01 21:32:33 +00:00
|
|
|
assert(lock->writers || lock->readers);
|
|
|
|
}
|
2009-05-30 02:22:04 +00:00
|
|
|
void assertunlocked(rwl * lock) {
|
|
|
|
assert(!(lock->writers || lock->readers));
|
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
void readlock (rwl *lock, int d)
|
|
|
|
{
|
2009-05-30 02:22:04 +00:00
|
|
|
/* printf("reader %d\n", d);
|
2004-07-20 00:15:17 +00:00
|
|
|
fflush(NULL); */
|
2009-05-30 02:22:04 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
pthread_mutex_lock (lock->mut);
|
2007-11-15 01:33:39 +00:00
|
|
|
if (lock->writers) { // XXX avoids deadlock; lets writers starve... || lock->waiting) {
|
2004-06-24 21:10:31 +00:00
|
|
|
do {
|
|
|
|
/* printf ("reader %d blocked. %d readers, %d writers, %d waiting\n", d, lock->readers, lock->writers, lock->waiting); */
|
|
|
|
pthread_cond_wait (lock->readOK, lock->mut);
|
|
|
|
/* printf ("reader %d unblocked.\n", d); */
|
|
|
|
} while (lock->writers);
|
|
|
|
}
|
|
|
|
lock->readers++;
|
|
|
|
pthread_mutex_unlock (lock->mut);
|
2009-05-30 02:22:04 +00:00
|
|
|
/* printf("reader %d done\n", d);
|
2004-07-20 00:15:17 +00:00
|
|
|
fflush(NULL); */
|
2009-05-30 02:22:04 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-03-08 07:36:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
void writelock (rwl *lock, int d)
|
|
|
|
{
|
2004-07-20 00:15:17 +00:00
|
|
|
/* printf("\nwritelock %d\n", d);
|
|
|
|
fflush(NULL); */
|
2004-06-24 21:10:31 +00:00
|
|
|
pthread_mutex_lock (lock->mut);
|
|
|
|
lock->waiting++;
|
|
|
|
while (lock->readers || lock->writers) {
|
|
|
|
/* printf ("writer %d blocked. %d readers, %d writers, %d waiting\n", d, lock->readers, lock->writers, lock->waiting); */
|
|
|
|
pthread_cond_wait (lock->writeOK, lock->mut);
|
|
|
|
/* printf ("writer %d unblocked.\n", d); */
|
2005-02-09 02:53:14 +00:00
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
lock->waiting--;
|
|
|
|
lock->writers++;
|
|
|
|
pthread_mutex_unlock (lock->mut);
|
|
|
|
|
2009-05-30 02:22:04 +00:00
|
|
|
/* printf("\nwritelock %d done\n", d);
|
2004-07-20 00:15:17 +00:00
|
|
|
fflush(NULL); */
|
2009-05-30 02:22:04 +00:00
|
|
|
|
2004-06-24 21:10:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-08 07:36:02 +00:00
|
|
|
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;
|
2009-05-30 02:22:04 +00:00
|
|
|
}
|
2007-03-08 07:36:02 +00:00
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
void downgradelock(rwl * lock) {
|
|
|
|
pthread_mutex_lock(lock->mut);
|
|
|
|
assert(lock->writers);
|
|
|
|
lock->writers--;
|
|
|
|
lock->readers++;
|
2004-10-04 05:24:12 +00:00
|
|
|
if(lock->waiting) {
|
2009-05-30 02:22:04 +00:00
|
|
|
pthread_cond_signal (lock->writeOK);
|
2004-10-04 05:24:12 +00:00
|
|
|
} else {
|
|
|
|
pthread_cond_broadcast(lock->readOK);
|
|
|
|
}
|
2004-07-20 00:15:17 +00:00
|
|
|
pthread_mutex_unlock(lock->mut);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock(rwl * lock) {
|
|
|
|
pthread_mutex_lock (lock->mut);
|
|
|
|
if(lock->readers) {
|
|
|
|
lock->readers--;
|
2004-10-04 05:24:12 +00:00
|
|
|
if(lock->waiting) {
|
|
|
|
pthread_cond_signal (lock->writeOK);
|
|
|
|
}
|
2004-07-20 00:15:17 +00:00
|
|
|
} else {
|
|
|
|
assert (lock->writers);
|
|
|
|
lock->writers--;
|
|
|
|
/* Need this as well (in case there's another writer, which is blocking the all of the readers. */
|
2004-10-04 05:24:12 +00:00
|
|
|
if(lock->waiting) {
|
2009-05-30 02:22:04 +00:00
|
|
|
pthread_cond_signal (lock->writeOK);
|
2004-10-04 05:24:12 +00:00
|
|
|
} else {
|
|
|
|
pthread_cond_broadcast (lock->readOK);
|
|
|
|
}
|
2004-07-20 00:15:17 +00:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock (lock->mut);
|
|
|
|
}
|
|
|
|
|
|
|
|
void readunlock(rwl * lock) {
|
|
|
|
unlock(lock);
|
|
|
|
}
|
|
|
|
void writeunlock(rwl * lock) {
|
|
|
|
unlock(lock);
|
|
|
|
}
|
2004-06-24 21:10:31 +00:00
|
|
|
|
|
|
|
void deletelock (rwl *lock)
|
|
|
|
{
|
|
|
|
pthread_mutex_destroy (lock->mut);
|
|
|
|
pthread_cond_destroy (lock->readOK);
|
|
|
|
pthread_cond_destroy (lock->writeOK);
|
2004-07-27 01:04:35 +00:00
|
|
|
free(lock->mut);
|
|
|
|
free(lock->writeOK);
|
|
|
|
free(lock->readOK);
|
2004-06-24 21:10:31 +00:00
|
|
|
free (lock);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|