Skip to content

Commit fe98c1d

Browse files
committed
Co::sleep return value will be the number of seconds left to sleep.
1 parent 669ddf2 commit fe98c1d

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

include/coroutine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Coroutine
103103
static void print_list();
104104

105105
static long create(coroutine_func_t fn, void* args = nullptr);
106-
static bool sleep(double sec);
106+
static double sleep(double sec);
107107
static swString* read_file(const char *file, int lock);
108108
static ssize_t write_file(const char *file, char *buf, size_t length, int lock, int flags);
109109
static std::string gethostbyname(const std::string &hostname, int domain, double timeout = -1);

src/coroutine/base.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool Coroutine::yield(sw_coro_on_swap_t on_cancel, void *data)
7373
canceled = false;
7474
yield();
7575
cancelable = false;
76-
if (canceled)
76+
if (unlikely(canceled))
7777
{
7878
on_cancel(data);
7979
if (this->on_cancel)

src/coroutine/hook.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -641,19 +641,25 @@ static void sleep_timeout_callback(swTimer *timer, swTimer_node *tnode)
641641
((Coroutine *) tnode->data)->resume();
642642
}
643643

644-
bool Coroutine::sleep(double sec)
644+
static void sleep_cancel_callback(void *data)
645+
{
646+
swTimer_node* tnode = (swTimer_node *) data;
647+
swTimer_del(&SwooleG.timer, tnode);
648+
}
649+
650+
double Coroutine::sleep(double sec)
645651
{
646652
Coroutine* co = Coroutine::get_current();
647653
swTimer_node* tnode = swTimer_add(&SwooleG.timer, (long) (sec * 1000), 0, co, sleep_timeout_callback);
648654
if (unlikely(!tnode))
649655
{
650-
return false;
656+
return sec;
657+
}
658+
if (unlikely(!co->yield(sleep_cancel_callback, tnode)))
659+
{
660+
return (tnode->exec_msec - swTimer_get_relative_msec()) / 1000;
651661
}
652-
co->yield([](void *data) {
653-
swTimer_node* tnode = (swTimer_node *) data;
654-
swTimer_del(&SwooleG.timer, tnode);
655-
}, tnode);
656-
return true;
662+
return 0;
657663
}
658664

659665
swString* Coroutine::read_file(const char *file, int lock)

src/network/timer.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
#include "swoole.h"
1818

19-
static int swTimer_init(long msec);
20-
2119
int swTimer_now(struct timeval *time)
2220
{
2321
#if defined(SW_USE_MONOTONIC_TIME) && defined(CLOCK_MONOTONIC)

swoole_coroutine_util.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,12 @@ static PHP_METHOD(swoole_coroutine_util, sleep)
649649
Z_PARAM_DOUBLE(seconds)
650650
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
651651

652-
if (seconds <= 0)
652+
if (UNEXPECTED(seconds <= 0))
653653
{
654654
swoole_php_fatal_error(E_WARNING, "Timer must be greater than 0");
655655
RETURN_FALSE;
656656
}
657-
RETURN_BOOL(Coroutine::sleep(seconds));
657+
RETURN_DOUBLE(Coroutine::sleep(seconds));
658658
}
659659

660660
static void aio_onReadCompleted(swAio_event *event)

swoole_runtime.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ static PHP_FUNCTION(_sleep)
11271127
if (num >= 0.001 && PHPCoroutine::is_in())
11281128
{
11291129
php_swoole_check_reactor();
1130-
RETURN_LONG(Coroutine::sleep((double) num) ? 0 : num);
1130+
RETURN_LONG((long) Coroutine::sleep((double) num));
11311131
}
11321132
else
11331133
{

0 commit comments

Comments
 (0)