Skip to content

Commit

Permalink
fix #2745
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 8, 2019
1 parent 64a4f64 commit 1d98608
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/async.h
Expand Up @@ -78,6 +78,7 @@ extern swAsyncIO SwooleAIO;
int swAio_dispatch(const swAio_event *request);
swAio_event* swAio_dispatch2(const swAio_event *request);
int swAio_cancel(int task_id);
size_t swAio_thread_count();

void swAio_handler_read(swAio_event *event);
void swAio_handler_write(swAio_event *event);
Expand Down
27 changes: 23 additions & 4 deletions src/network/async_thread.cc
Expand Up @@ -56,13 +56,13 @@ static int swAio_callback(swReactor *reactor, swEvent *_event)
class async_event_queue
{
public:
bool push(async_event *event)
inline bool push(async_event *event)
{
unique_lock<mutex> lock(_mutex);
_queue.push(event);
return true;
}
async_event* pop()
inline async_event* pop()
{
unique_lock<mutex> lock(_mutex);
if (_queue.empty())
Expand All @@ -73,11 +73,15 @@ class async_event_queue
_queue.pop();
return retval;
}
bool empty()
inline bool empty()
{
unique_lock<mutex> lock(_mutex);
return _queue.empty();
}
inline size_t count()
{
return _queue.size();
}
private:
queue<async_event*> _queue;
mutex _mutex;
Expand Down Expand Up @@ -180,6 +184,16 @@ class async_thread_pool
return _event_copy;
}

inline size_t thread_count()
{
return threads.size();
}

inline size_t queue_count()
{
return queue.count();
}

private:
void create_thread(int i)
{
Expand Down Expand Up @@ -322,13 +336,18 @@ static int swAio_init()

swReactor_add_destroy_callback(SwooleG.main_reactor, swAio_free, nullptr);

pool = new async_thread_pool(SwooleAIO.min_thread_count, SwooleAIO.min_thread_count);
pool = new async_thread_pool(SwooleAIO.min_thread_count, SwooleAIO.max_thread_count);
pool->start();
SwooleAIO.init = 1;

return SW_OK;
}

size_t swAio_thread_count()
{
return pool->thread_count();
}

int swAio_dispatch(const swAio_event *request)
{
if (sw_unlikely(!SwooleAIO.init))
Expand Down
1 change: 1 addition & 0 deletions swoole_coroutine.cc
Expand Up @@ -967,6 +967,7 @@ PHP_METHOD(swoole_coroutine, stats)
add_assoc_long_ex(return_value, ZEND_STRL("signal_listener_num"), SwooleG.main_reactor->signal_listener_num);
}
add_assoc_long_ex(return_value, ZEND_STRL("aio_task_num"), SwooleAIO.task_num);
add_assoc_long_ex(return_value, ZEND_STRL("aio_thread_num"), swAio_thread_count());
add_assoc_long_ex(return_value, ZEND_STRL("c_stack_size"), Coroutine::get_stack_size());
add_assoc_long_ex(return_value, ZEND_STRL("coroutine_num"), Coroutine::count());
add_assoc_long_ex(return_value, ZEND_STRL("coroutine_peak_num"), Coroutine::get_peak_num());
Expand Down
20 changes: 20 additions & 0 deletions tests/swoole_coroutine_system/aio_thread_num.phpt
@@ -0,0 +1,20 @@
--TEST--
swoole_coroutine_system: gethostbyname
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

$sch = new Swoole\Coroutine\Scheduler();
$sch->parallel(16, function () {
$ip = Co::gethostbyname('www.baidu.com');
Assert::assert($ip != false);
});
$sch->add(function () {
Assert::greaterThan(Co::stats()['aio_thread_num'], 8);
});
$sch->start();

?>
--EXPECT--

0 comments on commit 1d98608

Please sign in to comment.