core: Make global random generator thread-local

Change-Id: Ib17ea05592445cf99af9726ef7799ead813ca0fe
Refs: #2489
This commit is contained in:
Alexander Afanasyev
2015-02-15 11:57:51 -08:00
parent 6b34ab9cec
commit 63df436e5f
2 changed files with 74 additions and 9 deletions
+19 -9
View File
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis
* Copyright (c) 2014-2015, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -24,15 +24,25 @@
*/
#include "random.hpp"
#include <boost/thread/tss.hpp>
namespace nfd {
static boost::random::mt19937 g_rng;
static boost::thread_specific_ptr<boost::random::mt19937> g_rng;
boost::random::mt19937&
getGlobalRng()
{
return g_rng;
if (g_rng.get() == nullptr) {
g_rng.reset(new boost::random::mt19937());
}
return *g_rng;
}
void
resetGlobalRng()
{
g_rng.reset();
}
} // namespace nfd