]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/random.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
7 #include "util/random.h"
14 #include "port/likely.h"
15 #include "util/thread_local.h"
17 #ifdef ROCKSDB_SUPPORT_THREAD_LOCAL
18 #define STORAGE_DECL static __thread
20 #define STORAGE_DECL static
23 namespace ROCKSDB_NAMESPACE
{
25 Random
* Random::GetTLSInstance() {
26 STORAGE_DECL Random
* tls_instance
;
27 STORAGE_DECL
std::aligned_storage
<sizeof(Random
)>::type tls_instance_bytes
;
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
);
38 std::string
Random::HumanReadableString(int len
) {
41 for (int i
= 0; i
< len
; ++i
) {
42 ret
[i
] = static_cast<char>('a' + Uniform(26));
47 std::string
Random::RandomString(int len
) {
50 for (int i
= 0; i
< len
; i
++) {
51 ret
[i
] = static_cast<char>(' ' + Uniform(95)); // ' ' .. '~'
56 } // namespace ROCKSDB_NAMESPACE