Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ bool PHPCoroutine::interrupt_thread_running = false;

static zend_bool* zend_vm_interrupt = nullptr;
static user_opcode_handler_t ori_exit_handler = nullptr;
static user_opcode_handler_t ori_begin_silence_handler = nullptr;
static user_opcode_handler_t ori_end_silence_handler = nullptr;
static unordered_map<long, Coroutine *> user_yield_coros;

static void (*orig_interrupt_function)(zend_execute_data *execute_data) = nullptr;
Expand Down Expand Up @@ -284,6 +286,21 @@ static int coro_exit_handler(zend_execute_data *execute_data)
return ZEND_USER_OPCODE_DISPATCH;
}

static int coro_begin_silence_handler(zend_execute_data *execute_data)
{
php_coro_task *task = PHPCoroutine::get_task();
task->in_silence = true;
task->ori_error_reporting = EG(error_reporting);
return ZEND_USER_OPCODE_DISPATCH;
}

static int coro_end_silence_handler(zend_execute_data *execute_data)
{
php_coro_task *task = PHPCoroutine::get_task();
task->in_silence = false;
return ZEND_USER_OPCODE_DISPATCH;
}

static void coro_interrupt_resume(void *data)
{
Coroutine *co = (Coroutine *) data;
Expand Down Expand Up @@ -510,6 +527,11 @@ inline void PHPCoroutine::save_vm_stack(php_coro_task *task)
memcpy(task->array_walk_fci, &BG(array_walk_fci), sizeof(*task->array_walk_fci));
memset(&BG(array_walk_fci), 0, sizeof(*task->array_walk_fci));
}
if (UNEXPECTED(task->in_silence))
{
task->tmp_error_reporting = EG(error_reporting);
EG(error_reporting) = task->ori_error_reporting;
}
}

inline void PHPCoroutine::restore_vm_stack(php_coro_task *task)
Expand All @@ -532,6 +554,10 @@ inline void PHPCoroutine::restore_vm_stack(php_coro_task *task)
memcpy(&BG(array_walk_fci), task->array_walk_fci, sizeof(*task->array_walk_fci));
task->array_walk_fci->fci.size = 0;
}
if (UNEXPECTED(task->in_silence))
{
EG(error_reporting) = task->tmp_error_reporting;
}
}

inline void PHPCoroutine::save_og(php_coro_task *task)
Expand Down Expand Up @@ -701,18 +727,20 @@ void PHPCoroutine::main_func(void *arg)
EG(exception_class) = NULL;
EG(exception) = NULL;

save_vm_stack(task);
record_last_msec(task);

task->output_ptr = NULL;
task->array_walk_fci = NULL;
task->in_silence = false;

task->co = Coroutine::get_current();
task->co->set_task((void *) task);
task->defer_tasks = nullptr;
task->pcid = task->co->get_origin_cid();
task->context = nullptr;
task->enable_scheduler = 1;

save_vm_stack(task);
record_last_msec(task);

swTraceLog(
SW_TRACE_COROUTINE, "Create coro id: %ld, origin cid: %ld, coro total count: %zu, heap size: %zu",
task->co->get_cid(), task->co->get_origin_cid(), (uintmax_t) Coroutine::count(), (uintmax_t) zend_memory_usage(0)
Expand Down Expand Up @@ -887,6 +915,12 @@ void php_swoole_coroutine_minit(int module_number)
{
ori_exit_handler = zend_get_user_opcode_handler(ZEND_EXIT);
zend_set_user_opcode_handler(ZEND_EXIT, coro_exit_handler);

ori_begin_silence_handler = zend_get_user_opcode_handler(ZEND_BEGIN_SILENCE);
zend_set_user_opcode_handler(ZEND_BEGIN_SILENCE, coro_begin_silence_handler);

ori_end_silence_handler = zend_get_user_opcode_handler(ZEND_END_SILENCE);
zend_set_user_opcode_handler(ZEND_END_SILENCE, coro_end_silence_handler);
}
}

Expand Down
4 changes: 4 additions & 0 deletions swoole_coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ struct php_coro_task
zend_output_globals *output_ptr;
/* for array_walk non-reentrancy */
php_swoole_fci *array_walk_fci;
/* for error control `@` */
bool in_silence;
int ori_error_reporting;
int tmp_error_reporting;
swoole::Coroutine *co;
std::stack<php_swoole_fci *> *defer_tasks;
long pcid;
Expand Down