std::random_device is broken when using Mingw < 9.2 [1].
It always produces the same sequence of numbers, so one critical
implication is that multiple sessions coming from different
processes will use the same identifier.
For now, we'll use boost::random::random_device when building with
Mingw.
[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85494
Signed-off-by: Lucian Petrut <lpetrut@cloudbasesolutions.com>
#include <type_traits>
#include <boost/optional.hpp>
+// Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85494
+#ifdef __MINGW32__
+#include <boost/random/random_device.hpp>
+
+using random_device_t = boost::random::random_device;
+#else
+using random_device_t = std::random_device;
+#endif
+
// Basic random number facility (see N3551 for inspiration):
namespace ceph::util {
template <typename MutexT, typename EngineT>
void randomize_rng(MutexT& m, EngineT& e)
{
- std::random_device rd;
+ random_device_t rd;
std::lock_guard<MutexT> lg(m);
e.seed(rd());
template <typename EngineT = std::default_random_engine>
void randomize_rng()
{
- std::random_device rd;
+ random_device_t rd;
detail::engine<EngineT>().seed(rd());
}
class random_number_generator final
{
std::mutex l;
- std::random_device rd;
+ random_device_t rd;
std::default_random_engine e;
using seed_type = typename decltype(e)::result_type;
*/
#include "encoding.h"
+#include "random.h"
#include <ostream>
#include <random>
}
void generate_random() {
- std::random_device rng;
+ random_device_t rng;
boost::uuids::basic_random_generator gen(rng);
uuid = gen();
}
#include <boost/lexical_cast.hpp>
#include "librbd/Utils.h"
+#include "include/random.h"
#include "include/rbd_types.h"
#include "include/stringify.h"
#include "include/neorados/RADOS.hpp"
librados::Rados rados(ioctx);
uint64_t bid = rados.get_instance_id();
- std::mt19937 generator{std::random_device{}()};
+ std::mt19937 generator{random_device_t{}()};
std::uniform_int_distribution<uint32_t> distribution{0, 0xFFFFFFFF};
uint32_t extra = distribution(generator);
#include <boost/range/algorithm_ext/copy_n.hpp>
#include "common/weighted_shuffle.h"
+#include "include/random.h"
#include "include/scope_guard.h"
#include "include/stringify.h"
auto rank_name = monmap.get_name(i);
weights.push_back(monmap.get_weight(rank_name));
}
- std::random_device rd;
+ random_device_t rd;
if (std::accumulate(begin(weights), end(weights), 0u) == 0) {
std::shuffle(begin(ranks), end(ranks), std::mt19937{rd()});
} else {
test_random.cc
)
add_ceph_unittest(unittest_random)
+target_link_libraries(unittest_random Boost::random)
# unittest_throttle
add_executable(unittest_throttle
#include "common/ceph_argparse.h"
#include "common/errno.h"
#include "common/safe_io.h"
+#include "include/random.h"
#include "mon/health_check.h"
#include <time.h>
#include <algorithm>
int r = clock_gettime(CLOCK_MONOTONIC, &round_start);
assert(r == 0);
do {
- std::random_device rd;
+ random_device_t rd;
std::shuffle(pools.begin(), pools.end(), std::mt19937{rd()});
cout << "pools ";
for (auto& i: pools)