X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FRandom.cpp;h=87da68b6b5216469591efd17ad68466fd1c00068;hb=0347a79f57ee6e1179c0482adacf3a7e6ff890ff;hp=a189c3fc6789a2269f1a16fabd51a3995a129244;hpb=ce64f0f685111ac24c7a321ea56d0c3524621df1;p=folly.git diff --git a/folly/Random.cpp b/folly/Random.cpp index a189c3fc..87da68b6 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -30,11 +30,12 @@ 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 { @@ -91,16 +92,18 @@ void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) { ptr_ += size; } -ThreadLocal bufferedRandomDevice; } // namespace void Random::secureRandom(void* data, size_t size) { + static ThreadLocal bufferedRandomDevice; bufferedRandomDevice->get(data, size); } -folly::ThreadLocalPtr -ThreadLocalPRNG::localInstance; +ThreadLocalPRNG::ThreadLocalPRNG() { + static folly::ThreadLocal localInstance; + local_ = localInstance.get(); +} class ThreadLocalPRNG::LocalInstancePRNG { public: @@ -109,12 +112,6 @@ class ThreadLocalPRNG::LocalInstancePRNG { 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(); }