# usleep() on win32

- Thread: https://externals.io/message/21695
- Original archive: https://news-web.php.net/php.internals/21695
- Started by Tom Rogers on 2006-01-27T01:07:15+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #21695 — Tom Rogers — 2006-01-27T01:07:15+00:00

URL: https://externals.io/message/21695#21695

Hi

Here is a small change to usleep so it will work with win32.
It is very useful for tight while loops when using socket functions
and reduces cpu load from 100% to 0%-2% (on my win2k box) by just
adding usleep(1000); after the while().
(Code snippet was taken from pgsql)

Diff is from php-4.4 cvs

RCS file: /repository/php-src/ext/standard/basic_functions.c,v
retrieving revision 1.543.2.51.2.6
diff -u -w -b -r1.543.2.51.2.6 basic_functions.c
--- basic_functions.c   1 Jan 2006 13:46:57 -0000       1.543.2.51.2.6
+++ basic_functions.c   27 Jan 2006 00:52:10 -0000
@@ -1677,14 +1677,20 @@
    Delay for a given number of micro seconds */
 PHP_FUNCTION(usleep)
 {
-#if HAVE_USLEEP
        pval **num;
 
        if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
                WRONG_PARAM_COUNT;
        }
        convert_to_long_ex(num);
+#if HAVE_USLEEP
        usleep(Z_LVAL_PP(num));
+#else
+#ifdef PHP_WIN32
+       do {
+               SleepEx((Z_LVAL_PP(num) < 500 ? 1 : (Z_LVAL_PP(num) + 500) / 1000), TRUE);
+       } while(0);
+#endif
 #endif
 }
 /* }}} */




-- 

Tom

---

## #21696 — Wez Furlong — 2006-01-27T01:24:33+00:00

URL: https://externals.io/message/21695#21696 · In reply to: #21695

We've had working usleep on windows since november 2003 in the PHP 5 branch.
Check out win32/time.c

--Wez.


> Hi
>
> Here is a small change to usleep so it will work with win32.
> It is very useful for tight while loops when using socket functions
> and reduces cpu load from 100% to 0%-2% (on my win2k box) by just
> adding usleep(1000); after the while().
> (Code snippet was taken from pgsql)
>
> Diff is from php-4.4 cvs
>
> RCS file: /repository/php-src/ext/standard/basic_functions.c,v
> retrieving revision 1.543.2.51.2.6
> diff -u -w -b -r1.543.2.51.2.6 basic_functions.c
> --- basic_functions.c   1 Jan 2006 13:46:57 -0000       1.543.2.51.2.6
> +++ basic_functions.c   27 Jan 2006 00:52:10 -0000
> @@ -1677,14 +1677,20 @@
>     Delay for a given number of micro seconds */
>  PHP_FUNCTION(usleep)
>  {
> -#if HAVE_USLEEP
>         pval **num;
>
>         if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
>                 WRONG_PARAM_COUNT;
>         }
>         convert_to_long_ex(num);
> +#if HAVE_USLEEP
>         usleep(Z_LVAL_PP(num));
> +#else
> +#ifdef PHP_WIN32
> +       do {
> +               SleepEx((Z_LVAL_PP(num) < 500 ? 1 : (Z_LVAL_PP(num) + 500) / 1000), TRUE);
> +       } while(0);
> +#endif
>  #endif
>  }
>  /* }}} */
>
>
>
>
> --
>
> Tom

---

## #21698 — Tom Rogers — 2006-01-27T04:40:15+00:00

URL: https://externals.io/message/21695#21698 · In reply to: #21696

Hi Wez,

Friday, January 27, 2006, 11:24:33 AM, you wrote:
WF> We've had working usleep on windows since november 2003 in the PHP 5 branch.
WF> Check out win32/time.c

WF> --Wez.

Thanks for the pointer, was using php-gtk1 so php5 was out of the
question and a quick look at the same file showed no change to usleep.
Got to look harder next time...

-- 
regards,
Tom
