-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrwlock.c
More file actions
61 lines (46 loc) · 1.37 KB
/
rwlock.c
File metadata and controls
61 lines (46 loc) · 1.37 KB
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
50
51
52
53
54
55
56
57
58
59
60
/****************************************************************************
FILE : rwlock.c
LAST REVISED : 2007-11-22
SUBJECT : Reader/writer locks.
PROGRAMMER : (C) Copyright 2007 by Peter C. Chapin
Please send comments or bug reports pertaining to this file to
Peter C. Chapin
Electrical and Computer Engineering Technology
Vermont Technical College
Randolph Center, VT 05061
Peter.Chapin@vtc.vsc.edu
****************************************************************************/
#include "rwlock.h"
void rw_init( rw_lock *lock )
{
pthread_mutex_init( &lock->mutex, NULL );
pthread_mutex_init( &lock->wrt, NULL );
lock->readcount = 0;
}
void rw_destroy( rw_lock *lock )
{
pthread_mutex_destroy( &lock->mutex );
pthread_mutex_destroy( &lock->wrt );
}
void read_lock( rw_lock *lock )
{
pthread_mutex_lock( &lock->mutex );
lock->readcount++;
if( lock->readcount == 1 ) pthread_mutex_lock( &lock->wrt );
pthread_mutex_unlock( &lock->mutex );
}
void read_unlock( rw_lock *lock )
{
pthread_mutex_lock( &lock->mutex );
lock->readcount--;
if( lock->readcount == 0 ) pthread_mutex_unlock( &lock->wrt );
pthread_mutex_unlock( &lock->mutex );
}
void write_lock( rw_lock *lock )
{
pthread_mutex_lock( &lock->wrt );
}
void write_unlock( rw_lock *lock )
{
pthread_mutex_unlock( &lock->wrt );
}