diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2017-07-19 19:55:28 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2017-07-19 19:55:28 +0200 |
commit | fd26c5dabc770b1b23ee8a9c4713e8b62be7de67 (patch) | |
tree | afdc52bba2e70a351fc7b00ff6455d403b489b7c | |
parent | 8c1dc840b5c55ce8d533fcabec263afa6cf84ed9 (diff) | |
parent | d25049cc1b74ae445d6521997a421a7462f1ad5b (diff) | |
download | php-git-fd26c5dabc770b1b23ee8a9c4713e8b62be7de67.tar.gz |
Merge branch 'PHP-7.2'
-rw-r--r-- | ext/standard/uniqid.c | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/ext/standard/uniqid.c b/ext/standard/uniqid.c index 22173ae01d..e8f2f19520 100644 --- a/ext/standard/uniqid.c +++ b/ext/standard/uniqid.c @@ -38,17 +38,15 @@ #include "php_lcg.h" #include "uniqid.h" +#ifdef HAVE_GETTIMEOFDAY +ZEND_TLS struct timeval prev_tv = { 0, 0 }; + /* {{{ proto string uniqid([string prefix [, bool more_entropy]]) Generates a unique ID */ -#ifdef HAVE_GETTIMEOFDAY PHP_FUNCTION(uniqid) { char *prefix = ""; -#if defined(__CYGWIN__) - zend_bool more_entropy = 1; -#else zend_bool more_entropy = 0; -#endif zend_string *uniqid; int sec, usec; size_t prefix_len = 0; @@ -60,17 +58,20 @@ PHP_FUNCTION(uniqid) Z_PARAM_BOOL(more_entropy) ZEND_PARSE_PARAMETERS_END(); -#if HAVE_USLEEP && !defined(PHP_WIN32) if (!more_entropy) { -#if defined(__CYGWIN__) - php_error_docref(NULL, E_WARNING, "You must use 'more entropy' under CYGWIN"); - RETURN_FALSE; -#else - usleep(1); -#endif + /* This implementation needs current microsecond to change, + * hence we poll time until it does. This is much faster than + * calling usleep(1) which may cause the kernel to schedule + * another process, causing a pause of around 10ms. + */ + do { + (void)gettimeofday((struct timeval *) &tv, (struct timezone *) NULL); + } while (tv.tv_sec == prev_tv.tv_sec && tv.tv_usec == prev_tv.tv_usec); + + prev_tv.tv_sec = tv.tv_sec; + prev_tv.tv_usec = tv.tv_usec; } -#endif - gettimeofday((struct timeval *) &tv, (struct timezone *) NULL); + sec = (int) tv.tv_sec; usec = (int) (tv.tv_usec % 0x100000); |