@@ -1501,7 +1501,7 @@ static void hook_pdo_driver(uint32_t flags) {
15011501 }
15021502 } else {
15031503 if (runtime_hook_flags & PHPCoroutine::HOOK_PDO_FIREBIRD) {
1504- swoole_firebird_set_blocking (true );
1504+ swoole_firebird_set_blocking (true );
15051505 }
15061506 }
15071507#endif
@@ -1809,9 +1809,11 @@ static PHP_FUNCTION(swoole_sleep) {
18091809
18101810static PHP_FUNCTION (swoole_usleep) {
18111811 zend_long num;
1812- if (zend_parse_parameters (ZEND_NUM_ARGS (), " l" , &num) == FAILURE) {
1813- RETURN_FALSE;
1814- }
1812+
1813+ ZEND_PARSE_PARAMETERS_START (1 , 1 )
1814+ Z_PARAM_LONG (num)
1815+ ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
1816+
18151817 if (num < 0 ) {
18161818 php_error_docref (nullptr , E_WARNING, " Number of seconds must be greater than or equal to 0" );
18171819 RETURN_FALSE;
@@ -1826,9 +1828,11 @@ static PHP_FUNCTION(swoole_usleep) {
18261828
18271829static PHP_FUNCTION (swoole_time_nanosleep) {
18281830 zend_long tv_sec, tv_nsec;
1829- if (zend_parse_parameters (ZEND_NUM_ARGS (), " ll" , &tv_sec, &tv_nsec) == FAILURE) {
1830- RETURN_FALSE;
1831- }
1831+
1832+ ZEND_PARSE_PARAMETERS_START (2 , 2 )
1833+ Z_PARAM_LONG (tv_sec)
1834+ Z_PARAM_LONG (tv_nsec)
1835+ ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
18321836
18331837 if (tv_sec < 0 ) {
18341838 php_error_docref (nullptr , E_WARNING, " The seconds value must be greater than 0" );
@@ -1861,41 +1865,34 @@ static PHP_FUNCTION(swoole_time_nanosleep) {
18611865
18621866static PHP_FUNCTION (swoole_time_sleep_until) {
18631867 double d_ts;
1864- timeval tm;
1865- timespec php_req, php_rem;
18661868
1867- if ( zend_parse_parameters ( ZEND_NUM_ARGS (), " d " , &d_ts) == FAILURE) {
1868- RETURN_FALSE;
1869- }
1869+ ZEND_PARSE_PARAMETERS_START ( 1 , 1 )
1870+ Z_PARAM_DOUBLE (d_ts)
1871+ ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
18701872
1871- if (gettimeofday (&tm, nullptr ) != 0 ) {
1873+ if (!std::isfinite (d_ts) || d_ts < 0 ) {
1874+ php_error_docref (nullptr , E_WARNING, " Invalid timestamp: must be a positive finite number" );
18721875 RETURN_FALSE;
18731876 }
18741877
1875- double c_ts = d_ts - tm.tv_sec - tm.tv_usec / 1000000.00 ;
1876- if (c_ts < 0 ) {
1877- php_error_docref (nullptr , E_WARNING, " Sleep until to time is less than current time" );
1878- RETURN_FALSE;
1879- }
1878+ using namespace std ::chrono;
1879+ using dseconds = duration<double >;
1880+
1881+ const auto now = system_clock::now ();
1882+ const auto target_duration = duration_cast<system_clock::duration>(dseconds (d_ts));
1883+ const auto target_time = system_clock::time_point (target_duration);
18801884
1881- php_req. tv_sec = ( time_t ) c_ts;
1882- if (php_req. tv_sec > c_ts) {
1883- php_req. tv_sec -- ;
1885+ if (target_time <= now) {
1886+ php_error_docref ( NULL , E_WARNING, " Argument #1 ($timestamp) must be greater than or equal to the current time " );
1887+ RETURN_FALSE ;
18841888 }
1885- php_req.tv_nsec = (long ) ((c_ts - php_req.tv_sec ) * 1000000000.00 );
18861889
1887- double _time = ( double ) php_req. tv_sec + ( double ) php_req. tv_nsec / 1000000000.00 ;
1890+ const auto sleep_duration = target_time - now ;
18881891 if (Coroutine::get_current ()) {
1889- System::sleep (_time);
1892+ const double sleep_seconds = duration_cast<dseconds>(sleep_duration).count ();
1893+ System::sleep (sleep_seconds);
18901894 } else {
1891- while (nanosleep (&php_req, &php_rem)) {
1892- if (errno == EINTR) {
1893- php_req.tv_sec = php_rem.tv_sec ;
1894- php_req.tv_nsec = php_rem.tv_nsec ;
1895- } else {
1896- RETURN_FALSE;
1897- }
1898- }
1895+ std::this_thread::sleep_until (target_time);
18991896 }
19001897 RETURN_TRUE;
19011898}
0 commit comments