Skip to content

Commit

Permalink
remove constexpr from util/random.h for MSVC compat
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Nathan Bronson committed Nov 11, 2015
1 parent b81b430 commit 505accd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 4 additions & 5 deletions util/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

#include "util/random.h"

#include <pthread.h>
#include <stdint.h>
#include <string.h>
#include <thread>
#include <utility>

#include "port/likely.h"
#include "util/thread_local.h"
Expand All @@ -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::thread::id>()(std::this_thread::get_id());
rv = new (&tls_instance_bytes) Random((uint32_t)seed);
tls_instance = rv;
}
return rv;
Expand Down
10 changes: 7 additions & 3 deletions util/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ 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_;

static uint32_t GoodSeed(uint32_t s) { return (s & M) != 0 ? (s & M) : 1; }

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)) {}

Expand Down

0 comments on commit 505accd

Please sign in to comment.