Create ReadHolder::unlock
[folly.git] / folly / Random.cpp
index a189c3fc6789a2269f1a16fabd51a3995a129244..ef2e8b9cc5d6bc570870614398d1e31111246cee 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <atomic>
 #include <unistd.h>
 #include <sys/time.h>
+#include <mutex>
 #include <random>
 #include <array>
 
 #include <glog/logging.h>
+#include <folly/CallOnce.h>
 #include <folly/File.h>
 #include <folly/FileUtil.h>
+#include <folly/ThreadLocal.h>
+
+#ifdef _MSC_VER
+# include <wincrypt.h>
+#endif
 
 namespace folly {
 
 namespace {
 
-// Keep it open for the duration of the program
-File randomDevice("/dev/urandom");
-
 void readRandomDevice(void* data, size_t size) {
-  PCHECK(readFull(randomDevice.fd(), data, size) == size);
+#ifdef _MSC_VER
+  static folly::once_flag flag;
+  static HCRYPTPROV cryptoProv;
+  folly::call_once(flag, [&] {
+    PCHECK(CryptAcquireContext(&cryptoProv, nullptr, nullptr,
+                               PROV_RSA_FULL, 0));
+  });
+  CHECK(size <= std::numeric_limits<DWORD>::max());
+  PCHECK(CryptGenRandom(cryptoProv, (DWORD)size, (BYTE*)data));
+#else
+  // Keep the random device open for the duration of the program.
+  static int randomFd = ::open("/dev/urandom", O_RDONLY);
+  PCHECK(randomFd >= 0);
+  auto bytesRead = readFull(randomFd, data, size);
+  PCHECK(bytesRead >= 0 && size_t(bytesRead) == size);
+#endif
 }
 
 class BufferedRandomDevice {
@@ -91,17 +110,14 @@ void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) {
   ptr_ += size;
 }
 
-ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
 
 }  // namespace
 
 void Random::secureRandom(void* data, size_t size) {
+  static ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
   bufferedRandomDevice->get(data, size);
 }
 
-folly::ThreadLocalPtr<ThreadLocalPRNG::LocalInstancePRNG>
-ThreadLocalPRNG::localInstance;
-
 class ThreadLocalPRNG::LocalInstancePRNG {
  public:
   LocalInstancePRNG() : rng(Random::create()) { }
@@ -109,10 +125,9 @@ class ThreadLocalPRNG::LocalInstancePRNG {
   Random::DefaultGenerator rng;
 };
 
-ThreadLocalPRNG::LocalInstancePRNG* ThreadLocalPRNG::initLocal() {
-  auto ret = new LocalInstancePRNG;
-  localInstance.reset(ret);
-  return ret;
+ThreadLocalPRNG::ThreadLocalPRNG() {
+  static folly::ThreadLocal<ThreadLocalPRNG::LocalInstancePRNG> localInstance;
+  local_ = localInstance.get();
 }
 
 uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {