Remove global init of ThreadLocal in Random
[folly.git] / folly / Random.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/Random.h>
18
19 #include <atomic>
20 #include <unistd.h>
21 #include <sys/time.h>
22 #include <random>
23 #include <array>
24
25 #include <glog/logging.h>
26 #include <folly/File.h>
27 #include <folly/FileUtil.h>
28
29 namespace folly {
30
31 namespace {
32
33 void readRandomDevice(void* data, size_t size) {
34   // Keep it open for the duration of the program
35   static File randomDevice("/dev/urandom");
36   PCHECK(readFull(randomDevice.fd(), data, size) == size);
37 }
38
39 class BufferedRandomDevice {
40  public:
41   static constexpr size_t kDefaultBufferSize = 128;
42
43   explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize);
44
45   void get(void* data, size_t size) {
46     if (LIKELY(size <= remaining())) {
47       memcpy(data, ptr_, size);
48       ptr_ += size;
49     } else {
50       getSlow(static_cast<unsigned char*>(data), size);
51     }
52   }
53
54  private:
55   void getSlow(unsigned char* data, size_t size);
56
57   inline size_t remaining() const {
58     return buffer_.get() + bufferSize_ - ptr_;
59   }
60
61   const size_t bufferSize_;
62   std::unique_ptr<unsigned char[]> buffer_;
63   unsigned char* ptr_;
64 };
65
66 BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize)
67   : bufferSize_(bufferSize),
68     buffer_(new unsigned char[bufferSize]),
69     ptr_(buffer_.get() + bufferSize) {  // refill on first use
70 }
71
72 void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) {
73   DCHECK_GT(size, remaining());
74   if (size >= bufferSize_) {
75     // Just read directly.
76     readRandomDevice(data, size);
77     return;
78   }
79
80   size_t copied = remaining();
81   memcpy(data, ptr_, copied);
82   data += copied;
83   size -= copied;
84
85   // refill
86   readRandomDevice(buffer_.get(), bufferSize_);
87   ptr_ = buffer_.get();
88
89   memcpy(data, ptr_, size);
90   ptr_ += size;
91 }
92
93
94 }  // namespace
95
96 void Random::secureRandom(void* data, size_t size) {
97   static ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
98   bufferedRandomDevice->get(data, size);
99 }
100
101 ThreadLocalPRNG::ThreadLocalPRNG() {
102   static folly::ThreadLocal<ThreadLocalPRNG::LocalInstancePRNG> localInstance;
103   local_ = localInstance.get();
104 }
105
106 class ThreadLocalPRNG::LocalInstancePRNG {
107  public:
108   LocalInstancePRNG() : rng(Random::create()) { }
109
110   Random::DefaultGenerator rng;
111 };
112
113 uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {
114   return local->rng();
115 }
116
117 }