fixed race condition in random number generator

This commit is contained in:
Arvid Norberg 2018-06-03 19:33:00 +02:00 committed by Arvid Norberg
parent 2408200475
commit 049d867c12
2 changed files with 13 additions and 12 deletions

View File

@ -1,4 +1,5 @@
* fixed race condition in random number generator
* fix race condition in stat_cache (disk storage)
* improve error handling of failing to change file priority
The API for custom storage implementations was altered

View File

@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/random.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/thread.hpp"
#ifdef BOOST_NO_CXX11_HDR_RANDOM
#include "libtorrent/aux_/disable_warnings_push.hpp"
@ -46,10 +47,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <random>
#endif
#if !TORRENT_THREADSAFE_STATIC
#include "libtorrent/thread.hpp"
#endif
namespace libtorrent
{
#ifdef BOOST_NO_CXX11_HDR_RANDOM
@ -73,25 +70,28 @@ namespace libtorrent
#else
namespace
{
#if !TORRENT_THREADSAFE_STATIC
// because local statics are not atomic pre c++11
// do it manually, probably at a higher cost
namespace
{
static mutex random_device_mutex;
static random_device* dev = NULL;
static mt19937* rnd = NULL;
}
random_device* dev = NULL;
mt19937* rnd = NULL;
#endif
mutex random_device_mutex;
}
boost::uint32_t random()
{
// TODO: use a thread local mt19937 instance instead!
mutex::scoped_lock l(random_device_mutex);
#if TORRENT_THREADSAFE_STATIC
static random_device dev;
static mt19937 random_engine(dev());
return uniform_int_distribution<boost::uint32_t>(0, UINT_MAX)(random_engine);
#else
mutex::scoped_lock l(random_device_mutex);
if (dev == NULL)
{