2004-06-24 21:10:31 +00:00
|
|
|
/*
|
|
|
|
* File : rw.c
|
|
|
|
*
|
|
|
|
* Title : Demo Readers/Writer.
|
|
|
|
*
|
|
|
|
* Short : A solution to the multi-reader's, one writer problem.
|
|
|
|
*
|
|
|
|
* Long :
|
|
|
|
*
|
|
|
|
* Author : Andrae Muys
|
|
|
|
*
|
|
|
|
* Date : 18 September 1997
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2004-07-04 00:46:49 +00:00
|
|
|
#ifndef __LIBDFA_RW_H
|
|
|
|
#define __LIBDFA_RW_H
|
2004-06-24 21:10:31 +00:00
|
|
|
typedef struct {
|
|
|
|
pthread_mutex_t *mut;
|
|
|
|
int writers;
|
|
|
|
int readers;
|
|
|
|
int waiting;
|
|
|
|
pthread_cond_t *writeOK, *readOK;
|
|
|
|
} rwl;
|
|
|
|
|
|
|
|
rwl *initlock (void);
|
|
|
|
void readlock (rwl *lock, int d);
|
|
|
|
void writelock (rwl *lock, int d);
|
|
|
|
void readunlock (rwl *lock);
|
|
|
|
void writeunlock (rwl *lock);
|
|
|
|
void deletelock (rwl *lock);
|
|
|
|
/*
|
|
|
|
typedef struct {
|
|
|
|
rwl *lock;
|
|
|
|
int id;
|
|
|
|
long delay;
|
|
|
|
} rwargs;
|
|
|
|
|
|
|
|
rwargs *newRWargs (rwl *l, int i, long d);
|
|
|
|
void *reader (void *args);
|
|
|
|
void *writer (void *args);
|
|
|
|
*/
|
2004-07-04 00:46:49 +00:00
|
|
|
#endif /* rw.h */
|