usleep() on win32

php.internals

Tom Rogers

20 years ago
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

Wez Furlong

20 years ago
We've had working usleep on windows since november 2003 in the PHP 5 branch. Check out win32/time.c --Wez. On 1/27/06, Tom Rogers <trogers@kwikin.com> wrote:

Tom Rogers

20 years ago
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