forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_atomic.cc
363 lines (300 loc) · 11.1 KB
/
swoole_atomic.cc
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
static PHP_METHOD(swoole_atomic, __construct);
static PHP_METHOD(swoole_atomic, add);
static PHP_METHOD(swoole_atomic, sub);
static PHP_METHOD(swoole_atomic, get);
static PHP_METHOD(swoole_atomic, set);
static PHP_METHOD(swoole_atomic, cmpset);
static PHP_METHOD(swoole_atomic, wait);
static PHP_METHOD(swoole_atomic, wakeup);
static PHP_METHOD(swoole_atomic_long, __construct);
static PHP_METHOD(swoole_atomic_long, add);
static PHP_METHOD(swoole_atomic_long, sub);
static PHP_METHOD(swoole_atomic_long, get);
static PHP_METHOD(swoole_atomic_long, set);
static PHP_METHOD(swoole_atomic_long, cmpset);
#ifdef HAVE_FUTEX
#include <linux/futex.h>
#include <syscall.h>
static sw_inline int swoole_futex_wait(sw_atomic_t *atomic, double timeout)
{
if (sw_atomic_cmp_set(atomic, 1, 0))
{
return SW_OK;
}
int ret;
struct timespec _timeout;
if (timeout > 0)
{
_timeout.tv_sec = (long) timeout;
_timeout.tv_nsec = (timeout - _timeout.tv_sec) * 1000 * 1000 * 1000;
ret = syscall(SYS_futex, atomic, FUTEX_WAIT, 0, &_timeout, NULL, 0);
}
else
{
ret = syscall(SYS_futex, atomic, FUTEX_WAIT, 0, NULL, NULL, 0);
}
if (ret == SW_OK && sw_atomic_cmp_set(atomic, 1, 0))
{
return SW_OK;
}
else
{
return SW_ERR;
}
}
static sw_inline int swoole_futex_wakeup(sw_atomic_t *atomic, int n)
{
if (sw_atomic_cmp_set(atomic, 0, 1))
{
return syscall(SYS_futex, atomic, FUTEX_WAKE, n, NULL, NULL, 0);
}
else
{
return SW_OK;
}
}
#endif
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_atomic_construct, 0, 0, 0)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_atomic_add, 0, 0, 0)
ZEND_ARG_INFO(0, add_value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_atomic_sub, 0, 0, 0)
ZEND_ARG_INFO(0, sub_value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_atomic_get, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_atomic_set, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_atomic_cmpset, 0, 0, 2)
ZEND_ARG_INFO(0, cmp_value)
ZEND_ARG_INFO(0, new_value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_atomic_wait, 0, 0, 0)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_atomic_waitup, 0, 0, 0)
ZEND_ARG_INFO(0, count)
ZEND_END_ARG_INFO()
zend_class_entry *swoole_atomic_ce;
static zend_object_handlers swoole_atomic_handlers;
zend_class_entry *swoole_atomic_long_ce;
static zend_object_handlers swoole_atomic_long_handlers;
static const zend_function_entry swoole_atomic_methods[] =
{
PHP_ME(swoole_atomic, __construct, arginfo_swoole_atomic_construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic, add, arginfo_swoole_atomic_add, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic, sub, arginfo_swoole_atomic_sub, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic, get, arginfo_swoole_atomic_get, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic, set, arginfo_swoole_atomic_set, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic, wait, arginfo_swoole_atomic_wait, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic, wakeup, arginfo_swoole_atomic_waitup, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic, cmpset, arginfo_swoole_atomic_cmpset, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static const zend_function_entry swoole_atomic_long_methods[] =
{
PHP_ME(swoole_atomic_long, __construct, arginfo_swoole_atomic_construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic_long, add, arginfo_swoole_atomic_add, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic_long, sub, arginfo_swoole_atomic_sub, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic_long, get, arginfo_swoole_atomic_get, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic_long, set, arginfo_swoole_atomic_set, ZEND_ACC_PUBLIC)
PHP_ME(swoole_atomic_long, cmpset, arginfo_swoole_atomic_cmpset, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void php_swoole_atomic_minit(int module_number)
{
SW_INIT_CLASS_ENTRY(swoole_atomic, "Swoole\\Atomic", "swoole_atomic", NULL, swoole_atomic_methods);
SW_SET_CLASS_SERIALIZABLE(swoole_atomic, zend_class_serialize_deny, zend_class_unserialize_deny);
SW_SET_CLASS_CLONEABLE(swoole_atomic, sw_zend_class_clone_deny);
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_atomic, sw_zend_class_unset_property_deny);
// SW_SET_CLASS_CREATE_WITH_ITS_OWN_HANDLERS(swoole_atomic);
SW_INIT_CLASS_ENTRY(swoole_atomic_long, "Swoole\\Atomic\\Long", "swoole_atomic_long", NULL, swoole_atomic_long_methods);
SW_SET_CLASS_SERIALIZABLE(swoole_atomic_long, zend_class_serialize_deny, zend_class_unserialize_deny);
SW_SET_CLASS_CLONEABLE(swoole_atomic_long, sw_zend_class_clone_deny);
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_atomic_long, sw_zend_class_unset_property_deny);
// SW_SET_CLASS_CREATE_WITH_ITS_OWN_HANDLERS(swoole_atomic_long);
}
PHP_METHOD(swoole_atomic, __construct)
{
zend_long value = 0;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
sw_atomic_t *atomic = (sw_atomic_t *) SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(sw_atomic_t));
if (atomic == NULL)
{
zend_throw_exception(swoole_exception_ce, "global memory allocation failure", SW_ERROR_MALLOC_FAIL);
RETURN_FALSE;
}
*atomic = (sw_atomic_t) value;
swoole_set_object(ZEND_THIS, (void*) atomic);
RETURN_TRUE;
}
PHP_METHOD(swoole_atomic, add)
{
zend_long add_value = 1;
sw_atomic_t *atomic = (sw_atomic_t *) swoole_get_object(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(add_value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
RETURN_LONG(sw_atomic_add_fetch(atomic, (uint32_t) add_value));
}
PHP_METHOD(swoole_atomic, sub)
{
zend_long sub_value = 1;
sw_atomic_t *atomic = (sw_atomic_t *) swoole_get_object(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sub_value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
RETURN_LONG(sw_atomic_sub_fetch(atomic, (uint32_t) sub_value));
}
PHP_METHOD(swoole_atomic, get)
{
sw_atomic_t *atomic = (sw_atomic_t *) swoole_get_object(ZEND_THIS);
RETURN_LONG(*atomic);
}
PHP_METHOD(swoole_atomic, set)
{
sw_atomic_t *atomic = (sw_atomic_t *) swoole_get_object(ZEND_THIS);
zend_long set_value;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(set_value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
*atomic = (uint32_t) set_value;
}
PHP_METHOD(swoole_atomic, cmpset)
{
zend_long cmp_value, set_value;
sw_atomic_t *atomic = (sw_atomic_t *) swoole_get_object(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(cmp_value)
Z_PARAM_LONG(set_value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
RETURN_BOOL(sw_atomic_cmp_set(atomic, (sw_atomic_t) cmp_value, (sw_atomic_t) set_value));
}
PHP_METHOD(swoole_atomic, wait)
{
double timeout = 1.0;
sw_atomic_t *atomic = (sw_atomic_t *) swoole_get_object(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
#ifdef HAVE_FUTEX
SW_CHECK_RETURN(swoole_futex_wait(atomic, timeout));
#else
timeout = timeout <= 0 ? INT_MAX : timeout;
int32_t i = (int32_t) sw_atomic_add_fetch(atomic, 1);
while (timeout > 0)
{
if ((int32_t) *atomic < i)
{
RETURN_TRUE;
}
else
{
usleep(1000);
timeout -= 0.001;
}
}
RETURN_FALSE;
#endif
}
PHP_METHOD(swoole_atomic, wakeup)
{
zend_long n = 1;
sw_atomic_t *atomic = (sw_atomic_t *) swoole_get_object(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(n)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
#ifdef HAVE_FUTEX
SW_CHECK_RETURN(swoole_futex_wakeup(atomic, (int ) n));
#else
sw_atomic_fetch_sub(atomic, n);
RETURN_TRUE;
#endif
}
PHP_METHOD(swoole_atomic_long, __construct)
{
zend_long value = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
sw_atomic_long_t *atomic = (sw_atomic_long_t *) SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(sw_atomic_long_t));
if (atomic == NULL)
{
zend_throw_exception(swoole_exception_ce, "global memory allocation failure", SW_ERROR_MALLOC_FAIL);
RETURN_FALSE;
}
*atomic = (sw_atomic_long_t) value;
swoole_set_object(ZEND_THIS, (void*) atomic);
RETURN_TRUE;
}
PHP_METHOD(swoole_atomic_long, add)
{
zend_long add_value = 1;
sw_atomic_long_t *atomic = (sw_atomic_long_t *) swoole_get_object(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(add_value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
RETURN_LONG(sw_atomic_add_fetch(atomic, (sw_atomic_long_t) add_value));
}
PHP_METHOD(swoole_atomic_long, sub)
{
zend_long sub_value = 1;
sw_atomic_long_t *atomic = (sw_atomic_long_t *) swoole_get_object(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sub_value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
RETURN_LONG(sw_atomic_sub_fetch(atomic, (sw_atomic_long_t) sub_value));
}
PHP_METHOD(swoole_atomic_long, get)
{
sw_atomic_long_t *atomic = (sw_atomic_long_t *) swoole_get_object(ZEND_THIS);
RETURN_LONG(*atomic);
}
PHP_METHOD(swoole_atomic_long, set)
{
sw_atomic_long_t *atomic = (sw_atomic_long_t *) swoole_get_object(ZEND_THIS);
zend_long set_value;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(set_value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
*atomic = (sw_atomic_long_t) set_value;
}
PHP_METHOD(swoole_atomic_long, cmpset)
{
zend_long cmp_value, set_value;
sw_atomic_long_t *atomic = (sw_atomic_long_t *) swoole_get_object(ZEND_THIS);
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(cmp_value)
Z_PARAM_LONG(set_value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
RETURN_BOOL(sw_atomic_cmp_set(atomic, (sw_atomic_long_t) cmp_value, (sw_atomic_long_t) set_value));
}