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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2009-2010. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
/*
* Author: Rickard Green
*/
#define ETHR_INLINE_FUNC_NAME_(X) X ## __
#define ETHR_EVENT_IMPL__
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ethread.h"
#if defined(ETHR_LINUX_FUTEX_IMPL__)
/* --- Linux futex implementation of ethread events ------------------------- */
#include <sched.h>
#include <errno.h>
#define ETHR_YIELD_AFTER_BUSY_LOOPS 50
int
ethr_event_init(ethr_event *e)
{
ethr_atomic_init(&e->futex, ETHR_EVENT_OFF__);
return 0;
}
int
ethr_event_destroy(ethr_event *e)
{
return 0;
}
static ETHR_INLINE int
wait__(ethr_event *e, int spincount)
{
unsigned sc = spincount;
int res;
ethr_sint_t val;
int until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;
if (spincount < 0)
ETHR_FATAL_ERROR__(EINVAL);
while (1) {
while (1) {
val = ethr_atomic_read(&e->futex);
if (val == ETHR_EVENT_ON__)
return 0;
if (sc == 0)
break;
sc--;
ETHR_SPIN_BODY;
if (--until_yield == 0) {
until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;
res = ETHR_YIELD();
if (res != 0)
ETHR_FATAL_ERROR__(res);
}
}
if (val != ETHR_EVENT_OFF_WAITER__) {
val = ethr_atomic_cmpxchg(&e->futex,
ETHR_EVENT_OFF_WAITER__,
ETHR_EVENT_OFF__);
if (val == ETHR_EVENT_ON__)
return 0;
ETHR_ASSERT(val == ETHR_EVENT_OFF__);
}
res = ETHR_FUTEX__(&e->futex, ETHR_FUTEX_WAIT__, ETHR_EVENT_OFF_WAITER__);
if (res == EINTR)
break;
if (res != 0 && res != EWOULDBLOCK)
ETHR_FATAL_ERROR__(res);
}
return res;
}
#elif defined(ETHR_PTHREADS)
/* --- Posix mutex/cond implementation of events ---------------------------- */
int
ethr_event_init(ethr_event *e)
{
int res;
ethr_atomic_init(&e->state, ETHR_EVENT_OFF__);
res = pthread_mutex_init(&e->mtx, NULL);
if (res != 0)
return res;
res = pthread_cond_init(&e->cnd, NULL);
if (res != 0) {
pthread_mutex_destroy(&e->mtx);
return res;
}
return 0;
}
int
ethr_event_destroy(ethr_event *e)
{
int res;
res = pthread_mutex_destroy(&e->mtx);
if (res != 0)
return res;
res = pthread_cond_destroy(&e->cnd);
if (res != 0)
return res;
return 0;
}
static ETHR_INLINE int
wait__(ethr_event *e, int spincount)
{
int sc = spincount;
ethr_sint_t val;
int res, ulres;
int until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;
if (spincount < 0)
ETHR_FATAL_ERROR__(EINVAL);
while (1) {
val = ethr_atomic_read(&e->state);
if (val == ETHR_EVENT_ON__)
return 0;
if (sc == 0)
break;
sc--;
ETHR_SPIN_BODY;
if (--until_yield == 0) {
until_yield = ETHR_YIELD_AFTER_BUSY_LOOPS;
res = ETHR_YIELD();
if (res != 0)
ETHR_FATAL_ERROR__(res);
}
}
if (val != ETHR_EVENT_OFF_WAITER__) {
val = ethr_atomic_cmpxchg(&e->state,
ETHR_EVENT_OFF_WAITER__,
ETHR_EVENT_OFF__);
if (val == ETHR_EVENT_ON__)
return 0;
ETHR_ASSERT(val == ETHR_EVENT_OFF__);
}
ETHR_ASSERT(val == ETHR_EVENT_OFF_WAITER__
|| val == ETHR_EVENT_OFF__);
res = pthread_mutex_lock(&e->mtx);
if (res != 0)
ETHR_FATAL_ERROR__(res);
while (1) {
val = ethr_atomic_read(&e->state);
if (val == ETHR_EVENT_ON__)
break;
res = pthread_cond_wait(&e->cnd, &e->mtx);
if (res == EINTR)
break;
if (res != 0)
ETHR_FATAL_ERROR__(res);
}
ulres = pthread_mutex_unlock(&e->mtx);
if (ulres != 0)
ETHR_FATAL_ERROR__(ulres);
return res; /* 0 || EINTR */
}
#else
#error No ethread event implementation
#endif
void
ethr_event_reset(ethr_event *e)
{
ethr_event_reset__(e);
}
void
ethr_event_set(ethr_event *e)
{
ethr_event_set__(e);
}
int
ethr_event_wait(ethr_event *e)
{
return wait__(e, 0);
}
int
ethr_event_swait(ethr_event *e, int spincount)
{
return wait__(e, spincount);
}
|