-
Notifications
You must be signed in to change notification settings - Fork 0
/
_rw_locks.c
123 lines (100 loc) · 2.85 KB
/
_rw_locks.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "_rw_locks.h"
int
rw_init(struct rw_lock_t *rw)
{
struct rw_lock_t temp={0,0,PTHREAD_MUTEX_INITIALIZER,PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER,PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER};
/* if ((temp = (struct rw_lock_t*) malloc(sizeof(struct rw_lock_t))) == NULL) { */
/* perror("Malloc failed."); */
/* exit(1); */
/* } */
/* temp->readers = 0; */
/* temp->writers = 0; */
/* /\* temp->WriterConditionMutex = PTHREAD_MUTEX_INITIALIZER; *\/ */
/* /\* temp->WriterMutex = PTHREAD_MUTEX_INITIALIZER; *\/ */
/* /\* temp->WriterCondition = PTHREAD_COND_INITIALIZER; *\/ */
/* /\* temp->ReaderConditionMutex = PTHREAD_MUTEX_INITIALIZER; *\/ */
/* /\* temp->ReaderCondition = PTHREAD_COND_INITIALIZER; *\/ */
*rw=temp;
if (rw->readers == 0 && rw->writers == 0)
return 0;
else
return 1;
}
int
rw_destroy(struct rw_lock_t *rw)
{
pthread_cond_destroy(&rw->WriterCondition);
pthread_cond_destroy(&rw->ReaderCondition);
pthread_mutex_destroy(&rw->WriterConditionMutex);
pthread_mutex_destroy(&rw->WriterMutex);
pthread_mutex_destroy(&rw->ReaderConditionMutex);
free(rw);
return 0;
}
int
rw_readlock(struct rw_lock_t *rw)
{
if (rw->readers < 0) {
printf("READER PANIC: %d\n", rw->readers);
exit(2);
}
pthread_mutex_lock(&rw->WriterConditionMutex);
while(rw->writers > 0) {
pthread_cond_wait(&rw->WriterCondition, &rw->WriterConditionMutex);
}
pthread_mutex_unlock(&rw->WriterConditionMutex);
pthread_mutex_lock(&rw->ReaderConditionMutex);
rw->readers++;
pthread_mutex_unlock(&rw->ReaderConditionMutex);
return 0;
}
int
rw_readunlock(struct rw_lock_t *rw)
{
if (rw->readers < 0) {
printf("READER PANIC\n");
exit(2);
}
pthread_mutex_lock(&rw->ReaderConditionMutex);
rw->readers--;
if (rw->readers == 0) {
pthread_cond_broadcast(&rw->ReaderCondition);
}
pthread_mutex_unlock(&rw->ReaderConditionMutex);
return 0;
}
int
rw_writelock(struct rw_lock_t *rw)
{
pthread_mutex_lock(&rw->WriterConditionMutex);
rw->writers++;
if (rw->writers > 2) {
printf("WRITER PANIC\n");
exit(2);
}
pthread_mutex_unlock(&rw->WriterConditionMutex);
pthread_mutex_lock(&rw->ReaderConditionMutex);
while(rw->readers > 0) {
pthread_cond_wait(&rw->ReaderCondition, &rw->ReaderConditionMutex);
}
pthread_mutex_unlock(&rw->ReaderConditionMutex);
pthread_mutex_lock(&rw->WriterMutex);
return 0;
}
int
rw_writeunlock(struct rw_lock_t *rw)
{
pthread_mutex_lock(&rw->WriterConditionMutex);
rw->writers--;
pthread_mutex_unlock(&rw->WriterConditionMutex);
pthread_mutex_lock(&rw->WriterConditionMutex);
if (rw->writers == 0) {
pthread_cond_broadcast(&rw->WriterCondition);
}
pthread_mutex_unlock(&rw->WriterConditionMutex);
pthread_mutex_unlock(&rw->WriterMutex);
return 0;
}