forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_coroutine_util.c
527 lines (457 loc) · 15.6 KB
/
swoole_coroutine_util.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include "php_swoole.h"
#ifdef SW_COROUTINE
#include "swoole_coroutine.h"
static PHP_METHOD(swoole_coroutine_util, create);
static PHP_METHOD(swoole_coroutine_util, suspend);
static PHP_METHOD(swoole_coroutine_util, cli_wait);
static PHP_METHOD(swoole_coroutine_util, resume);
static PHP_METHOD(swoole_coroutine_util, getuid);
static PHP_METHOD(swoole_coroutine_util, call_user_func);
static PHP_METHOD(swoole_coroutine_util, call_user_func_array);
static swHashMap *defer_coros;
static zend_class_entry swoole_coroutine_util_ce;
static zend_class_entry *swoole_coroutine_util_class_entry_ptr;
extern jmp_buf *swReactorCheckPoint;
static const zend_function_entry swoole_coroutine_util_methods[] =
{
PHP_ME(swoole_coroutine_util, create, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_util, cli_wait, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_util, suspend, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_util, resume, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_util, getuid, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_util, call_user_func, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_util, call_user_func_array, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
};
void swoole_coroutine_util_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_coroutine_util_ce, "swoole_coroutine", "Swoole\\Coroutine", swoole_coroutine_util_methods);
swoole_coroutine_util_class_entry_ptr = zend_register_internal_class(&swoole_coroutine_util_ce TSRMLS_CC);
if (SWOOLE_G(use_namespace))
{
zend_register_class_alias("swoole_coroutine", swoole_coroutine_util_class_entry_ptr);
}
else
{
zend_register_class_alias("Swoole\\Coroutine", swoole_coroutine_util_class_entry_ptr);
}
defer_coros = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL);
}
static void swoole_coroutine_util_resume(void *data)
{
php_context *context = (php_context *)data;
zval *retval = NULL;
zval *result;
SW_MAKE_STD_ZVAL(result);
ZVAL_BOOL(result, 1);
int ret = coro_resume(context, result, &retval);
if (ret == CORO_END && retval)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&result);
efree(context);
}
#if PHP_MAJOR_VERSION < 7
#if ZEND_MODULE_API_NO <= 20121212
#define zend_create_execute_data_from_op_array sw_zend_create_execute_data_from_op_array
zend_execute_data *sw_zend_create_execute_data_from_op_array(zend_op_array *op_array, zend_bool nested TSRMLS_DC)
{
zend_execute_data *execute_data;
size_t execute_data_size = ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data));
size_t CVs_size = ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2));
size_t Ts_size = ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T;
size_t call_slots_size = ZEND_MM_ALIGNED_SIZE(sizeof(call_slot)) * op_array->nested_calls;
size_t stack_size = ZEND_MM_ALIGNED_SIZE(sizeof(zval*)) * op_array->used_stack;
size_t total_size = execute_data_size + Ts_size + CVs_size + call_slots_size + stack_size;
execute_data = zend_vm_stack_alloc(total_size TSRMLS_CC);
execute_data = (zend_execute_data*)((char*)execute_data + Ts_size);
execute_data->prev_execute_data = EG(current_execute_data);
memset(EX_CV_NUM(execute_data, 0), 0, sizeof(zval **) * op_array->last_var);
execute_data->call_slots = (call_slot*)((char *)execute_data + execute_data_size + CVs_size);
execute_data->op_array = op_array;
EG(argument_stack)->top = zend_vm_stack_frame_base(execute_data);
execute_data->object = NULL;
execute_data->current_this = NULL;
execute_data->old_error_reporting = NULL;
execute_data->symbol_table = EG(active_symbol_table);
execute_data->call = NULL;
EG(current_execute_data) = execute_data;
execute_data->nested = nested;
if (!op_array->run_time_cache && op_array->last_cache_slot) {
op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*));
}
if (op_array->this_var != -1 && EG(This)) {
Z_ADDREF_P(EG(This)); /* For $this pointer */
if (!EG(active_symbol_table)) {
SW_EX_CV(op_array->this_var) = (zval **) SW_EX_CV_NUM(execute_data, op_array->last_var + op_array->this_var);
*SW_EX_CV(op_array->this_var) = EG(This);
} else {
if (zend_hash_add(EG(active_symbol_table), "this", sizeof("this"), &EG(This), sizeof(zval *), (void **) EX_CV_NUM(execute_data, op_array->this_var))==FAILURE) {
Z_DELREF_P(EG(This));
}
}
}
execute_data->opline = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes;
EG(opline_ptr) = &(execute_data->opline);
execute_data->function_state.function = (zend_function *) op_array;
execute_data->function_state.arguments = NULL;
return execute_data;
}
#endif
static void swoole_corountine_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zval **return_value_ptr, zend_bool use_array, int return_value_used)
{
SWOOLE_GET_TSRMLS;
int i;
zval **origin_return_ptr_ptr;
zend_op **origin_opline_ptr;
zend_op_array *origin_active_op_array;
zend_op_array *op_array = (zend_op_array *)fci_cache->function_handler;
zend_execute_data *current = EG(current_execute_data);
void **start, **end, **allocated_params, **old_arguments;
if (use_array)
{
start = (void **)emalloc(sizeof(zval **) * (fci->param_count + 1));
allocated_params = start;
end = start + fci->param_count;
old_arguments = current->function_state.arguments;
current->function_state.arguments = end;
for (i = 0; i < fci->param_count; ++i)
{
*start = *fci->params[i];
++start;
}
*start = (void*)(zend_uintptr_t)fci->param_count;
}
else
{
end = EG(argument_stack)->top - 1;
start = end - (int)(zend_uintptr_t)(*end);
zval_ptr_dtor((zval **)(start));
for (i = 0; i < fci->param_count; ++i)
{
*start = *(start + 1);
++start;
}
*start = (void*)(zend_uintptr_t)fci->param_count;
EG(argument_stack)->top = start + 1;
current->function_state.arguments = start;
}
origin_return_ptr_ptr = EG(return_value_ptr_ptr);
if (current->opline->result_type & EXT_TYPE_UNUSED)
{
EG(return_value_ptr_ptr) = NULL;
}
else
{
EG(return_value_ptr_ptr) = return_value_ptr;
}
origin_active_op_array = EG(active_op_array);
origin_opline_ptr = EG(opline_ptr);
EG(active_op_array) = op_array;
EG(active_symbol_table) = NULL;
EG(scope) = fci_cache->calling_scope;
if (fci_cache->called_scope)
{
EG(called_scope) = fci_cache->called_scope;
}
else
{
EG(called_scope) = NULL;
}
if (fci_cache->object_ptr)
{
EG(This) = fci_cache->object_ptr;
if (!PZVAL_IS_REF(EG(This)))
{
Z_ADDREF_P(EG(This));
}
else
{
zval *this_ptr;
ALLOC_ZVAL(this_ptr);
*this_ptr = *EG(This);
INIT_PZVAL(this_ptr);
zval_copy_ctor(this_ptr);
EG(This) = this_ptr;
}
}
else
{
EG(This) = NULL;
}
zend_execute_data *next = zend_create_execute_data_from_op_array(op_array, 0 TSRMLS_CC);
jmp_buf *prev_checkpoint = swReactorCheckPoint;
swReactorCheckPoint = emalloc(sizeof(jmp_buf));
if (!setjmp(*swReactorCheckPoint))
{
zend_execute_ex(next TSRMLS_CC);
if (fci->params)
{
efree(fci->params);
if (use_array)
{
for (i = 0; i < fci->param_count; ++i)
{
zval *tmp = (zval *) *(--start);
zval_ptr_dtor(&tmp);
}
efree(allocated_params);
}
}
efree(swReactorCheckPoint);
swReactorCheckPoint = prev_checkpoint;
EG(active_op_array) = origin_active_op_array;
EG(return_value_ptr_ptr) = origin_return_ptr_ptr;
EG(opline_ptr) = origin_opline_ptr;
}
else
{
current->original_return_value = origin_return_ptr_ptr;
next->nested = 1;
efree(swReactorCheckPoint);
swReactorCheckPoint = prev_checkpoint;
if (!return_value_used && return_value_ptr)
zval_ptr_dtor(return_value_ptr);
if (fci->params)
{
efree(fci->params);
if (use_array)
{
efree(allocated_params);
current->function_state.arguments = old_arguments;
}
}
longjmp(*swReactorCheckPoint, 1);
}
}
#else
static void swoole_corountine_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, zend_bool use_array)
{
int i;
zend_execute_data *call, *current_ex = EG(current_execute_data);
zend_function *func = fci_cache->function_handler;
zend_object *object = (func->common.fn_flags & ZEND_ACC_STATIC) ? NULL : fci_cache->object;
#if ZEND_MODULE_API_NO < 20160303
zend_class_entry* origal_scope = EG(scope);
call = zend_vm_stack_push_call_frame(ZEND_CALL_TOP_FUNCTION, func,
fci->param_count, fci_cache->called_scope, object);
EG(scope) = func->common.scope;
#else
call = zend_vm_stack_push_call_frame(ZEND_CALL_TOP_FUNCTION | ZEND_CALL_DYNAMIC, func,
fci->param_count, fci_cache->called_scope, object);
#endif
for (i = 0; i < fci->param_count; ++i)
{
zval *target;
target = ZEND_CALL_ARG(call, i + 1);
ZVAL_COPY(target, &fci->params[i]);
}
call->symbol_table = NULL;
zend_init_execute_data(call, &func->op_array, fci->retval);
jmp_buf *prev_checkpoint = swReactorCheckPoint;
swReactorCheckPoint = emalloc(sizeof(jmp_buf));
if (!setjmp(*swReactorCheckPoint))
{
zend_execute_ex(call);
efree(swReactorCheckPoint);
swReactorCheckPoint = prev_checkpoint;
#if ZEND_MODULE_API_NO < 20160303
EG(scope) = origal_scope;
#endif
}
else
{
call->prev_execute_data = current_ex->prev_execute_data;
#if ZEND_MODULE_API_NO < 20160303
ZEND_SET_CALL_INFO(call, ZEND_CALL_NESTED);
#else
ZEND_SET_CALL_INFO(call, object, ZEND_CALL_DYNAMIC|ZEND_CALL_NESTED);
#endif
efree(swReactorCheckPoint);
swReactorCheckPoint = prev_checkpoint;
if (use_array) {
zend_fcall_info_args_clear(fci, 1);
}
zend_vm_stack_free_args(current_ex);
longjmp(*swReactorCheckPoint, 1);
}
}
#endif
#if PHP_MAJOR_VERSION < 7
static PHP_METHOD(swoole_coroutine_util, call_user_func)
{
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f*",&fci, &fci_cache, &fci.params, &fci.param_count) == FAILURE)
{
return;
}
swoole_corountine_call_function(&fci, &fci_cache, return_value_ptr, 0, return_value_used);
RETURN_FALSE;
}
#else
static PHP_METHOD(swoole_coroutine_util, call_user_func)
{
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_FUNC(fci, fci_cache)
Z_PARAM_VARIADIC('*', fci.params, fci.param_count)
ZEND_PARSE_PARAMETERS_END();
fci.retval = (execute_data->prev_execute_data->opline->result_type != IS_UNUSED) ? return_value : NULL;
swoole_corountine_call_function(&fci, &fci_cache, 0);
}
#endif
#if PHP_MAJOR_VERSION < 7
static PHP_METHOD(swoole_coroutine_util, call_user_func_array)
{
zval *params;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "fa/",&fci, &fci_cache, ¶ms) == FAILURE)
{
return;
}
zend_fcall_info_args(&fci, params TSRMLS_CC);
swoole_corountine_call_function(&fci, &fci_cache, return_value_ptr, 1, return_value_used);
RETURN_FALSE;
}
#else
static PHP_METHOD(swoole_coroutine_util, call_user_func_array)
{
zval *params;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_FUNC(fci, fci_cache)
Z_PARAM_ARRAY_EX(params, 0, 1)
ZEND_PARSE_PARAMETERS_END();
zend_fcall_info_args(&fci, params);
fci.retval = (execute_data->prev_execute_data->opline->result_type != IS_UNUSED) ? return_value : NULL;
swoole_corountine_call_function(&fci, &fci_cache, 1);
zend_fcall_info_args_clear(&fci, 1);
}
#endif
static PHP_METHOD(swoole_coroutine_util, suspend)
{
char *id;
int id_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",&id, &id_len) == FAILURE)
{
return;
}
swLinkedList *coros_list = swHashMap_find(defer_coros, id, id_len);
if (coros_list == NULL)
{
coros_list = swLinkedList_new(2, NULL);
if (coros_list == NULL)
{
RETURN_FALSE;
}
if (swHashMap_add(defer_coros, id, id_len, coros_list) == SW_ERR)
{
swLinkedList_free(coros_list);
RETURN_FALSE;
}
}
php_context *context = emalloc(sizeof(php_context));
coro_save(context);
if (swLinkedList_append(coros_list, (void *)context) == SW_ERR) {
efree(context);
RETURN_FALSE;
}
coro_yield();
}
static PHP_METHOD(swoole_coroutine_util, create)
{
zval *callback;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callback) == FAILURE)
{
return;
}
sw_zval_add_ref(&callback);
callback = sw_zval_dup(callback);
char *func_name = NULL;
zend_fcall_info_cache *func_cache = emalloc(sizeof(zend_fcall_info_cache));
if (!sw_zend_is_callable_ex(callback, NULL, 0, &func_name, NULL, func_cache, NULL TSRMLS_CC))
{
swoole_php_fatal_error(E_ERROR, "Function '%s' is not callable", func_name);
sw_zval_free(callback);
efree(func_name);
return;
}
efree(func_name);
php_swoole_check_reactor();
if (swReactorCheckPoint == NULL)
{
coro_init(TSRMLS_C);
}
zval *retval = NULL;
zval *args[1];
jmp_buf *prev_checkpoint = swReactorCheckPoint;
swReactorCheckPoint = emalloc(sizeof(jmp_buf));
php_context *cxt = emalloc(sizeof(php_context));
coro_save(cxt);
int ret = coro_create(func_cache, args, 0, &retval, NULL, NULL);
efree(func_cache);
efree(swReactorCheckPoint);
if (ret < 0)
{
sw_zval_free(callback);
RETURN_FALSE;
}
//save callback
COROG.current_coro->function = callback;
swReactorCheckPoint = prev_checkpoint;
coro_resume_parent(cxt, retval, retval);
efree(cxt);
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_coroutine_util, cli_wait) {
if (SwooleGS->start == 1) {
RETURN_FALSE;
}
php_context *cxt = emalloc(sizeof(php_context));
coro_save(cxt);
php_swoole_event_wait();
coro_resume_parent(cxt, NULL, NULL);
efree(cxt);
RETURN_LONG(COROG.coro_num);
}
static PHP_METHOD(swoole_coroutine_util, resume)
{
char *id;
int id_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &id, &id_len) == FAILURE)
{
return;
}
swLinkedList *coros_list = swHashMap_find(defer_coros, id, id_len);
if (coros_list == NULL)
{
swoole_php_fatal_error(E_WARNING, "Nothing can coroResume.");
RETURN_FALSE;
}
php_context *context = swLinkedList_shift(coros_list);
if (context == NULL)
{
swoole_php_fatal_error(E_WARNING, "Nothing can coroResume.");
RETURN_FALSE;
}
SwooleG.main_reactor->defer(SwooleG.main_reactor, swoole_coroutine_util_resume, context);
RETURN_TRUE;
}
static PHP_METHOD(swoole_coroutine_util, getuid)
{
RETURN_LONG(COROG.current_coro->cid);
}
#endif