Skip to content

Commit df17bd4

Browse files
committed
update coro
1 parent f18676d commit df17bd4

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

swoole_coroutine.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
((int)((ZEND_MM_ALIGNED_SIZE(sizeof(coro_task)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval))))
3939
#define SWCC(x) sw_current_context->x
4040
coro_global COROG;
41-
zval *allocated_return_value_ptr = NULL;
4241

4342
static void (*orig_interrupt_function)(zend_execute_data *execute_data);
4443
static void sw_interrupt_function(zend_execute_data *execute_data);
@@ -118,9 +117,6 @@ int coro_init(TSRMLS_D)
118117
fake_frame.return_value = NULL;
119118
fake_frame.prev_execute_data = NULL;
120119

121-
COROG.origin_vm_stack = EG(vm_stack);
122-
COROG.origin_vm_stack_top = EG(vm_stack_top);
123-
COROG.origin_vm_stack_end = EG(vm_stack_end);
124120
COROG.origin_ex = &fake_frame;
125121

126122
while (1)
@@ -164,9 +160,7 @@ int coro_init(TSRMLS_D)
164160
{
165161
COROG.stack_size = DEFAULT_STACK_SIZE;
166162
}
167-
zval *retval;
168-
SW_ALLOC_INIT_ZVAL(retval);
169-
allocated_return_value_ptr = retval;
163+
170164
COROG.require = 0;
171165
COROG.active = 1;
172166
SwooleWG.coro_timeout_list = swLinkedList_new(1, NULL);
@@ -175,10 +169,7 @@ int coro_init(TSRMLS_D)
175169

176170
void coro_destroy(TSRMLS_D)
177171
{
178-
if (allocated_return_value_ptr)
179-
{
180-
efree(allocated_return_value_ptr);
181-
}
172+
182173
}
183174

184175
static int sw_terminate_opcode_handler(zend_execute_data *execute_data)
@@ -220,6 +211,11 @@ int sw_coro_create(zend_fcall_info_cache *fci_cache, zval **argv, int argc, zval
220211
swWarn("exceed max number of coro %d", COROG.coro_num);
221212
return CORO_LIMIT;
222213
}
214+
215+
COROG.origin_vm_stack = EG(vm_stack);
216+
COROG.origin_vm_stack_top = EG(vm_stack_top);
217+
COROG.origin_vm_stack_end = EG(vm_stack_end);
218+
223219
zend_function *func;
224220
uint32_t i;
225221

@@ -240,7 +236,7 @@ int sw_coro_create(zend_fcall_info_cache *fci_cache, zval **argv, int argc, zval
240236
coro_frame->return_value = NULL;
241237
coro_frame->prev_execute_data = &dummy_frame;
242238

243-
call = zend_vm_stack_push_call_frame(ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC, func, argc,
239+
call = zend_vm_stack_push_call_frame(ZEND_CALL_TOP_FUNCTION | ZEND_CALL_ALLOCATED, func, argc,
244240
fci_cache->called_scope, fci_cache->object);
245241
#if PHP_MINOR_VERSION < 1
246242
EG(scope) = func->common.scope;
@@ -253,7 +249,7 @@ int sw_coro_create(zend_fcall_info_cache *fci_cache, zval **argv, int argc, zval
253249
ZVAL_COPY(target, argv[i]);
254250
}
255251
call->symbol_table = NULL;
256-
allocated_return_value_ptr = retval;
252+
257253
EG(current_execute_data) = NULL;
258254
if (UNEXPECTED(func->op_array.fn_flags & ZEND_ACC_CLOSURE))
259255
{
@@ -278,7 +274,7 @@ int sw_coro_create(zend_fcall_info_cache *fci_cache, zval **argv, int argc, zval
278274
COROG.root_coro->post_callback_params = params;
279275
COROG.root_coro->has_yield_parent = 0;
280276
COROG.require = 1;
281-
if (COROG.current_coro)
277+
if (COROG.call_stack_size > 1 && COROG.current_coro)
282278
{
283279
COROG.root_coro->origin_coro = COROG.current_coro;
284280
}
@@ -326,7 +322,6 @@ sw_inline php_context *sw_coro_save(zval *return_value, php_context *sw_current_
326322
SWCC(current_vm_stack_top) = EG(vm_stack_top);
327323
SWCC(current_vm_stack_end) = EG(vm_stack_end);
328324
SWCC(current_task) = COROG.current_coro;
329-
SWCC(allocated_return_value_ptr) = allocated_return_value_ptr;
330325
return sw_current_context;
331326
}
332327

@@ -344,7 +339,6 @@ int sw_coro_resume(php_context *sw_current_context, zval *retval, zval *coro_ret
344339
#if PHP_MINOR_VERSION < 1
345340
EG(scope) = EG(current_execute_data)->func->op_array.scope;
346341
#endif
347-
allocated_return_value_ptr = SWCC(allocated_return_value_ptr);
348342
EG(current_execute_data)->opline--;
349343
if (EG(current_execute_data)->opline->result_type != IS_UNUSED)
350344
{
@@ -369,7 +363,6 @@ int sw_coro_resume_parent(php_context *sw_current_context, zval *retval, zval *c
369363
EG(vm_stack_end) = SWCC(current_vm_stack_end);
370364
EG(current_execute_data) = SWCC(current_execute_data);
371365
COROG.current_coro = SWCC(current_task);
372-
allocated_return_value_ptr = SWCC(allocated_return_value_ptr);
373366
return CORO_END;
374367
}
375368

@@ -419,9 +412,8 @@ void sw_interrupt_function(zend_execute_data *execute_data)
419412
else
420413
{
421414
EG(current_execute_data) = COROG.origin_ex;
422-
if (current_coro && current_coro->origin_coro && current_coro->has_yield_parent == 0)
415+
if (current_coro && current_coro->origin_coro && COROG.call_stack_size > 1 && current_coro->has_yield_parent == 0)
423416
{
424-
current_coro->has_yield_parent = 1;
425417
EG(vm_stack) = current_coro->origin_coro->stack;
426418
EG(vm_stack_top) = current_coro->origin_coro->vm_stack_top;
427419
EG(vm_stack_end) = current_coro->origin_coro->vm_stack_end;
@@ -432,6 +424,7 @@ void sw_interrupt_function(zend_execute_data *execute_data)
432424
EG(vm_stack_top) = COROG.origin_vm_stack_top;
433425
EG(vm_stack_end) = COROG.origin_vm_stack_end;
434426
}
427+
current_coro->has_yield_parent = 1;
435428
}
436429
COROG.require = 1;
437430
COROG.next_coro = NULL;

swoole_coroutine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ typedef struct _coro_global
9797
volatile zend_bool pending_interrupt;
9898
zend_bool require;
9999
zend_bool active;
100+
int call_stack_size;
100101
} coro_global;
101102

102103
struct _coro_task

swoole_coroutine_util.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,11 @@ PHP_FUNCTION(swoole_coroutine_create)
292292
php_context *ctx = emalloc(sizeof(php_context));
293293
//coro_save(ctx);
294294
int required = COROG.require;
295+
COROG.call_stack_size++;
295296
int ret = coro_create(func_cache, args, 0, &retval, NULL, NULL);
296297
sw_zval_free(callback);
297298
efree(func_cache);
298-
299+
COROG.call_stack_size--;
299300
if (ret < 0)
300301
{
301302
RETURN_FALSE;

0 commit comments

Comments
 (0)