diff --git a/include/coroutine.h b/include/coroutine.h index 62e6745edb2..a4e6a200045 100644 --- a/include/coroutine.h +++ b/include/coroutine.h @@ -19,6 +19,9 @@ #include "swoole.h" #include "context.h" +#include +#include + #define DEFAULT_MAX_CORO_NUM 3000 #define DEFAULT_STACK_SIZE 8192 #define MAX_CORO_NUM_LIMIT 0x80000 @@ -27,7 +30,6 @@ #define CORO_LIMIT -1 #define CORO_INVALID -2 - typedef enum { SW_CORO_INIT = 0, SW_CORO_YIELD, SW_CORO_RUNNING, SW_CORO_END, @@ -99,3 +101,5 @@ void coroutine_set_onClose(coro_php_close_t func); void internal_coro_yield(void *arg); void internal_coro_resume(void *arg); +std::unordered_map* coroutine_get_map(); + diff --git a/src/coroutine/base.cc b/src/coroutine/base.cc index 7b825bad3e6..434a3e8dec5 100644 --- a/src/coroutine/base.cc +++ b/src/coroutine/base.cc @@ -17,10 +17,6 @@ #include "coroutine.h" #include "async.h" -#include -#include -#include - using namespace swoole; static struct @@ -157,6 +153,11 @@ void* coroutine_get_current_task() } } +std::unordered_map* coroutine_get_map() +{ + return &coroutines; +} + long coroutine_get_current_cid() { Coroutine* co = coroutine_get_current(); diff --git a/swoole_coroutine_util.cc b/swoole_coroutine_util.cc index db7b901a6bc..11712fa8fbb 100644 --- a/swoole_coroutine_util.cc +++ b/swoole_coroutine_util.cc @@ -42,9 +42,9 @@ typedef struct typedef struct { - long current_cid; - uint64_t index; - uint64_t count; + std::unordered_map *_map; + std::unordered_map::iterator _cursor; + int index; } coroutine_iterator; ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_void, 0, 0, 0) @@ -1317,39 +1317,28 @@ static PHP_METHOD(swoole_coroutine_util, getBackTrace) static PHP_METHOD(swoole_coroutine_iterator, rewind) { coroutine_iterator *itearator = (coroutine_iterator *) swoole_get_object(getThis()); - bzero(itearator, sizeof(coroutine_iterator)); - itearator->count = COROG.coro_num; + itearator->_cursor = itearator->_map->begin(); + itearator->index = 0; } static PHP_METHOD(swoole_coroutine_iterator, valid) { coroutine_iterator *itearator = (coroutine_iterator *) swoole_get_object(getThis()); - long cid = itearator->current_cid; - - // FIXME: new coroutine mode - for (; itearator->count > 0 && cid < MAX_CORO_NUM_LIMIT + 1; cid++) - { - if (coroutine_get_by_id(cid)) - { - itearator->current_cid = cid; - itearator->index++; - itearator->count--; - RETURN_TRUE; - } - } - RETURN_FALSE; + RETURN_BOOL(itearator->_cursor != itearator->_map->end()); } static PHP_METHOD(swoole_coroutine_iterator, current) { coroutine_iterator *itearator = (coroutine_iterator *) swoole_get_object(getThis()); - RETURN_LONG(itearator->current_cid); + Coroutine *co = itearator->_cursor->second; + RETURN_LONG(co->cid); } static PHP_METHOD(swoole_coroutine_iterator, next) { coroutine_iterator *itearator = (coroutine_iterator *) swoole_get_object(getThis()); - itearator->current_cid++; + itearator->_cursor++; + itearator->index++; } PHP_METHOD(swoole_coroutine_iterator, key) @@ -1375,6 +1364,7 @@ static PHP_METHOD(swoole_coroutine_util, listCoroutines) object_init_ex(return_value, swoole_coroutine_iterator_class_entry_ptr); coroutine_iterator *itearator = (coroutine_iterator *) emalloc(sizeof(coroutine_iterator)); bzero(itearator, sizeof(coroutine_iterator)); + itearator->_map = coroutine_get_map(); swoole_set_object(return_value, itearator); } diff --git a/tests/swoole_coroutine/list_and_backtrace.phpt b/tests/swoole_coroutine/list_and_backtrace.phpt index 9cb971c0ea3..f7f7bfb6079 100644 --- a/tests/swoole_coroutine/list_and_backtrace.phpt +++ b/tests/swoole_coroutine/list_and_backtrace.phpt @@ -33,142 +33,141 @@ go(function () { }); }); }); +swoole_event_wait(); ?> --EXPECTF-- -int(1) +int(9) array(1) { [0]=> - array(4) { + array(6) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(29) + int(25) ["function"]=> - string(2) "go" + string(6) "resume" + ["class"]=> + string(16) "Swoole\Coroutine" + ["type"]=> + string(2) "::" ["args"]=> array(1) { [0]=> - object(Closure)#2 (0) { - } + int(4) } } } -int(2) +int(8) array(1) { [0]=> - array(4) { + array(6) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(28) + int(22) ["function"]=> - string(2) "go" + string(11) "getaddrinfo" + ["class"]=> + string(16) "Swoole\Coroutine" + ["type"]=> + string(2) "::" ["args"]=> array(1) { [0]=> - object(Closure)#3 (0) { - } + string(9) "localhost" } } } -int(3) +int(7) array(1) { [0]=> - array(4) { + array(6) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(27) + int(19) ["function"]=> - string(2) "go" + string(8) "readFile" + ["class"]=> + string(16) "Swoole\Coroutine" + ["type"]=> + string(2) "::" ["args"]=> array(1) { [0]=> - object(Closure)#6 (1) { - ["static"]=> - array(1) { - ["main"]=> - int(4) - } - } + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" } } } -int(4) +int(2) array(1) { [0]=> - array(6) { + array(4) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(11) + int(28) ["function"]=> - string(12) "getBackTrace" - ["class"]=> - string(16) "Swoole\Coroutine" - ["type"]=> - string(2) "::" + string(2) "go" ["args"]=> array(1) { [0]=> - int(4) + object(Closure)#3 (0) { + } } } } -int(5) +int(1) array(1) { [0]=> array(4) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(26) + int(29) ["function"]=> string(2) "go" ["args"]=> array(1) { [0]=> - object(Closure)#10 (1) { - ["static"]=> - array(1) { - ["main"]=> - int(4) - } + object(Closure)#2 (0) { } } } } -int(6) +int(3) array(1) { [0]=> - array(6) { + array(4) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(16) + int(27) ["function"]=> - string(5) "sleep" - ["class"]=> - string(16) "Swoole\Coroutine" - ["type"]=> - string(2) "::" + string(2) "go" ["args"]=> array(1) { [0]=> - float(0.001) + object(Closure)#6 (1) { + ["static"]=> + array(1) { + ["main"]=> + int(4) + } + } } } } -int(7) +int(4) array(1) { [0]=> array(6) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(19) + int(11) ["function"]=> - string(8) "readFile" + string(12) "getBackTrace" ["class"]=> string(16) "Swoole\Coroutine" ["type"]=> @@ -176,41 +175,43 @@ array(1) { ["args"]=> array(1) { [0]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + int(4) } } } -int(8) +int(5) array(1) { [0]=> - array(6) { + array(4) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(22) + int(26) ["function"]=> - string(11) "getaddrinfo" - ["class"]=> - string(16) "Swoole\Coroutine" - ["type"]=> - string(2) "::" + string(2) "go" ["args"]=> array(1) { [0]=> - string(9) "localhost" + object(Closure)#10 (1) { + ["static"]=> + array(1) { + ["main"]=> + int(4) + } + } } } } -int(9) +int(6) array(1) { [0]=> array(6) { ["file"]=> - string(%d) "%s/tests/swoole_coroutine/list_and_backtrace.php" + string(72) "/home/htf/workspace/swoole/tests/swoole_coroutine/list_and_backtrace.php" ["line"]=> - int(25) + int(16) ["function"]=> - string(6) "resume" + string(5) "sleep" ["class"]=> string(16) "Swoole\Coroutine" ["type"]=> @@ -218,7 +219,8 @@ array(1) { ["args"]=> array(1) { [0]=> - int(4) + float(0.001) } } } +