/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "folly/Random.h"
+#include <folly/Random.h>
#include <atomic>
#include <unistd.h>
#include <array>
#include <glog/logging.h>
-#include "folly/File.h"
-#include "folly/FileUtil.h"
+#include <folly/File.h>
+#include <folly/FileUtil.h>
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);
+ // 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);
}
class BufferedRandomDevice {
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;
+ThreadLocalPRNG::ThreadLocalPRNG() {
+ static folly::ThreadLocal<ThreadLocalPRNG::LocalInstancePRNG> localInstance;
+ local_ = localInstance.get();
+}
class ThreadLocalPRNG::LocalInstancePRNG {
public:
Random::DefaultGenerator rng;
};
-ThreadLocalPRNG::LocalInstancePRNG* ThreadLocalPRNG::initLocal() {
- auto ret = new LocalInstancePRNG;
- localInstance.reset(ret);
- return ret;
-}
-
uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {
return local->rng();
}