From 9ea8df1be4de766a61d4a800d909e84e6571e7c9 Mon Sep 17 00:00:00 2001 From: twosee Date: Sat, 24 Nov 2018 16:16:50 +0800 Subject: [PATCH] Improve coroutine state constants. --- gdbinit | 2 +- include/coroutine.h | 2 +- src/coroutine/base.cc | 4 ++-- swoole_coroutine_util.cc | 4 ++++ .../{onSwap/on_swap.phpt => swap_callback/log.phpt} | 8 ++++---- 5 files changed, 12 insertions(+), 8 deletions(-) rename tests/swoole_coroutine/{onSwap/on_swap.phpt => swap_callback/log.phpt} (87%) diff --git a/gdbinit b/gdbinit index bcda7cc0c92..b79523dca25 100644 --- a/gdbinit +++ b/gdbinit @@ -24,7 +24,7 @@ define co_list end if $co->state == 1 color $RED - printf "%s\n", "SW_CORO_YIELD" + printf "%s\n", "SW_CORO_WAITING" color_reset end if $co->state == 2 diff --git a/include/coroutine.h b/include/coroutine.h index ac59964b518..66d881de9ec 100644 --- a/include/coroutine.h +++ b/include/coroutine.h @@ -32,7 +32,7 @@ typedef enum { - SW_CORO_INIT = 0, SW_CORO_YIELD, SW_CORO_RUNNING, SW_CORO_END, + SW_CORO_INIT = 0, SW_CORO_WAITING, SW_CORO_RUNNING, SW_CORO_END, } sw_coro_state; typedef void (*coro_php_create_t)(); diff --git a/src/coroutine/base.cc b/src/coroutine/base.cc index a3a77acd8ac..61de13f79ae 100644 --- a/src/coroutine/base.cc +++ b/src/coroutine/base.cc @@ -54,7 +54,7 @@ long Coroutine::create(coroutine_func_t fn, void* args) void Coroutine::yield() { - state = SW_CORO_YIELD; + state = SW_CORO_WAITING; if (swCoroG.onYield) { swCoroG.onYield(task); @@ -80,7 +80,7 @@ void Coroutine::resume() void Coroutine::yield_naked() { - state = SW_CORO_YIELD; + state = SW_CORO_WAITING; swCoroG.call_stack_size--; ctx.SwapOut(); } diff --git a/swoole_coroutine_util.cc b/swoole_coroutine_util.cc index 7747d1edc20..3dee387aed0 100644 --- a/swoole_coroutine_util.cc +++ b/swoole_coroutine_util.cc @@ -314,6 +314,10 @@ void swoole_coroutine_util_init(int module_number) SWOOLE_DEFINE(DEFAULT_MAX_CORO_NUM); SWOOLE_DEFINE(MAX_CORO_NUM_LIMIT); SWOOLE_DEFINE(MAX_CORO_NESTING_LEVEL); + SWOOLE_DEFINE(CORO_INIT); + SWOOLE_DEFINE(CORO_WAITING); + SWOOLE_DEFINE(CORO_RUNNING); + SWOOLE_DEFINE(CORO_END); //prohibit exit in coroutine INIT_CLASS_ENTRY(swoole_exit_exception_ce, "Swoole\\ExitException", swoole_exit_exception_methods); diff --git a/tests/swoole_coroutine/onSwap/on_swap.phpt b/tests/swoole_coroutine/swap_callback/log.phpt similarity index 87% rename from tests/swoole_coroutine/onSwap/on_swap.phpt rename to tests/swoole_coroutine/swap_callback/log.phpt index 774b1ecd200..30fdd109a87 100644 --- a/tests/swoole_coroutine/onSwap/on_swap.phpt +++ b/tests/swoole_coroutine/swap_callback/log.phpt @@ -8,22 +8,22 @@ require __DIR__ . '/../../include/bootstrap.php'; co::onSwap(function (int $state, int $cid, int $origin_cid) { $cid1 = $cid2 = -1; switch ($state){ - case 0: + case SWOOLE_CORO_INIT: $state = 'create'; $cid1 = $origin_cid; $cid2 = $cid; break; - case 1: + case SWOOLE_CORO_WATING: $state = 'yield'; $cid1 = $cid; $cid2 = $origin_cid; break; - case 2: + case SWOOLE_CORO_RUNNING: $state = 'resume'; $cid1 = $origin_cid; $cid2 = $cid; break; - case 3: + case SWOOLE_CORO_END: $state = 'close'; $cid1 = $cid; $cid2 = $origin_cid;