Skip to content

Commit

Permalink
Improve unordered_map find, fix the task_async test.
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed Nov 26, 2018
1 parent be379be commit a431eaf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/coroutine/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ void* coroutine_get_task_by_cid(long cid)

Coroutine* coroutine_get_by_id(long cid)
{
std::unordered_map<long, Coroutine*>::iterator i = coroutines.find(cid);
if (i == coroutines.end())
auto coroutine_iterator = coroutines.find(cid);
if (coroutine_iterator == coroutines.end())
{
return nullptr;
}
else
{
return i->second;
return coroutine_iterator->second;
}
}

Expand Down
10 changes: 6 additions & 4 deletions swoole_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,16 @@ static void coro_onDNSCompleted(char *domain, swDNSResolver_result *result, void

std::string key(Z_STRVAL_P(req->domain), Z_STRLEN_P(req->domain));
dns_cache *cache;
if (request_cache_map.find(key) == request_cache_map.end())
auto cache_iterator = request_cache_map.find(key);
if (cache_iterator == request_cache_map.end())
{
cache = (dns_cache *) emalloc(sizeof(dns_cache));
request_cache_map[key] = cache;
cache->zaddress = swString_new(20);
}
else
{
cache = request_cache_map[key];
cache = cache_iterator->second;
}

swString_write_ptr(cache->zaddress, 0, Z_STRVAL_P(zaddress), Z_STRLEN_P(zaddress));
Expand Down Expand Up @@ -595,7 +596,8 @@ PHP_FUNCTION(swoole_async_write)

int fd;
std::string key(Z_STRVAL_P(filename), Z_STRLEN_P(filename));
if (open_files.find(key) == open_files.end())
auto file_iterator = open_files.find(key);
if (file_iterator == open_files.end())
{
int open_flag = O_WRONLY | O_CREAT;
if (offset < 0)
Expand All @@ -612,7 +614,7 @@ PHP_FUNCTION(swoole_async_write)
}
else
{
fd = open_files[key];
fd = file_iterator->second;
}

if (offset < 0)
Expand Down
7 changes: 4 additions & 3 deletions swoole_coroutine_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_create, 0, 0, 1)
ZEND_ARG_INFO(0, func)
ZEND_ARG_VARIADIC_INFO(0, params)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_coroutine_exec, 0, 0, 1)
Expand Down Expand Up @@ -446,14 +447,14 @@ static PHP_METHOD(swoole_coroutine_util, resume)
RETURN_FALSE;
}

std::unordered_map<int, Coroutine *>::iterator _i_co = user_yield_coros.find(cid);
if (_i_co == user_yield_coros.end())
auto coroutine_iterator = user_yield_coros.find(cid);
if (coroutine_iterator == user_yield_coros.end())
{
swoole_php_fatal_error(E_WARNING, "you can not resume the coroutine which is in IO operation.");
RETURN_FALSE;
}

