diff options
author | Ilia Alshanetsky <iliaa@php.net> | 2005-04-10 16:25:11 +0000 |
---|---|---|
committer | Ilia Alshanetsky <iliaa@php.net> | 2005-04-10 16:25:11 +0000 |
commit | 6f539ae99e624b59588a0c6fccb6ea7bff7c7412 (patch) | |
tree | 9965e07373b61b6391fa1beddea4443cd24cf855 /ext | |
parent | de3a7cecc94ea04e912e0f76777cea73eb8c620a (diff) | |
download | php-git-6f539ae99e624b59588a0c6fccb6ea7bff7c7412.tar.gz |
Added time_sleep_until() function, which is a high precision mechanism of
making a script sleep until specified timestamp.
Diffstat (limited to 'ext')
-rw-r--r-- | ext/standard/basic_functions.c | 43 | ||||
-rw-r--r-- | ext/standard/basic_functions.h | 1 |
2 files changed, 44 insertions, 0 deletions
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 |