Listing 3

/* rwsem.h */
#ifndef RWSEM_H    /* prevent multiple includes */
#define RWSEM_H

#include <limits.h>
#include "semaphor.h"

/* constants */
#define RWSOPEN_MAX SEMOPEN_MAX /* max # rwsem sets open at once */

/* type definitions */
typedef struct {             /* rwsem set control structure */
    char rwsdir[PATH_MAX + 1];  /* directory */
    int rwsc;           /* r/w semaphore count */
    semset_t *ssp;          /* semaphore set */
    short *lockheld;        /* locks held by calling process */
} rwsset_t;

/* function declarations */
int     rwsclose(rwsset_t *rwsp);
#define     rwscount(rwsp) ((rwsp)->rwsc)
int     rwslock(rwsset_t *rwsp, int rwsno, int ltype);
rwsset_t *  rwsopen(char *rwsdir, int flags, int rwsc);
int     rwsremove(char *rwsdir);

/* rwsopen command flags */
#define RWS_CREAT   (01000)     /* create and open */
#define RWS_EXCL    (02000)     /* exclusive open */

/* lock types */
#define RWS_UNLCK   (0)     /* unlock */
#define RWS_RDLCK   (1)     /* read lock */
#define RWS_WRLCK   (2)     /* write lock */

/* error codes */
#define RWSEOS      (-20)      /* start of error code domain */
#define RWSEMFILE   (RWSEOS - 1)   /* too many rwsem sets open */
#define RWSPANIC    (RWSEOS - 2)   /* internal rwsem error */

#endif /* #ifndef RWSEM_H */