Mark QHashSeed::globalSeed() noexcept

It is noexcept, except when initializing. When initializing, let's just
use qEnvironmentVariableIntValue (which we should have used anyway),
which avoids the memory allocation and is noexcept. The QRandomGenerator
functions are not marked noexcept, but are mostly so: they can't throw
regular exceptions, but some implementations do call POSIX Thread
Cancellation Points, which may cause forced stack unwinding. That's
unlikely to happen at the moment of the QHash initialization.

This is also mitigated in the next commit.

Change-Id: Id2983978ad544ff79911fffd1671fd16f8d6378d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Thiago Macieira 2021-04-02 01:26:56 -07:00
parent 7ac0621ad1
commit a24ced7474
2 changed files with 9 additions and 6 deletions

View File

@ -721,15 +721,18 @@ size_t qHash(QLatin1String key, size_t seed) noexcept
/*!
\internal
Note: not \c{noexcept}, but called from a \c{noexcept} function and thus
will cause termination if any of the functions here throw.
*/
static size_t qt_create_qhash_seed()
{
size_t seed = 0;
#ifndef QT_BOOTSTRAPPED
QByteArray envSeed = qgetenv("QT_HASH_SEED");
if (!envSeed.isEmpty()) {
seed = envSeed.toUInt();
bool ok;
seed = qEnvironmentVariableIntValue("QT_HASH_SEED", &ok);
if (ok) {
if (seed) {
// can't use qWarning here (reentrancy)
fprintf(stderr, "QT_HASH_SEED: forced seed value is not 0; ignored.\n");
@ -757,7 +760,7 @@ static QBasicAtomicInteger<size_t> qt_qhash_seed = Q_BASIC_ATOMIC_INITIALIZER(0)
\internal
\threadsafe
Initializes the seed and returns it
Initializes the seed and returns it.
*/
static size_t qt_initialize_qhash_seed()
{
@ -821,7 +824,7 @@ static size_t qt_initialize_qhash_seed()
will be zero if setDeterministicGlobalSeed() has been called or if the
\c{QT_HASH_SEED} environment variable is set to zero.
*/
QHashSeed QHashSeed::globalSeed()
QHashSeed QHashSeed::globalSeed() noexcept
{
size_t seed = qt_qhash_seed.loadRelaxed();
if (Q_UNLIKELY(seed == 0))

View File

@ -73,7 +73,7 @@ struct QHashSeed
constexpr QHashSeed(size_t d = 0) : data(d) {}
constexpr operator size_t() const noexcept { return data; }
static Q_CORE_EXPORT QHashSeed globalSeed() Q_DECL_PURE_FUNCTION;
static Q_CORE_EXPORT QHashSeed globalSeed() noexcept Q_DECL_PURE_FUNCTION;
static Q_CORE_EXPORT void setDeterministicGlobalSeed();
static Q_CORE_EXPORT void resetRandomGlobalSeed();
private: