Skip to content

Commit

Permalink
refactor coroutine iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Nov 22, 2018
1 parent 28a41b4 commit 208ae13
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 101 deletions.
6 changes: 5 additions & 1 deletion include/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "swoole.h"
#include "context.h"

#include <string>
#include <unordered_map>

#define DEFAULT_MAX_CORO_NUM 3000
#define DEFAULT_STACK_SIZE 8192
#define MAX_CORO_NUM_LIMIT 0x80000
Expand All @@ -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,
Expand Down Expand Up @@ -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<long, swoole::Coroutine*>* coroutine_get_map();

9 changes: 5 additions & 4 deletions src/coroutine/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
#include "coroutine.h"
#include "async.h"

#include <stack>
#include <unordered_map>
#include <string>

using namespace swoole;

static struct
Expand Down Expand Up @@ -157,6 +153,11 @@ void* coroutine_get_current_task()
}
}

std::unordered_map<long, Coroutine*>* coroutine_get_map()
{
return &coroutines;
}

long coroutine_get_current_cid()
{
Coroutine* co = coroutine_get_current();
Expand Down
32 changes: 11 additions & 21 deletions swoole_coroutine_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ typedef struct

typedef struct
{
long current_cid;
uint64_t index;
uint64_t count;
std::unordered_map<long, Coroutine*> *_map;
std::unordered_map<long, Coroutine*>::iterator _cursor;
int index;
} coroutine_iterator;

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_void, 0, 0, 0)
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down
152 changes: 77 additions & 75 deletions tests/swoole_coroutine/list_and_backtrace.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,192 +33,194 @@ 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"]=>
string(2) "::"
["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"]=>
string(2) "::"
["args"]=>
array(1) {
[0]=>
int(4)
float(0.001)
}
}
}

0 comments on commit 208ae13

Please sign in to comment.