Coroutine* co = _i_co->second;
Coroutine* co = coroutine_iterator->second;
user_yield_coros.erase(cid);
co->resume();
RETURN_TRUE;
Expand Down
10 changes: 6 additions & 4 deletions swoole_http_v2_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -818,13 +818,14 @@ int swoole_http2_onFrame(swConnection *conn, swEventData *req)
case SW_HTTP2_TYPE_DATA:
{
swHttp2FrameTraceLog(recv, "data");
if (client->streams.find(stream_id) == client->streams.end())
auto stream_iterator = client->streams.find(stream_id);
if (stream_iterator == client->streams.end())
{
zval_ptr_dtor(zdata);
swoole_error_log(SW_LOG_WARNING, SW_ERROR_HTTP2_STREAM_NOT_FOUND, "http2 stream#%d not found.", stream_id);
return SW_ERR;
}
stream = client->streams[stream_id];
stream = stream_iterator->second;
ctx = stream->ctx;

zrequest_object = ctx->request.zobject;
Expand Down Expand Up @@ -940,11 +941,12 @@ int swoole_http2_onFrame(swConnection *conn, swEventData *req)

void swoole_http2_free(swConnection *conn)
{
if (http2_sessions.find(conn->session_id) == http2_sessions.end())
auto session_iterator = http2_sessions.find(conn->session_id);
if (session_iterator == http2_sessions.end())
{
return;
}
http2_session *client = http2_sessions[conn->session_id];
http2_session *client = session_iterator->second;
http2_sessions.erase(conn->session_id);
delete client;
}
Expand Down
18 changes: 9 additions & 9 deletions swoole_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1107,15 +1107,15 @@ static int php_swoole_onFinish(swServer *serv, swEventData *req)
if (swTask_type(req) & SW_TASK_COROUTINE)
{
int task_id = req->info.fd;
auto _i_task_co = task_coroutine_map.find(task_id);
auto task_co_iterator = task_coroutine_map.find(task_id);

if (_i_task_co == task_coroutine_map.end())
if (task_co_iterator == task_coroutine_map.end())
{
swoole_php_fatal_error(E_WARNING, "task[%d] has expired.", task_id);
_fail: sw_zval_free(zdata);
return SW_OK;
}
swTaskCo *task_co = _i_task_co->second;
swTaskCo *task_co = task_co_iterator->second;
//Server->taskwait
if (task_co->list == NULL)
{
Expand Down Expand Up @@ -1181,14 +1181,14 @@ static int php_swoole_onFinish(swServer *serv, swEventData *req)
zval *callback = NULL;
if (swTask_type(req) & SW_TASK_CALLBACK)
{
auto _i_callback = task_callbacks.find(req->info.fd);
if (_i_callback == task_callbacks.end())
auto callback_iterator = task_callbacks.find(req->info.fd);
if (callback_iterator == task_callbacks.end())
{
swTask_type(req) = swTask_type(req) & (~SW_TASK_CALLBACK);
}
else
{
callback = _i_callback->second;
callback = callback_iterator->second;
}
}
if (callback == NULL)
Expand Down Expand Up @@ -1824,16 +1824,16 @@ static int php_swoole_server_send_resume(swServer *serv, php_context *context, i
void php_swoole_server_send_yield(swServer *serv, int fd, zval *zdata, zval *return_value)
{
list<php_context *> *coros_list;
unordered_map<int, list<php_context *> *>::iterator _i_coros_list = send_coroutine_map.find(fd);
auto coroutine_iterator = send_coroutine_map.find(fd);

if (_i_coros_list == send_coroutine_map.end())
if (coroutine_iterator == send_coroutine_map.end())
{
coros_list = new list<php_context *>;
send_coroutine_map[fd] = coros_list;
}
else
{
coros_list = _i_coros_list->second;
coros_list = coroutine_iterator->second;
}

php_context *context = (php_context *) emalloc(sizeof(php_context));
Expand Down
10 changes: 5 additions & 5 deletions tests/swoole_server/task/task_async.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
swoole_runtime: pdo in task and http response detach
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.inc';
require __DIR__ . '/../../include/skipif.inc';
skip_if_pdo_not_support_mysql8();
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';
require __DIR__ . '/../../include/bootstrap.php';
Swoole\Runtime::enableCoroutine();
$pm = new ProcessManager;
$pm->parentFunc = function (int $pid) use ($pm) {
Expand All @@ -33,10 +33,10 @@ $pm->childFunc = function () use ($pm) {
if (mt_rand(0, 1)) {
$http->task($response->fd);
} else {
$http->task($response->fd, -1, function ($server, $taskId, $fd) {
var_dump($fd);
$http->task($response->fd, -1, function ($server, $taskId, $data) {
list($fd, $data) = $data;
$response = swoole_http_response::create($fd);
$response->end('Hello Swoole!');
$response->end($data);
});
}
});
Expand Down

0 comments on commit a431eaf

Please sign in to comment.