forked from hailinzeng/Programming-POSIX-Threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwlock.h
49 lines (45 loc) · 1.65 KB
/
rwlock.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* rwlock.h
*
* This header file describes the "reader/writer lock" synchronization
* construct. The type rwlock_t describes the full state of the lock
* including the POSIX 1003.1c synchronization objects necessary.
*
* A reader/writer lock allows a thread to lock shared data either for shared
* read access or exclusive write access.
*
* The rwl_init() and rwl_destroy() functions, respectively, allow you to
* initialize/create and destroy/free the reader/writer lock.
*/
#include <pthread.h>
/*
* Structure describing a read-write lock.
*/
typedef struct rwlock_tag {
pthread_mutex_t mutex;
pthread_cond_t read; /* wait for read */
pthread_cond_t write; /* wait for write */
int valid; /* set when valid */
int r_active; /* readers active */
int w_active; /* writer active */
int r_wait; /* readers waiting */
int w_wait; /* writers waiting */
} rwlock_t;
#define RWLOCK_VALID 0xfacade
/*
* Support static initialization of barriers
*/
#define RWL_INITIALIZER \
{PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
PTHREAD_COND_INITIALIZER, RWLOCK_VALID, 0, 0, 0, 0}
/*
* Define read-write lock functions
*/
extern int rwl_init (rwlock_t *rwlock);
extern int rwl_destroy (rwlock_t *rwlock);
extern int rwl_readlock (rwlock_t *rwlock);
extern int rwl_readtrylock (rwlock_t *rwlock);
extern int rwl_readunlock (rwlock_t *rwlock);
extern int rwl_writelock (rwlock_t *rwlock);
extern int rwl_writetrylock (rwlock_t *rwlock);
extern int rwl_writeunlock (rwlock_t *rwlock);