cvs: php4 /ext/standard array.c basic_functions.h crypt.c php_rand.h rand.c

From: Date: Sun, 16 Sep 2001 20:49:58 +0000
Subject: cvs: php4 /ext/standard array.c basic_functions.h crypt.c php_rand.h rand.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7434@lists.php.net to get a copy of this message
sterling Sun Sep 16 16:49:58 2001 EDT Modified files: /php4/ext/standard array.c basic_functions.h crypt.c php_rand.h rand.c Log: Make rand thread safe when ZTS is defined. Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.135 php4/ext/standard/array.c:1.136 --- php4/ext/standard/array.c:1.135 Sun Sep 9 09:29:13 2001 +++ php4/ext/standard/array.c Sun Sep 16 16:49:57 2001 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: array.c,v 1.135 2001/09/09 13:29:13 derick Exp $ */ +/* $Id: array.c,v 1.136 2001/09/16 20:49:57 sterling Exp $ */ #include "php.h" #include "php_ini.h" @@ -1377,7 +1377,8 @@ static int array_data_shuffle(const void *a, const void*b) { - return (php_rand() % 2) ? 1 : -1; + TSRMLS_FETCH(); + return (php_rand(TSRMLS_C) % 2) ? 1 : -1; } @@ -3176,6 +3177,6 @@ * tab-width: 4 * c-basic-offset: 4 * End: - * vim600: sw=4 ts=4 fdm=marker - * vim<600: sw=4 ts=4 + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 */ Index: php4/ext/standard/basic_functions.h diff -u php4/ext/standard/basic_functions.h:1.93 php4/ext/standard/basic_functions.h:1.94 --- php4/ext/standard/basic_functions.h:1.93 Wed Sep 5 15:47:55 2001 +++ php4/ext/standard/basic_functions.h Sun Sep 16 16:49:57 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.h,v 1.93 2001/09/05 19:47:55 sniper Exp $ */ +/* $Id: basic_functions.h,v 1.94 2001/09/16 20:49:57 sterling Exp $ */ #ifndef BASIC_FUNCTIONS_H #define BASIC_FUNCTIONS_H @@ -174,6 +174,8 @@ php_uint32 *next; /* next random value is computed from here */ int left; /* can *next++ this many times before reloading */ + unsigned int rand_seed; /* Seed for rand() */ + /* syslog.c */ int syslog_started; char *syslog_device; Index: php4/ext/standard/crypt.c diff -u php4/ext/standard/crypt.c:1.50 php4/ext/standard/crypt.c:1.51 --- php4/ext/standard/crypt.c:1.50 Sun Sep 9 09:29:15 2001 +++ php4/ext/standard/crypt.c Sun Sep 16 16:49:57 2001 @@ -17,7 +17,7 @@ | Rasmus Lerdorf <rasmus@lerdorf.on.ca> | +----------------------------------------------------------------------+ */ -/* $Id: crypt.c,v 1.50 2001/09/09 13:29:15 derick Exp $ */ +/* $Id: crypt.c,v 1.51 2001/09/16 20:49:57 sterling Exp $ */ #include <stdlib.h> #include "php.h" @@ -87,7 +87,7 @@ #endif -#define PHP_CRYPT_RAND php_rand() +#define PHP_CRYPT_RAND php_rand(TSRMLS_C) static int php_crypt_rand_seeded=0; @@ -106,7 +106,7 @@ PHP_RINIT_FUNCTION(crypt) { if(!php_crypt_rand_seeded) { - php_srand(time(0) * getpid() * (php_combined_lcg(TSRMLS_C) * 10000.0)); + php_srand(time(0) * getpid() * (php_combined_lcg(TSRMLS_C) * 10000.0) TSRMLS_CC); php_crypt_rand_seeded=1; } return SUCCESS; @@ -115,7 +115,8 @@ static unsigned char itoa64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; -static void php_to64(char *s, long v, int n) { +static void php_to64(char *s, long v, int n) +{ while (--n >= 0) { *s++ = itoa64[v&0x3f]; v >>= 6; Index: php4/ext/standard/php_rand.h diff -u php4/ext/standard/php_rand.h:1.15 php4/ext/standard/php_rand.h:1.16 --- php4/ext/standard/php_rand.h:1.15 Sun Sep 9 06:44:12 2001 +++ php4/ext/standard/php_rand.h Sun Sep 16 16:49:57 2001 @@ -15,11 +15,12 @@ | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> | | Zeev Suraski <zeev@zend.com> | | Pedro Melo <melo@ip.pt> | + | Sterling Hughes <sterling@php.net> | | | | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ -/* $Id: php_rand.h,v 1.15 2001/09/09 10:44:12 sterling Exp $ */ +/* $Id: php_rand.h,v 1.16 2001/09/16 20:49:57 sterling Exp $ */ #ifndef PHP_RAND_H #define PHP_RAND_H @@ -38,31 +39,11 @@ #define PHP_RAND_MAX RAND_MAX #endif -/* Define rand Function wrapper */ -#ifdef HAVE_RANDOM -#define php_rand() random() -#else -#ifdef HAVE_LRAND48 -#define php_rand() lrand48() -#else -#define php_rand() rand() -#endif -#endif - -/* Define srand Function wrapper */ -#ifdef HAVE_SRANDOM -#define php_srand(seed) srandom((unsigned int)seed) -#else -#ifdef HAVE_SRAND48 -#define php_srand(seed) srand48((long)seed) -#else -#define php_srand(seed) srand((unsigned int)seed) -#endif -#endif - /* MT Rand */ -#define PHP_MT_RAND_MAX ((long)(0x7FFFFFFF)) /* (1<<31) - 1 */ +#define PHP_MT_RAND_MAX ((long) (0x7FFFFFFF)) /* (1<<31) - 1 */ +PHPAPI void php_srand(long seed TSRMLS_DC); +PHPAPI long php_rand(TSRMLS_D); PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC); PHPAPI php_uint32 php_mt_rand(TSRMLS_D); Index: php4/ext/standard/rand.c diff -u php4/ext/standard/rand.c:1.53 php4/ext/standard/rand.c:1.54 --- php4/ext/standard/rand.c:1.53 Sun Sep 16 13:37:19 2001 +++ php4/ext/standard/rand.c Sun Sep 16 16:49:57 2001 @@ -20,12 +20,15 @@ | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ -/* $Id: rand.c,v 1.53 2001/09/16 17:37:19 sterling Exp $ */ +/* $Id: rand.c,v 1.54 2001/09/16 20:49:57 sterling Exp $ */ #include <stdlib.h> #ifdef PHP_WIN32 -#include <windows.h> +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include <windows.h> #endif #include "php.h" @@ -35,6 +38,52 @@ #include "basic_functions.h" + +/* SYSTEM RAND FUNCTIONS */ + +/* {{{ php_srand + */ +PHPAPI void php_srand(long seed TSRMLS_DC) +{ +#ifdef ZTS + BG(rand_seed) = (unsigned int) seed; +#else +# if defined(HAVE_SRANDOM) + srandom((unsigned int) seed); +# elif defined(HAVE_SRAND48) + srand48(seed); +# else + srand((unsigned int) seed); +# endif +#endif +} +/* }}} */ + +/* {{{ php_rand + */ +PHPAPI long php_rand(TSRMLS_D) +{ + long ret; + +#ifdef ZTS + ret = php_rand_r(&BG(rand_seed)); +#else +# if defined(HAVE_RANDOM) + ret = random(); +# elif defined(HAVE_LRAND48) + ret = lrand48(); +# else + ret = rand(); +# endif +#endif + + return ret; +} +/* }}} */ + + +/* MT RAND FUNCTIONS */ + /* This is the ``Mersenne Twister'' random number generator MT19937, which generates pseudorandom integers uniformly distributed in 0..(2^32 - 1) @@ -88,8 +137,6 @@ Melo: we should put some ifdefs here to catch those alphas... */ - - #define N MT_N /* length of state vector */ #define M (397) /* a period parameter */ #define K (0x9908B0DFU) /* a magic constant */ @@ -157,7 +204,9 @@ } /* }}} */ -static php_uint32 reloadMT(TSRMLS_D) +/* {{{ php_mt_reload + */ +static php_uint32 php_mt_reload(TSRMLS_D) { register php_uint32 *p0 = BG(state), *p2 = BG(state) + 2, *pM = BG(state) + M, s0, s1; register int j; @@ -180,14 +229,16 @@ return s1 ^ (s1 >> 18); } +/* }}} */ - +/* {{{ php_mt_rand + */ PHPAPI php_uint32 php_mt_rand(TSRMLS_D) { php_uint32 y; if (--BG(left) < 0) - return reloadMT(TSRMLS_C); + return php_mt_reload(TSRMLS_C); y = *BG(next)++; y ^= (y >> 11); @@ -196,6 +247,7 @@ return y ^ (y >> 18); } +/* }}} */ #ifdef PHP_WIN32 #define GENERATE_SEED() (time(0) * GetCurrentProcessId() * 1000000 * php_combined_lcg(TSRMLS_C)) @@ -215,7 +267,7 @@ if (ZEND_NUM_ARGS() == 0) seed = GENERATE_SEED(); - php_srand(seed); + php_srand(seed TSRMLS_CC); } /* }}} */ @@ -276,7 +328,8 @@ if (argc != 0 && zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE) return; - number = php_rand(); + number = php_rand(TSRMLS_C); + if (argc == 2) { RAND_RANGE(number, min, max, PHP_RAND_MAX); }

« previous php.cvs (#7434) next »