no more deprecated std::random_shuffle (#999)

replace deprecated std::random_shuffle. more aggressive random_shuffle in DHT simulation setup. use travis_retry.
This commit is contained in:
Alden Torres 2016-08-14 19:48:31 -04:00 committed by Arvid Norberg
parent bc8c49dc15
commit 2f7adfbb92
16 changed files with 55 additions and 61 deletions

View File

@ -97,8 +97,8 @@ install:
wget -O boost.zip http://pilotfiber.dl.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.zip; wget -O boost.zip http://pilotfiber.dl.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.zip;
unzip -qq boost.zip; unzip -qq boost.zip;
export BOOST_ROOT=$PWD/boost_1_55_0; export BOOST_ROOT=$PWD/boost_1_55_0;
sudo apt-get install -y qemu-user-static debootstrap; travis_retry sudo apt-get install -y qemu-user-static debootstrap;
sudo debootstrap --variant=minbase --arch armhf --foreign --include=build-essential testing rootfs; travis_retry sudo debootstrap --variant=minbase --arch armhf --foreign --include=build-essential testing rootfs;
sudo cp /usr/bin/qemu-arm-static rootfs/usr/bin/; sudo cp /usr/bin/qemu-arm-static rootfs/usr/bin/;
sudo chroot rootfs /debootstrap/debootstrap --second-stage; sudo chroot rootfs /debootstrap/debootstrap --second-stage;
sudo chroot rootfs mount -t proc none /proc; sudo chroot rootfs mount -t proc none /proc;
@ -118,7 +118,7 @@ script:
# libtorrent is the name of the test suite target # libtorrent is the name of the test suite target
- cd test - cd test
- 'if [ "$variant" != "" ]; then - 'if [ "$variant" != "" ]; then
bjam -j3 warnings-as-errors=on ssl=$ssl crypto=$crypto debug-iterators=on picker-debugging=on invariant-checks=full $toolset variant=$variant libtorrent test_natpmp enum_if -l300 && travis_retry bjam -j3 warnings-as-errors=on ssl=$ssl crypto=$crypto debug-iterators=on picker-debugging=on invariant-checks=full $toolset variant=$variant libtorrent test_natpmp enum_if -l300 &&
if [ "$coverage" == "1" ]; then if [ "$coverage" == "1" ]; then
codecov --root .. --gcov-exec gcov-5; codecov --root .. --gcov-exec gcov-5;
fi; fi;

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/hasher.hpp" #include "libtorrent/hasher.hpp"
#include "libtorrent/socket_io.hpp" #include "libtorrent/socket_io.hpp"
#include "libtorrent/file_pool.hpp" #include "libtorrent/file_pool.hpp"
#include "libtorrent/random.hpp"
#include <cstring> #include <cstring>
#include <thread> #include <thread>
#include <functional> #include <functional>
@ -553,7 +554,7 @@ struct peer_conn
pieces.resize(num_pieces); pieces.resize(num_pieces);
for (int i = 0; i < int(pieces.size()); ++i) for (int i = 0; i < int(pieces.size()); ++i)
pieces[i] = i; pieces[i] = i;
std::random_shuffle(pieces.begin(), pieces.end()); aux::random_shuffle(pieces.begin(), pieces.end());
} }
else if (msg == 4) // have else if (msg == 4) // have
{ {
@ -577,7 +578,7 @@ struct peer_conn
} }
++ptr; ++ptr;
} }
std::random_shuffle(pieces.begin(), pieces.end()); aux::random_shuffle(pieces.begin(), pieces.end());
} }
else if (msg == 7) // piece else if (msg == 7) // piece
{ {

View File

@ -123,9 +123,6 @@ POSSIBILITY OF SUCH DAMAGE.
// //
// virtual std::string message() const; // virtual std::string message() const;
// //
// virtual std::auto_ptr<alert> clone() const
// { return std::auto_ptr<alert>(new name(\*this)); }
//
// static const int static_category = *<bitmask of alert::category_t flags>*; // static const int static_category = *<bitmask of alert::category_t flags>*;
// virtual int category() const { return static_category; } // virtual int category() const { return static_category; }
// //

View File

@ -62,7 +62,7 @@ namespace libtorrent
bool add_vote(sha1_hash const& k, int type); bool add_vote(sha1_hash const& k, int type);
// we want to sort decending // we want to sort descending
bool operator<(external_ip_t const& rhs) const bool operator<(external_ip_t const& rhs) const
{ {
if (num_votes > rhs.num_votes) return true; if (num_votes > rhs.num_votes) return true;
@ -128,4 +128,3 @@ namespace libtorrent
} }
#endif #endif

View File

@ -31,10 +31,23 @@ POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include <cstdint> #include <cstdint>
#include <random>
#include <algorithm>
namespace libtorrent namespace libtorrent
{ {
std::uint32_t TORRENT_EXTRA_EXPORT random(std::uint32_t max); namespace aux
std::uint32_t TORRENT_EXTRA_EXPORT randint(std::uint32_t one_past_end); {
TORRENT_EXTRA_EXPORT std::mt19937& random_engine();
template<class RandomIt>
void random_shuffle(RandomIt first, RandomIt last)
{
std::shuffle(first, last, random_engine());
}
}
TORRENT_EXTRA_EXPORT std::uint32_t random(std::uint32_t max);
} }

View File

@ -311,7 +311,7 @@ namespace libtorrent
// //
virtual void release_files(storage_error& ec) = 0; virtual void release_files(storage_error& ec) = 0;
// Rename file with index ``file`` to the thame ``new_name``. // Rename file with index ``index`` to the name ``new_filename``.
// //
// If an error occurs, ``storage_error`` should be set to reflect it. // If an error occurs, ``storage_error`` should be set to reflect it.
// //

View File

@ -81,4 +81,3 @@ namespace libtorrent {
} }
#endif // TORRENT_TIME_HPP_INCLUDED #endif // TORRENT_TIME_HPP_INCLUDED

View File

@ -214,7 +214,7 @@ struct dht_node final : lt::dht::udp_socket_interface
, to_hex(dht::distance(id, n.first).to_string()).c_str() , to_hex(dht::distance(id, n.first).to_string()).c_str()
, bucket); , bucket);
*/ */
// there are no more slots in this bucket, just move ont // there are no more slots in this bucket, just move on
if (nodes_per_bucket[bucket] == 0) continue; if (nodes_per_bucket[bucket] == 0) continue;
--nodes_per_bucket[bucket]; --nodes_per_bucket[bucket];
bool const added = dht().m_table.node_seen(n.first, n.second, lt::random(300) + 10); bool const added = dht().m_table.node_seen(n.first, n.second, lt::random(300) + 10);
@ -292,11 +292,11 @@ dht_network::dht_network(sim::simulation& sim, int num_nodes, std::uint32_t flag
for (auto& n : m_nodes) for (auto& n : m_nodes)
{ {
n.bootstrap(all_nodes); n.bootstrap(all_nodes);
if (++cnt == 50) if (++cnt == 25)
{ {
// every now and then, shuffle all_nodes to make the // every now and then, shuffle all_nodes to make the
// routing tables more randomly distributed // routing tables more randomly distributed
std::random_shuffle(all_nodes.begin(), all_nodes.end()); lt::aux::random_shuffle(all_nodes.begin(), all_nodes.end());
cnt = 0; cnt = 0;
} }
} }

View File

@ -1082,7 +1082,7 @@ namespace libtorrent
if (storage && storage->get_storage_impl()->m_settings == nullptr) if (storage && storage->get_storage_impl()->m_settings == nullptr)
storage->get_storage_impl()->m_settings = &m_settings; storage->get_storage_impl()->m_settings = &m_settings;
TORRENT_ASSERT(j->action < sizeof(job_functions)/sizeof(job_functions[0])); TORRENT_ASSERT(j->action < sizeof(job_functions) / sizeof(job_functions[0]));
time_point start_time = clock_type::now(); time_point start_time = clock_type::now();
@ -1091,7 +1091,7 @@ namespace libtorrent
// call disk function // call disk function
int ret = (this->*(job_functions[j->action]))(j, completed_jobs); int ret = (this->*(job_functions[j->action]))(j, completed_jobs);
// note that -2 erros are OK // note that -2 errors are OK
TORRENT_ASSERT(ret != -1 || (j->error.ec && j->error.operation != 0)); TORRENT_ASSERT(ret != -1 || (j->error.ec && j->error.operation != 0));
m_stats_counters.inc_stats_counter(counters::num_running_disk_jobs, -1); m_stats_counters.inc_stats_counter(counters::num_running_disk_jobs, -1);
@ -1226,7 +1226,7 @@ namespace libtorrent
- (iov_len-1) * block_size, block_size); - (iov_len-1) * block_size, block_size);
TORRENT_ASSERT(iov[iov_len-1].iov_len > 0); TORRENT_ASSERT(iov[iov_len-1].iov_len > 0);
// at this point, all the buffers are allocated and iov is initizalied // at this point, all the buffers are allocated and iov is initialized
// and the blocks have their refcounters incremented, so no other thread // and the blocks have their refcounters incremented, so no other thread
// can remove them. We can now release the cache std::mutex and dive into the // can remove them. We can now release the cache std::mutex and dive into the
// disk operations. // disk operations.
@ -2898,7 +2898,7 @@ namespace libtorrent
int disk_io_thread::do_file_priority(disk_io_job* j, jobqueue_t& /* completed_jobs */ ) int disk_io_thread::do_file_priority(disk_io_job* j, jobqueue_t& /* completed_jobs */ )
{ {
std::unique_ptr<std::vector<std::uint8_t> > p(j->buffer.priorities); std::unique_ptr<std::vector<std::uint8_t>> p(j->buffer.priorities);
j->storage->get_storage_impl()->set_file_priority(*p, j->error); j->storage->get_storage_impl()->set_file_priority(*p, j->error);
return 0; return 0;
} }

