]> granicus.if.org Git - php/commitdiff
Added time_sleep_until() function, which is a high precision mechanism of
authorIlia Alshanetsky <iliaa@php.net>
Sun, 10 Apr 2005 16:25:11 +0000 (16:25 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Sun, 10 Apr 2005 16:25:11 +0000 (16:25 +0000)
making a script sleep until specified timestamp.

NEWS
ext/standard/basic_functions.c
ext/standard/basic_functions.h

diff --git a/NEWS b/NEWS
index 229e57b4aa7ed75af432385b64b8c35eeb6cf49d..9e673689a25c06cfbd24308caac65ab6bfff1948 100644 (file)
--- 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)
index 7a19a6406393b0fc0fd4cbb5436ad0afd90b680d..556b0e1f847e6672d2a36c742bb8556bcbbef0ce 100644 (file)
@@ -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)
index c24ad491f479ba3f70526083a0c98cd12a97f825..58e1f024fcf161b87a14c8476c1141c4d9a3586f 100644 (file)
@@ -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