fix seeding of random number generator on mingw

This commit is contained in:
arvidn 2019-03-28 20:45:39 +01:00 committed by Arvid Norberg
parent d113816ae6
commit bcb26fd638
2 changed files with 25 additions and 1 deletions

View File

@ -236,6 +236,9 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_USE_PREADV 1 #define TORRENT_USE_PREADV 1
#define TORRENT_USE_PWRITEV 1 #define TORRENT_USE_PWRITEV 1
// mingw doesn't implement random_device.
#define TORRENT_BROKEN_RANDOM_DEVICE 1
# if !defined TORRENT_USE_LIBCRYPTO && !defined TORRENT_USE_LIBGCRYPT # if !defined TORRENT_USE_LIBCRYPTO && !defined TORRENT_USE_LIBGCRYPT
// unless some other crypto library has been specified, default to the native // unless some other crypto library has been specified, default to the native
// windows CryptoAPI // windows CryptoAPI
@ -354,6 +357,10 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_FORMAT(fmt, ellipsis) #define TORRENT_FORMAT(fmt, ellipsis)
#endif #endif
#ifndef TORRENT_BROKEN_RANDOM_DEVICE
#define TORRENT_BROKEN_RANDOM_DEVICE 0
#endif
// libiconv presence detection is not implemented yet // libiconv presence detection is not implemented yet
#ifndef TORRENT_USE_ICONV #ifndef TORRENT_USE_ICONV
#define TORRENT_USE_ICONV 1 #define TORRENT_USE_ICONV 1

View File

@ -40,6 +40,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include <mutex> #include <mutex>
#endif #endif
#if TORRENT_BROKEN_RANDOM_DEVICE
#include "libtorrent/time.hpp"
#include <atomic>
#endif
#if TORRENT_USE_CRYPTOAPI #if TORRENT_USE_CRYPTOAPI
#include "libtorrent/aux_/win_crypto_provider.hpp" #include "libtorrent/aux_/win_crypto_provider.hpp"
@ -67,7 +72,8 @@ namespace {
} }
#endif #endif
namespace libtorrent { namespace aux { namespace libtorrent {
namespace aux {
std::mt19937& random_engine() std::mt19937& random_engine()
{ {
@ -76,7 +82,18 @@ namespace libtorrent { namespace aux {
static std::mt19937 rng(0x82daf973); static std::mt19937 rng(0x82daf973);
#else #else
#if TORRENT_BROKEN_RANDOM_DEVICE
struct {
std::uint32_t operator()() const
{
static std::atomic<std::uint32_t> seed{static_cast<std::uint32_t>(duration_cast<microseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch()).count())};
return seed++;
}
} dev;
#else
static std::random_device dev; static std::random_device dev;
#endif
#ifdef BOOST_NO_CXX11_THREAD_LOCAL #ifdef BOOST_NO_CXX11_THREAD_LOCAL
static std::mt19937 rng(dev()); static std::mt19937 rng(dev());
#else #else