View File

@ -529,7 +529,7 @@ void http_connection::on_resolve(error_code const& e
return; return;
} }
std::random_shuffle(m_endpoints.begin(), m_endpoints.end(), randint); aux::random_shuffle(m_endpoints.begin(), m_endpoints.end());
// sort the endpoints so that the ones with the same IP version as our // sort the endpoints so that the ones with the same IP version as our
// bound listen socket are first. So that when contacting a tracker, // bound listen socket are first. So that when contacting a tracker,

View File

@ -1515,7 +1515,7 @@ namespace libtorrent
for (int b : m_priority_boundaries) for (int b : m_priority_boundaries)
{ {
if (start == b) continue; if (start == b) continue;
std::random_shuffle(&m_pieces[0] + start, &m_pieces[0] + b, randint); aux::random_shuffle(&m_pieces[0] + start, &m_pieces[0] + b);
start = b; start = b;
} }

View File

@ -33,41 +33,25 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/random.hpp" #include "libtorrent/random.hpp"
#include <random>
namespace libtorrent namespace libtorrent
{ {
using std::random_device; namespace aux
using std::mt19937; {
using std::uniform_int_distribution; std::mt19937& random_engine()
{
#ifdef TORRENT_BUILD_SIMULATOR #ifdef TORRENT_BUILD_SIMULATOR
// make sure random numbers are deterministic. Seed with a fixed number
std::uint32_t random(std::uint32_t max) static std::mt19937 rng(0x82daf973);
{
// make sure random numbers are deterministic. Seed with a fixed number
static mt19937 random_engine(0x82daf973);
return uniform_int_distribution<std::uint32_t>(0, max)(random_engine);
}
#else #else
static std::random_device dev;
static std::mt19937 rng(dev());
#endif
return rng;
}
}
std::uint32_t random(std::uint32_t max) std::uint32_t random(std::uint32_t max)
{ {
// TODO: versions prior to msvc-14 (visual studio 2015) do return std::uniform_int_distribution<std::uint32_t>(0, max)(aux::random_engine());
// not generate thread safe initialization of statics
static random_device dev;
static mt19937 random_engine(dev());
return uniform_int_distribution<std::uint32_t>(0, max)(random_engine);
} }
#endif // TORRENT_BUILD_SIMULATOR
std::uint32_t randint(std::uint32_t one_past_end)
{
return random(one_past_end - 1);
}
} }

