3131#include "ext/standard/info.h"
3232#include "ext/session/php_session.h"
3333#include "zend_exceptions.h"
34+ #include "zend_hrtime.h"
3435#include "zend_ini.h"
3536#include "zend_operators.h"
3637#include "ext/standard/php_dns.h"
@@ -1206,8 +1207,9 @@ PHP_FUNCTION(time_sleep_until)
12061207{
12071208 double target_secs ;
12081209 struct timeval tm ;
1209- struct timespec php_req , php_rem ;
1210+ struct timespec php_req ;
12101211 uint64_t current_ns , target_ns , diff_ns ;
1212+ zend_hrtime_t start ;
12111213 const uint64_t ns_per_sec = 1000000000 ;
12121214 const double top_target_sec = (double )(UINT64_MAX / ns_per_sec );
12131215
@@ -1218,6 +1220,7 @@ PHP_FUNCTION(time_sleep_until)
12181220 if (gettimeofday ((struct timeval * ) & tm , NULL ) != 0 ) {
12191221 RETURN_FALSE ;
12201222 }
1223+ start = zend_hrtime ();
12211224
12221225 if (UNEXPECTED (!(target_secs >= 0 && target_secs <= top_target_sec ))) {
12231226 zend_argument_value_error (1 , "must be between 0 and %" PRIu64 , (uint64_t )top_target_sec );
@@ -1235,13 +1238,23 @@ PHP_FUNCTION(time_sleep_until)
12351238 php_req .tv_sec = (time_t ) (diff_ns / ns_per_sec );
12361239 php_req .tv_nsec = (long ) (diff_ns % ns_per_sec );
12371240
1238- while (nanosleep (& php_req , & php_rem )) {
1239- if (errno == EINTR ) {
1240- php_req .tv_sec = php_rem .tv_sec ;
1241- php_req .tv_nsec = php_rem .tv_nsec ;
1242- } else {
1241+ while (nanosleep (& php_req , NULL )) {
1242+ if (errno != EINTR ) {
12431243 RETURN_FALSE ;
12441244 }
1245+ if (UNEXPECTED (atomic_load (& EG (vm_interrupt )))) {
1246+ zend_fcall_interrupt (execute_data );
1247+ if (UNEXPECTED (EG (exception ))) {
1248+ RETURN_THROWS ();
1249+ }
1250+ }
1251+ zend_hrtime_t elapsed = zend_hrtime () - start ;
1252+ if (elapsed >= diff_ns ) {
1253+ break ;
1254+ }
1255+ uint64_t remaining_ns = diff_ns - elapsed ;
1256+ php_req .tv_sec = (time_t ) (remaining_ns / ns_per_sec );
1257+ php_req .tv_nsec = (long ) (remaining_ns % ns_per_sec );
12451258 }
12461259
12471260 RETURN_TRUE ;
0 commit comments