diff options
author | ninerider <qt-info@nokia.com> | 2009-10-22 14:50:46 (GMT) |
---|---|---|
committer | ninerider <qt-info@nokia.com> | 2009-10-22 14:50:46 (GMT) |
commit | 8a64af9c24c5c275cba22240760d9239d4b3fd6f (patch) | |
tree | 79c5c3ab3c10435604634a1eb4cc082640ee04d6 /src/corelib | |
parent | a96c204078122b8dd06ac5bb4d49b76f87f686f5 (diff) | |
download | Qt-8a64af9c24c5c275cba22240760d9239d4b3fd6f.zip Qt-8a64af9c24c5c275cba22240760d9239d4b3fd6f.tar.gz Qt-8a64af9c24c5c275cba22240760d9239d4b3fd6f.tar.bz2 |
Changed qsrand() behavior for Windows to match the linux version
A problem occurred related to the createUUid function on Windows Mobile.
Calling rand() before srand() resulted in identical pseudo random
sequences for different threads.
Reviewed-by: Joerg
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/global/qglobal.cpp | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 5a7b559..7d47944 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -2479,7 +2479,7 @@ bool qputenv(const char *varName, const QByteArray& value) #endif } -#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && !defined(Q_OS_SYMBIAN) +#if (defined(Q_OS_UNIX) || defined(Q_OS_WIN)) && !defined(QT_NO_THREAD) && !defined(Q_OS_SYMBIAN) # if defined(Q_OS_INTEGRITY) && defined(__GHS_VERSION_NUMBER) && (__GHS_VERSION_NUMBER < 500) // older versions of INTEGRITY used a long instead of a uint for the seed. @@ -2535,20 +2535,35 @@ void qsrand(uint seed) */ void qsrand() { -#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && !defined(Q_OS_SYMBIAN) +#if (defined(Q_OS_UNIX) || defined(Q_OS_WIN)) && !defined(QT_NO_THREAD) && !defined(Q_OS_SYMBIAN) SeedStorageType *pseed = randTLS()->localData(); if (pseed) { // already seeded return; } randTLS()->setLocalData(pseed = new SeedStorageType); - static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(0); + // start beyond 1 to avoid the sequence reset + static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(2); *pseed = QDateTime::currentDateTime().toTime_t() + quintptr(&pseed) + serial.fetchAndAddRelaxed(1); -#else - // On Windows, we assume that rand() already does the right thing +#if defined(Q_OS_WIN) + // for Windows the srand function must still be called. + srand(*pseed); #endif + +#elif defined(Q_OS_WIN) + static unsigned int seed = 0; + + if (seed) + return; + + seed = GetTickCount(); + srand(seed); +#else + // Symbian? + +#endif // defined(Q_OS_UNIX) || defined(Q_OS_WIN)) && !defined(QT_NO_THREAD) && !defined(Q_OS_SYMBIAN) } /*! |