-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccr.c
170 lines (136 loc) · 3.79 KB
/
ccr.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <ccr.h>
#include <pthread.h>
#include <stdlib.h>
#include <errno.h> //errno macros
#define BSEM_WAIT(_bsem) \
do { \
err = pthread_mutex_lock(&_bsem.mtx); \
if (err) goto RET; \
while (!_bsem.count) { \
err = pthread_cond_wait(&_bsem.cond, &_bsem.mtx); \
if (err) goto RET; \
} \
_bsem.count = 0; \
err = pthread_mutex_unlock(&_bsem.mtx); \
if (err) goto RET; \
} while (0)
#define BSEM_POST(_bsem) \
do { \
err = pthread_mutex_lock(&_bsem.mtx); \
if (err) goto RET; \
_bsem.count = 1; \
err = pthread_cond_signal(&_bsem.cond); \
if (err) goto RET; \
err = pthread_mutex_unlock(&_bsem.mtx); \
if (err) goto RET; \
} while (0)
typedef struct internal_bsem_s {
pthread_mutex_t mtx;
pthread_cond_t cond;
int count;
} internal_bsem_s;
struct ccr_s {
internal_bsem_s mutex, q1, q2;
int n1, n2;
};
int ccr_init(ccr_s **ccr)
{
if (ccr) {
int err = 0;
pthread_mutexattr_t attr;
*ccr = malloc(sizeof(ccr_s));
if (!*ccr) {
return ENOMEM;
}
err = pthread_mutexattr_init(&attr);
if (err) goto INIT_FAIL_1;
err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (err) goto INIT_FAIL_2;
err = pthread_mutex_init(&(*ccr)->mutex.mtx, &attr);
if (err) goto INIT_FAIL_2;
err = pthread_mutex_init(&(*ccr)->q1.mtx, &attr);
if (err) goto INIT_FAIL_3;
err = pthread_mutex_init(&(*ccr)->q2.mtx, &attr);
if (err) goto INIT_FAIL_4;
err = pthread_cond_init(&(*ccr)->mutex.cond, NULL);
if (err) goto INIT_FAIL_5;
err = pthread_cond_init(&(*ccr)->q1.cond, NULL);
if (err) goto INIT_FAIL_6;
err = pthread_cond_init(&(*ccr)->q2.cond, NULL);
if (err) goto INIT_FAIL_7;
(*ccr)->mutex.count = 1;
(*ccr)->n1 = (*ccr)->n2 = 0;
(*ccr)->q1.count = (*ccr)->q2.count = 0;
pthread_mutexattr_destroy(&attr);
goto INIT_SUCCESS;
INIT_FAIL_7:
pthread_cond_destroy(&(*ccr)->q1.cond);
INIT_FAIL_6:
pthread_cond_destroy(&(*ccr)->mutex.cond);
INIT_FAIL_5:
pthread_mutex_destroy(&(*ccr)->q2.mtx);
INIT_FAIL_4:
pthread_mutex_destroy(&(*ccr)->q1.mtx);
INIT_FAIL_3:
pthread_mutex_destroy(&(*ccr)->mutex.mtx);
INIT_FAIL_2:
pthread_mutexattr_destroy(&attr);
INIT_FAIL_1:
free(*ccr);
INIT_SUCCESS:
return err;
}
return EINVAL;
}
int ccr_exec(ccr_s *ccr, condition_func cond, void *cond_param,
cs_body_func body, void *body_param)
{
int err = EINVAL;
if (ccr && cond) {
BSEM_WAIT(ccr->mutex);
while (!cond(cond_param)) {
ccr->n1++;
if (ccr->n2 > 0) {
ccr->n2--;
BSEM_POST(ccr->q2);
} else {
BSEM_POST(ccr->mutex);
}
BSEM_WAIT(ccr->q1);
ccr->n2++;
if (ccr->n1 > 0) {
ccr->n1--;
BSEM_POST(ccr->q1);
} else {
ccr->n2--;
BSEM_POST(ccr->q2);
}
BSEM_WAIT(ccr->q2);
}
if (body)
body(body_param);
if (ccr->n1 > 0) {
ccr->n1--;
BSEM_POST(ccr->q1);
} else if (ccr->n2 > 0) {
ccr->n2--;
BSEM_POST(ccr->q2);
} else {
BSEM_POST(ccr->mutex);
}
}
RET:
return err;
}
void ccr_destroy(ccr_s *ccr)
{
if (ccr) {
pthread_cond_destroy(&ccr->q2.cond);
pthread_cond_destroy(&ccr->q1.cond);
pthread_cond_destroy(&ccr->mutex.cond);
pthread_mutex_destroy(&ccr->q2.mtx);
pthread_mutex_destroy(&ccr->q1.mtx);
pthread_mutex_destroy(&ccr->mutex.mtx);
free(ccr);
}
}