Make rand thread safe when ZTS is defined.

This commit is contained in:
Sterling Hughes 2001-09-16 20:49:57 +00:00
parent 96763e8372
commit 97fea0931c
5 changed files with 75 additions and 37 deletions

View File

@ -1377,7 +1377,8 @@ PHP_FUNCTION(range)
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 @@ PHP_FUNCTION(key_exists)
* 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
*/

View File

@ -174,6 +174,8 @@ typedef struct {
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;

View File

@ -87,7 +87,7 @@ extern char *crypt(char *__key, char *__salt);
#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_MINIT_FUNCTION(crypt)
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 @@ PHP_RINIT_FUNCTION(crypt)
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;

View File

@ -15,6 +15,7 @@
| 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> |
+----------------------------------------------------------------------+
@ -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);

View File

@ -25,7 +25,10 @@
#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 @@ PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC)
}
/* }}} */
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 @@ static php_uint32 reloadMT(TSRMLS_D)
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 @@ PHPAPI php_uint32 php_mt_rand(TSRMLS_D)
return y ^ (y >> 18);
}
/* }}} */
#ifdef PHP_WIN32
#define GENERATE_SEED() (time(0) * GetCurrentProcessId() * 1000000 * php_combined_lcg(TSRMLS_C))
@ -215,7 +267,7 @@ PHP_FUNCTION(srand)
if (ZEND_NUM_ARGS() == 0)
seed = GENERATE_SEED();
php_srand(seed);
php_srand(seed TSRMLS_CC);
}
/* }}} */
@ -276,7 +328,8 @@ PHP_FUNCTION(rand)
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);
}