]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/random.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / util / random.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
5 //
6
7 #include "util/random.h"
8
9 #include <stdint.h>
10 #include <string.h>
11 #include <thread>
12 #include <utility>
13
14 #include "port/likely.h"
15 #include "util/thread_local.h"
16
17 #ifdef ROCKSDB_SUPPORT_THREAD_LOCAL
18 #define STORAGE_DECL static __thread
19 #else
20 #define STORAGE_DECL static
21 #endif
22
23 namespace rocksdb {
24
25 Random* Random::GetTLSInstance() {
26 STORAGE_DECL Random* tls_instance;
27 STORAGE_DECL std::aligned_storage<sizeof(Random)>::type tls_instance_bytes;
28
29 auto rv = tls_instance;
30 if (UNLIKELY(rv == nullptr)) {
31 size_t seed = std::hash<std::thread::id>()(std::this_thread::get_id());
32 rv = new (&tls_instance_bytes) Random((uint32_t)seed);
33 tls_instance = rv;
34 }
35 return rv;
36 }
37
38 } // namespace rocksdb