Skip to content

Commit

Permalink
fix seeding of random number generator on mingw
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jul 4, 2019
1 parent d113816 commit bcb26fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions include/libtorrent/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_USE_PREADV 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
// unless some other crypto library has been specified, default to the native
// windows CryptoAPI
Expand Down Expand Up @@ -354,6 +357,10 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_FORMAT(fmt, ellipsis)
#endif

#ifndef TORRENT_BROKEN_RANDOM_DEVICE
#define TORRENT_BROKEN_RANDOM_DEVICE 0
#endif

// libiconv presence detection is not implemented yet
#ifndef TORRENT_USE_ICONV
#define TORRENT_USE_ICONV 1
Expand Down
19 changes: 18 additions & 1 deletion src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include <mutex>
#endif

#if TORRENT_BROKEN_RANDOM_DEVICE
#include "libtorrent/time.hpp"
#include <atomic>
#endif

#if TORRENT_USE_CRYPTOAPI
#include "libtorrent/aux_/win_crypto_provider.hpp"

Expand Down Expand Up @@ -67,7 +72,8 @@ namespace {
}
#endif

namespace libtorrent { namespace aux {
namespace libtorrent {
namespace aux {

std::mt19937& random_engine()
{
Expand All @@ -76,7 +82,18 @@ namespace libtorrent { namespace aux {
static std::mt19937 rng(0x82daf973);
#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;
#endif
#ifdef BOOST_NO_CXX11_THREAD_LOCAL
static std::mt19937 rng(dev());
#else
Expand Down

0 comments on commit bcb26fd

Please sign in to comment.