-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsynchronizer.c
More file actions
146 lines (115 loc) · 2.76 KB
/
synchronizer.c
File metadata and controls
146 lines (115 loc) · 2.76 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
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
#include "synchronizer.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include "tinycthread.h"
#include <zf_log.h>
struct urpc_synchronizer_t
{
mtx_t mutex;
cnd_t cond;
// handle "spurious wakeup" problem
bool really_notified;
unsigned int acquire_counter;
};
struct urpc_synchronizer_t *
urpc_syncronizer_create(
)
{
struct urpc_synchronizer_t *s = malloc(sizeof(struct urpc_synchronizer_t));
if (s == NULL)
{
goto malloc_failed;
}
if (mtx_init(&s->mutex, mtx_plain) != thrd_success)
{
goto mutex_init_failed;
}
if (cnd_init(&s->cond) != thrd_success)
{
goto cond_init_failed;
}
s->really_notified = false;
s->acquire_counter = 1;
return s;
cond_init_failed:
mtx_destroy(&s->mutex);
mutex_init_failed:
free(s);
malloc_failed:
ZF_LOGE("Can't create synchronizer");
return NULL;
}
int urpc_synchronizer_acquire(
struct urpc_synchronizer_t *s
)
{
assert(s != NULL);
if (mtx_lock(&s->mutex) != thrd_success)
{
ZF_LOGE("can't acquire synchronizer");
return 1;
}
s->acquire_counter++;
return 0;
}
// Must be called from the same thread which previously called urpc_synchronizer_acquire!
int urpc_synchronizer_release(
struct urpc_synchronizer_t *s
)
{
assert(s != NULL && s->acquire_counter >= 1);
s->acquire_counter--;
if (s->acquire_counter == 0)
{
s->really_notified = true;
if (cnd_signal(&s->cond) != thrd_success)
{
ZF_LOGE("can't release synchronizer");
return 1;
}
}
if (mtx_unlock(&s->mutex) != thrd_success)
{
ZF_LOGE("can't release synchronizer");
return 1;
}
return 0;
}
int urpc_synchronizer_destroy(
struct urpc_synchronizer_t *s
)
{
assert(s != NULL);
if (mtx_lock(&s->mutex) != thrd_success)
{
ZF_LOGE("can't destroy synchronizer");
return 1;
}
s->acquire_counter--;
if (s->acquire_counter > 0)
{
// wait for pending in-flight requests to complete (and fight the "spurious wakeup" problem)
while (!s->really_notified)
{
if (cnd_wait(&s->cond, &s->mutex) != thrd_success)
{
ZF_LOGE("can't destroy synchronizer");
return 1;
}
}
}
assert(s->acquire_counter == 0);
// at this point there SHOULD be no more users of the device except this stackframe so just unlock the mutex and cleanup resource
if (mtx_unlock(&s->mutex) != thrd_success)
{
ZF_LOGE("can't destroy synchronizer");
return 1;
}
mtx_destroy(&s->mutex);
cnd_destroy(&s->cond);
free(s);
return 0;
}