View File

@ -5244,7 +5244,7 @@ namespace libtorrent
if (!valid_metadata()) return; if (!valid_metadata()) return;
// stoage may be nullptr during shutdown // storage may be nullptr during shutdown
if (m_storage) if (m_storage)
{ {
inc_refcount("file_priority"); inc_refcount("file_priority");
@ -6609,7 +6609,7 @@ namespace libtorrent
// if we didn't save 100 peers, fill in with second choice peers // if we didn't save 100 peers, fill in with second choice peers
if (num_saved_peers < 100) if (num_saved_peers < 100)
{ {
std::random_shuffle(deferred_peers.begin(), deferred_peers.end(), randint); aux::random_shuffle(deferred_peers.begin(), deferred_peers.end());
for (std::vector<torrent_peer const*>::const_iterator i = deferred_peers.begin() for (std::vector<torrent_peer const*>::const_iterator i = deferred_peers.begin()
, end(deferred_peers.end()); i != end && num_saved_peers < 100; ++i) , end(deferred_peers.end()); i != end && num_saved_peers < 100; ++i)
{ {
@ -9629,7 +9629,7 @@ namespace libtorrent
if (p->is_seed()) seeds.push_back(p); if (p->is_seed()) seeds.push_back(p);
} }
std::random_shuffle(seeds.begin(), seeds.end(), randint); aux::random_shuffle(seeds.begin(), seeds.end());
TORRENT_ASSERT(to_disconnect <= int(seeds.size())); TORRENT_ASSERT(to_disconnect <= int(seeds.size()));
for (int i = 0; i < to_disconnect; ++i) for (int i = 0; i < to_disconnect; ++i)
seeds[i]->disconnect(errors::upload_upload_connection seeds[i]->disconnect(errors::upload_upload_connection

View File

@ -1401,12 +1401,12 @@ namespace libtorrent
{ {
if (stop->tier != current_tier) if (stop->tier != current_tier)
{ {
std::random_shuffle(start, stop, randint); aux::random_shuffle(start, stop);
start = stop; start = stop;
current_tier = stop->tier; current_tier = stop->tier;
} }
} }
std::random_shuffle(start, stop, randint); aux::random_shuffle(start, stop);
} }
} }

View File

@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/torrent_info.hpp" #include "libtorrent/torrent_info.hpp"
#include "libtorrent/io_service.hpp" #include "libtorrent/io_service.hpp"
#include "libtorrent/io.hpp" #include "libtorrent/io.hpp"
#include "libtorrent/random.hpp"
#include <cstdlib> #include <cstdlib>
#include <functional> #include <functional>
@ -400,7 +401,7 @@ void peer_conn::on_message(error_code const& ec, size_t bytes_transferred)
pieces.resize(m_ti.num_pieces()); pieces.resize(m_ti.num_pieces());
for (int i = 0; i < int(pieces.size()); ++i) for (int i = 0; i < int(pieces.size()); ++i)
pieces[i] = i; pieces[i] = i;
std::random_shuffle(pieces.begin(), pieces.end()); aux::random_shuffle(pieces.begin(), pieces.end());
} }
else if (msg == 4) // have else if (msg == 4) // have
{ {
@ -424,7 +425,7 @@ void peer_conn::on_message(error_code const& ec, size_t bytes_transferred)
} }
++ptr; ++ptr;
} }
std::random_shuffle(pieces.begin(), pieces.end()); aux::random_shuffle(pieces.begin(), pieces.end());
} }
else if (msg == 7) // piece else if (msg == 7) // piece
{ {

View File

@ -2623,7 +2623,7 @@ TORRENT_TEST(routing_table_extended)
std::vector<std::uint8_t> node_id_prefix; std::vector<std::uint8_t> node_id_prefix;
node_id_prefix.reserve(256); node_id_prefix.reserve(256);
for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i); for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i);
std::random_shuffle(node_id_prefix.begin(), node_id_prefix.end()); aux::random_shuffle(node_id_prefix.begin(), node_id_prefix.end());
routing_table tbl(id, udp::v4(), 8, sett, &observer); routing_table tbl(id, udp::v4(), 8, sett, &observer);
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)
@ -2657,7 +2657,7 @@ TORRENT_TEST(routing_table_set_id)
std::vector<std::uint8_t> node_id_prefix; std::vector<std::uint8_t> node_id_prefix;
node_id_prefix.reserve(256); node_id_prefix.reserve(256);
for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i); for (int i = 0; i < 256; ++i) node_id_prefix.push_back(i);
std::random_shuffle(node_id_prefix.begin(), node_id_prefix.end()); aux::random_shuffle(node_id_prefix.begin(), node_id_prefix.end());
routing_table tbl(id, udp::v4(), 8, sett, &observer); routing_table tbl(id, udp::v4(), 8, sett, &observer);
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)
{ {