forked from premiere/premiere-libtorrent
fixed race condition in random number generator
This commit is contained in:
parent
2408200475
commit
049d867c12
|
@ -1,4 +1,5 @@
|
||||||
|
|
||||||
|
* fixed race condition in random number generator
|
||||||
* fix race condition in stat_cache (disk storage)
|
* fix race condition in stat_cache (disk storage)
|
||||||
* improve error handling of failing to change file priority
|
* improve error handling of failing to change file priority
|
||||||
The API for custom storage implementations was altered
|
The API for custom storage implementations was altered
|
||||||
|
|
|
@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include "libtorrent/config.hpp"
|
#include "libtorrent/config.hpp"
|
||||||
#include "libtorrent/random.hpp"
|
#include "libtorrent/random.hpp"
|
||||||
#include "libtorrent/assert.hpp"
|
#include "libtorrent/assert.hpp"
|
||||||
|
#include "libtorrent/thread.hpp"
|
||||||
|
|
||||||
#ifdef BOOST_NO_CXX11_HDR_RANDOM
|
#ifdef BOOST_NO_CXX11_HDR_RANDOM
|
||||||
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
||||||
|
@ -46,10 +47,6 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#include <random>
|
#include <random>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !TORRENT_THREADSAFE_STATIC
|
|
||||||
#include "libtorrent/thread.hpp"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace libtorrent
|
namespace libtorrent
|
||||||
{
|
{
|
||||||
#ifdef BOOST_NO_CXX11_HDR_RANDOM
|
#ifdef BOOST_NO_CXX11_HDR_RANDOM
|
||||||
|
@ -73,25 +70,28 @@ namespace libtorrent
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#if !TORRENT_THREADSAFE_STATIC
|
|
||||||
// because local statics are not atomic pre c++11
|
|
||||||
// do it manually, probably at a higher cost
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
static mutex random_device_mutex;
|
#if !TORRENT_THREADSAFE_STATIC
|
||||||
static random_device* dev = NULL;
|
// because local statics are not atomic pre c++11
|
||||||
static mt19937* rnd = NULL;
|
// do it manually, probably at a higher cost
|
||||||
}
|
random_device* dev = NULL;
|
||||||
|
mt19937* rnd = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
mutex random_device_mutex;
|
||||||
|
}
|
||||||
|
|
||||||
boost::uint32_t random()
|
boost::uint32_t random()
|
||||||
{
|
{
|
||||||
|
// TODO: use a thread local mt19937 instance instead!
|
||||||
|
mutex::scoped_lock l(random_device_mutex);
|
||||||
|
|
||||||
#if TORRENT_THREADSAFE_STATIC
|
#if TORRENT_THREADSAFE_STATIC
|
||||||
static random_device dev;
|
static random_device dev;
|
||||||
static mt19937 random_engine(dev());
|
static mt19937 random_engine(dev());
|
||||||
return uniform_int_distribution<boost::uint32_t>(0, UINT_MAX)(random_engine);
|
return uniform_int_distribution<boost::uint32_t>(0, UINT_MAX)(random_engine);
|
||||||
#else
|
#else
|
||||||
mutex::scoped_lock l(random_device_mutex);
|
|
||||||
|
|
||||||
if (dev == NULL)
|
if (dev == NULL)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue