From 505accda38ef76e5eea80bf23ebb6c6ba861bc1a Mon Sep 17 00:00:00 2001 From: Nathan Bronson Date: Tue, 10 Nov 2015 12:50:09 -0800 Subject: [PATCH] remove constexpr from util/random.h for MSVC compat Summary: Scoped anonymous enums seem to be better supported than static constexpr at the moment, so this diff replaces the latter with the former. Also, this diff removes an incorrect inclusion of pthread.h. MSVC build was broken starting with D50439. Test Plan: 1. build 2. observe proper skiplist behavior by absence of pathological slowdown 3. push diff to tmp_try_windows branch to tickle AppVeyor 4. wait for contbuild before committing to master Reviewers: sdong Reviewed By: sdong Subscribers: dhruba Differential Revision: https://reviews.facebook.net/D50517 --- util/random.cc | 9 ++++----- util/random.h | 10 +++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/util/random.cc b/util/random.cc index 37e88e19f..56944773f 100644 --- a/util/random.cc +++ b/util/random.cc @@ -6,9 +6,10 @@ #include "util/random.h" -#include #include #include +#include +#include #include "port/likely.h" #include "util/thread_local.h" @@ -27,10 +28,8 @@ Random* Random::GetTLSInstance() { auto rv = tls_instance; if (UNLIKELY(rv == nullptr)) { - const pthread_t self = pthread_self(); - uint32_t seed = 0; - memcpy(&seed, &self, sizeof(seed)); - rv = new (&tls_instance_bytes) Random(seed); + size_t seed = std::hash()(std::this_thread::get_id()); + rv = new (&tls_instance_bytes) Random((uint32_t)seed); tls_instance = rv; } return rv; diff --git a/util/random.h b/util/random.h index f259db07d..8f90c7675 100644 --- a/util/random.h +++ b/util/random.h @@ -18,8 +18,12 @@ namespace rocksdb { // package. class Random { private: - static constexpr uint32_t M = 2147483647L; // 2^31-1 - static constexpr uint64_t A = 16807; // bits 14, 8, 7, 5, 2, 1, 0 + enum : uint32_t { + M = 2147483647L // 2^31-1 + }; + enum : uint64_t { + A = 16807 // bits 14, 8, 7, 5, 2, 1, 0 + }; uint32_t seed_; @@ -27,7 +31,7 @@ class Random { public: // This is the largest value that can be returned from Next() - static constexpr uint32_t kMaxNext = M; + enum : uint32_t { kMaxNext = M }; explicit Random(uint32_t s) : seed_(GoodSeed(s)) {} -- GitLab