From a0fffeed6fceb8244328b649a3f6feb520493bc2 Mon Sep 17 00:00:00 2001 From: "Bradley T. Hughes" Date: Mon, 12 Jul 2010 14:18:29 +0200 Subject: Fix regression in tst_qrand::testqrand() qrand() has seeded with a default value of 1 for quite a long time, and is checked by the test mentioned above. The previous commit to change the default seed value must be reverted to keep compatibility. Change qrand() and qsrand() back to the way they were in 4.5. This fixes the qrand() regression. Change QUuid::createUuid() to seed exactly once per thread, which is a change from 4.5, where QUuid would see only once per application. This solves the original bug, QTBUG-3543, where multiple threads would generate the same UUID sequences. This also fixes the regression reported in QTBUG-11213, where seeding did not happen in certain cases. Reviewed-by: Prasanth Ullattil --- src/corelib/global/qglobal.cpp | 9 +-------- src/corelib/plugin/quuid.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index b24ba38..12745e9 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -2609,14 +2609,7 @@ int qrand() SeedStorageType *pseed = seedStorage->localData(); if (!pseed) { seedStorage->setLocalData(pseed = new SeedStorageType); - - // Seed the PRNG, but only if it has not already been seeded. The - // default seed is a combination of current time, a stack address - // and a serial counter (since thread stack addresses are re-used). - static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(2); - *pseed = QDateTime::currentDateTime().toTime_t() - + quintptr(&pseed) - + serial.fetchAndAddRelaxed(1); + *pseed = 1; } return rand_r(pseed); } else { diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index f48cc2e..d0c59a4 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -577,6 +577,7 @@ QUuid QUuid::createUuid() QT_BEGIN_INCLUDE_NAMESPACE #include "qdatetime.h" +#include "qthreadstorage.h" #include // for RAND_MAX QT_END_INCLUDE_NAMESPACE @@ -591,6 +592,19 @@ QUuid QUuid::createUuid() randbits = r; } + static QThreadStorage uuidseed; + if (!uuidseed.hasLocalData()) { + // Seed the PRNG once per thread with a combination of current time, a + // stack address and a serial counter (since thread stack addresses are + // re-used). + int *pseed = new int; + static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(2); + qsrand(*pseed = QDateTime::currentDateTime().toTime_t() + + quintptr(&pseed) + + serial.fetchAndAddRelaxed(1)); + uuidseed.setLocalData(pseed); + } + QUuid result; uint *data = &(result.data1); int chunks = 16 / sizeof(uint); -- cgit v0.12