]> granicus.if.org Git - php/commitdiff
Make rand thread safe when ZTS is defined.
authorSterling Hughes <sterling@php.net>
Sun, 16 Sep 2001 20:49:57 +0000 (20:49 +0000)
committerSterling Hughes <sterling@php.net>
Sun, 16 Sep 2001 20:49:57 +0000 (20:49 +0000)
ext/standard/array.c
ext/standard/basic_functions.h
ext/standard/crypt.c
ext/standard/php_rand.h
ext/standard/rand.c

index 2eded909ae7044c0c483aa0d9158e14e5a43b6cc..3535703e6655bff1e884745198550e759529ec58 100644 (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
  */
index 38a06413eb3c10d2eedebd61ed69d421a8ad3977..afdde1c54dcdd8948ef1933fc95c949d30e9fa54 100644 (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;
index ac619d52efecc0d6aff453661b7dc0c4a4fd1569..3485cdac926c0144916e74f9e2c440ec1756a3bc 100644 (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;
index 7b3b471e402248df86e15b1768a99f5bfd55c537..52dcf38de216e8e8f5764c03e5cb9e81c001335c 100644 (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>          |
    +----------------------------------------------------------------------+
 #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 e68acad46f997ed4395466e53b0a8848f30f29c0..5d0f310064c7d3903db9a16bdb8704ca5d980941 100644 (file)
 #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"
 
 #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)
 
    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);
        }