From 6f539ae99e624b59588a0c6fccb6ea7bff7c7412 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Sun, 10 Apr 2005 16:25:11 +0000 Subject: [PATCH] Added time_sleep_until() function, which is a high precision mechanism of making a script sleep until specified timestamp. --- NEWS | 1 + ext/standard/basic_functions.c | 43 ++++++++++++++++++++++++++++++++++ ext/standard/basic_functions.h | 1 + 3 files changed, 45 insertions(+) diff --git a/NEWS b/NEWS index 229e57b4aa..9e673689a2 100644 --- a/NEWS +++ b/NEWS @@ -83,6 +83,7 @@ PHP NEWS . fputcsv() (David Sklar) . posix_access() (Magnus) . htmlspecialchars_decode() (Ilia) + . time_sleep_until() (Ilia) - Added DomDocument::$recover property for parsing not well-formed XML Documents. (Christian) - Added Cursor support for MySQL 5.0.x in mysqli (Georg) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 7a19a64063..556b0e1f84 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -167,6 +167,7 @@ function_entry basic_functions[] = { PHP_FE(usleep, NULL) #if HAVE_NANOSLEEP PHP_FE(time_nanosleep, NULL) + PHP_FE(time_sleep_until, NULL) #endif PHP_FE(time, NULL) PHP_FE(mktime, NULL) @@ -1790,6 +1791,48 @@ PHP_FUNCTION(time_nanosleep) RETURN_FALSE; } /* }}} */ + +/* {{{ proto mixed time_sleep_until(float timestamp) + Make the script sleep until the specified time */ +PHP_FUNCTION(time_sleep_until) +{ + double d_ts, c_ts; + struct timeval tm; + struct timespec php_req, php_rem; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &d_ts)) { + WRONG_PARAM_COUNT; + } + + if (gettimeofday((struct timeval *) &tm, NULL) != 0) { + RETURN_FALSE; + } + + c_ts = (double)(d_ts - tm.tv_sec - tm.tv_usec / 1000000.00); + if (c_ts < 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Sleep until to time is less then current time."); + RETURN_FALSE; + } + + php_req.tv_sec = (time_t) c_ts; + if (php_req.tv_sec > c_ts) { /* rounding up occurred */ + php_req.tv_sec--; + } + /* 1sec = 1000000000 nanoseconds */ + php_req.tv_nsec = (long) ((c_ts - php_req.tv_sec) * 1000000000.00); + + while (nanosleep(&php_req, &php_rem)) { + if (errno == EINTR) { + php_req.tv_sec = php_rem.tv_sec; + php_req.tv_nsec = php_rem.tv_nsec; + } else { + RETURN_FALSE; + } + } + + RETURN_TRUE; +} +/* }}} */ #endif /* {{{ proto string get_current_user(void) diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index c24ad491f4..58e1f024fc 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -50,6 +50,7 @@ PHP_FUNCTION(sleep); PHP_FUNCTION(usleep); #if HAVE_NANOSLEEP PHP_FUNCTION(time_nanosleep); +PHP_FUNCTION(time_sleep_until); #endif PHP_FUNCTION(flush); #ifdef HAVE_INET_NTOP -- 2.50.1