switch to boost::thread for mingw build

This commit is contained in:
Lee Clagett 2017-02-12 17:53:13 -05:00
parent 3f171b931f
commit 2cde2c02ca
8 changed files with 69 additions and 64 deletions

View file

@ -41,8 +41,10 @@ target_link_libraries(libwallet_api_tests
PRIVATE
wallet
epee
${Boost_CHRONO_LIBRARY}
${Boost_SERIALIZATION_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${GTEST_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}

View file

@ -34,18 +34,19 @@
#include "wallet/wallet2.h"
#include "include_base_utils.h"
#include <boost/chrono/chrono.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>
#include <vector>
#include <mutex>
#include <thread>
#include <atomic>
#include <functional>
#include <condition_variable>
using namespace std;
@ -254,15 +255,15 @@ TEST_F(WalletManagerTest, WalletAmountFromString)
}
void open_wallet_helper(Monero::WalletManager *wmgr, Monero::Wallet **wallet, const std::string &pass, std::mutex *mutex)
void open_wallet_helper(Monero::WalletManager *wmgr, Monero::Wallet **wallet, const std::string &pass, boost::mutex *mutex)
{
if (mutex)
mutex->lock();
LOG_PRINT_L3("opening wallet in thread: " << std::this_thread::get_id());
LOG_PRINT_L3("opening wallet in thread: " << boost::this_thread::get_id());
*wallet = wmgr->openWallet(WALLET_NAME, pass, true);
LOG_PRINT_L3("wallet address: " << (*wallet)->address());
LOG_PRINT_L3("wallet status: " << (*wallet)->status());
LOG_PRINT_L3("closing wallet in thread: " << std::this_thread::get_id());
LOG_PRINT_L3("closing wallet in thread: " << boost::this_thread::get_id());
if (mutex)
mutex->unlock();
}
@ -307,7 +308,7 @@ TEST_F(WalletManagerTest, WalletManagerOpensWalletWithPasswordAndReopen)
Monero::Wallet *wallet2 = nullptr;
Monero::Wallet *wallet3 = nullptr;
std::mutex mutex;
boost::mutex mutex;
open_wallet_helper(wmgr, &wallet2, wrong_wallet_pass, nullptr);
ASSERT_TRUE(wallet2 != nullptr);
@ -785,12 +786,12 @@ struct MyWalletListener : public Monero::WalletListener
Monero::Wallet * wallet;
uint64_t total_tx;
uint64_t total_rx;
std::mutex mutex;
std::condition_variable cv_send;
std::condition_variable cv_receive;
std::condition_variable cv_update;
std::condition_variable cv_refresh;
std::condition_variable cv_newblock;
boost::mutex mutex;
boost::condition_variable cv_send;
boost::condition_variable cv_receive;
boost::condition_variable cv_update;
boost::condition_variable cv_refresh;
boost::condition_variable cv_newblock;
bool send_triggered;
bool receive_triggered;
bool newblock_triggered;
@ -880,8 +881,8 @@ TEST_F(WalletTest2, WalletCallBackRefreshedSync)
ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0));
ASSERT_TRUE(wallet_src_listener->refresh_triggered);
ASSERT_TRUE(wallet_src->connected());
std::chrono::seconds wait_for = std::chrono::seconds(60*3);
std::unique_lock<std::mutex> lock (wallet_src_listener->mutex);
boost::chrono::seconds wait_for = boost::chrono::seconds(60*3);
boost::unique_lock<boost::mutex> lock (wallet_src_listener->mutex);
wallet_src_listener->cv_refresh.wait_for(lock, wait_for);
wmgr->closeWallet(wallet_src);
}
@ -895,8 +896,8 @@ TEST_F(WalletTest2, WalletCallBackRefreshedAsync)
Monero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
MyWalletListener * wallet_src_listener = new MyWalletListener(wallet_src);
std::chrono::seconds wait_for = std::chrono::seconds(20);
std::unique_lock<std::mutex> lock (wallet_src_listener->mutex);
boost::chrono::seconds wait_for = boost::chrono::seconds(20);
boost::unique_lock<boost::mutex> lock (wallet_src_listener->mutex);
wallet_src->init(MAINNET_DAEMON_ADDRESS, 0);
wallet_src->startRefresh();
std::cerr << "TEST: waiting on refresh lock...\n";
@ -936,8 +937,8 @@ TEST_F(WalletTest2, WalletCallbackSent)
ASSERT_TRUE(tx->status() == Monero::PendingTransaction::Status_Ok);
ASSERT_TRUE(tx->commit());
std::chrono::seconds wait_for = std::chrono::seconds(60*3);
std::unique_lock<std::mutex> lock (wallet_src_listener->mutex);
boost::chrono::seconds wait_for = boost::chrono::seconds(60*3);
boost::unique_lock<boost::mutex> lock (wallet_src_listener->mutex);
std::cerr << "TEST: waiting on send lock...\n";
wallet_src_listener->cv_send.wait_for(lock, wait_for);
std::cerr << "TEST: send lock acquired...\n";
@ -978,8 +979,8 @@ TEST_F(WalletTest2, WalletCallbackReceived)
ASSERT_TRUE(tx->status() == Monero::PendingTransaction::Status_Ok);
ASSERT_TRUE(tx->commit());
std::chrono::seconds wait_for = std::chrono::seconds(60*4);
std::unique_lock<std::mutex> lock (wallet_dst_listener->mutex);
boost::chrono::seconds wait_for = boost::chrono::seconds(60*4);
boost::unique_lock<boost::mutex> lock (wallet_dst_listener->mutex);
std::cerr << "TEST: waiting on receive lock...\n";
wallet_dst_listener->cv_receive.wait_for(lock, wait_for);
std::cerr << "TEST: receive lock acquired...\n";
@ -1011,8 +1012,8 @@ TEST_F(WalletTest2, WalletCallbackNewBlock)
std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet_src));
// wait max 4 min for new block
std::chrono::seconds wait_for = std::chrono::seconds(60*4);
std::unique_lock<std::mutex> lock (wallet_listener->mutex);
boost::chrono::seconds wait_for = boost::chrono::seconds(60*4);
boost::unique_lock<boost::mutex> lock (wallet_listener->mutex);
std::cerr << "TEST: waiting on newblock lock...\n";
wallet_listener->cv_newblock.wait_for(lock, wait_for);
std::cerr << "TEST: newblock lock acquired...\n";
@ -1049,8 +1050,8 @@ TEST_F(WalletManagerMainnetTest, CreateAndRefreshWalletMainNetAsync)
Monero::Wallet * wallet = wmgr->createWallet(WALLET_NAME_MAINNET, "", WALLET_LANG);
std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet));
std::chrono::seconds wait_for = std::chrono::seconds(SECONDS_TO_REFRESH);
std::unique_lock<std::mutex> lock (wallet_listener->mutex);
boost::chrono::seconds wait_for = boost::chrono::seconds(SECONDS_TO_REFRESH);
boost::unique_lock<boost::mutex> lock (wallet_listener->mutex);
wallet->init(MAINNET_DAEMON_ADDRESS, 0);
wallet->startRefresh();
std::cerr << "TEST: waiting on refresh lock...\n";
@ -1075,8 +1076,8 @@ TEST_F(WalletManagerMainnetTest, OpenAndRefreshWalletMainNetAsync)
std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet));
std::chrono::seconds wait_for = std::chrono::seconds(SECONDS_TO_REFRESH);
std::unique_lock<std::mutex> lock (wallet_listener->mutex);
boost::chrono::seconds wait_for = boost::chrono::seconds(SECONDS_TO_REFRESH);
boost::unique_lock<boost::mutex> lock (wallet_listener->mutex);
wallet->init(MAINNET_DAEMON_ADDRESS, 0);
wallet->startRefresh();
std::cerr << "TEST: waiting on refresh lock...\n";
@ -1109,8 +1110,8 @@ TEST_F(WalletManagerMainnetTest, RecoverAndRefreshWalletMainNetAsync)
ASSERT_TRUE(wallet->status() == Monero::Wallet::Status_Ok);
ASSERT_TRUE(wallet->address() == address);
std::unique_ptr<MyWalletListener> wallet_listener (new MyWalletListener(wallet));
std::chrono::seconds wait_for = std::chrono::seconds(SECONDS_TO_REFRESH);
std::unique_lock<std::mutex> lock (wallet_listener->mutex);
boost::chrono::seconds wait_for = boost::chrono::seconds(SECONDS_TO_REFRESH);
boost::unique_lock<boost::mutex> lock (wallet_listener->mutex);
wallet->init(MAINNET_DAEMON_ADDRESS, 0);
wallet->startRefresh();
std::cerr << "TEST: waiting on refresh lock...\n";

View file

@ -44,6 +44,7 @@ target_link_libraries(net_load_tests_clt
${Boost_CHRONO_LIBRARY}
${Boost_DATE_TIME_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_THREAD_LIBARRY}
${CMAKE_THREAD_LIBS_INIT}
${EXTRA_LIBRARIES})

View file

@ -32,7 +32,7 @@
#include <chrono>
#include <functional>
#include <numeric>
#include <thread>
#include <boost/thread/thread.hpp>
#include <vector>
#include "gtest/gtest.h"
@ -191,7 +191,7 @@ namespace
protected:
virtual void SetUp()
{
m_thread_count = (std::max)(min_thread_count, std::thread::hardware_concurrency() / 2);
m_thread_count = (std::max)(min_thread_count, boost::thread::hardware_concurrency() / 2);
m_tcp_server.get_config_object().m_pcommands_handler = &m_commands_handler;
m_tcp_server.get_config_object().m_invoke_timeout = CONNECTION_TIMEOUT;
@ -278,10 +278,10 @@ namespace
void parallel_exec(const Func& func)
{
unit_test::call_counter properly_finished_threads;
std::vector<std::thread> threads(m_thread_count);
std::vector<boost::thread> threads(m_thread_count);
for (size_t i = 0; i < threads.size(); ++i)
{
threads[i] = std::thread([&, i] {
threads[i] = boost::thread([&, i] {
call_func(i, func, 0);
properly_finished_threads.inc();
});

View file

@ -28,8 +28,8 @@
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include <mutex>
#include <thread>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include "include_base_utils.h"
#include "misc_log_ex.h"
@ -58,7 +58,7 @@ namespace
//std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::unique_lock<std::mutex> lock(m_open_close_test_mutex);
boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
if (!m_open_close_test_conn_id.is_nil())
{
EXIT_ON_ERROR(m_open_close_test_helper->handle_new_connection(context.m_connection_id, true));
@ -69,7 +69,7 @@ namespace
{
test_levin_commands_handler::on_connection_close(context);
std::unique_lock<std::mutex> lock(m_open_close_test_mutex);
boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
if (context.m_connection_id == m_open_close_test_conn_id)
{
LOG_PRINT_L0("Stop open/close test");
@ -115,7 +115,7 @@ namespace
int handle_start_open_close_test(int command, const CMD_START_OPEN_CLOSE_TEST::request& req, CMD_START_OPEN_CLOSE_TEST::response&, test_connection_context& context)
{
std::unique_lock<std::mutex> lock(m_open_close_test_mutex);
boost::unique_lock<boost::mutex> lock(m_open_close_test_mutex);
if (0 == m_open_close_test_helper.get())
{
LOG_PRINT_L0("Start open/close test (" << req.open_request_target << ", " << req.max_opened_conn_count << ")");
@ -208,7 +208,7 @@ namespace
test_tcp_server& m_tcp_server;
boost::uuids::uuid m_open_close_test_conn_id;
std::mutex m_open_close_test_mutex;
boost::mutex m_open_close_test_mutex;
std::unique_ptr<open_close_test_helper> m_open_close_test_helper;
};
}
@ -218,7 +218,7 @@ int main(int argc, char** argv)
//set up logging options
mlog_configure(mlog_get_default_log_path("net_load_tests_srv.log"), true);
size_t thread_count = (std::max)(min_thread_count, std::thread::hardware_concurrency() / 2);
size_t thread_count = (std::max)(min_thread_count, boost::thread::hardware_concurrency() / 2);
test_tcp_server tcp_server(epee::net_utils::e_connection_type_RPC);
if (!tcp_server.init_server(srv_port, "127.0.0.1"))

View file

@ -75,6 +75,8 @@ target_link_libraries(unit_tests
wallet
p2p
epee
${Boost_CHRONO_LIBRARY}
${Boost_THREAD_LIBRARY}
${GTEST_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${EXTRA_LIBRARIES})

View file

@ -28,10 +28,9 @@
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include <condition_variable>
#include <chrono>
#include <mutex>
#include <thread>
#include <boost/chrono/chrono.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include "gtest/gtest.h"
@ -88,13 +87,13 @@ TEST(boosted_tcp_server, worker_threads_are_exception_resistant)
test_tcp_server srv(epee::net_utils::e_connection_type_RPC); // RPC disables network limit for unit tests
ASSERT_TRUE(srv.init_server(test_server_port, test_server_host));
std::mutex mtx;
std::condition_variable cond;
boost::mutex mtx;
boost::condition_variable cond;
int counter = 0;
auto counter_incrementer = [&counter, &cond, &mtx]()
{
std::unique_lock<std::mutex> lock(mtx);
boost::unique_lock<boost::mutex> lock(mtx);
++counter;
if (4 <= counter)
{
@ -110,8 +109,8 @@ TEST(boosted_tcp_server, worker_threads_are_exception_resistant)
ASSERT_TRUE(srv.async_call([&counter_incrementer]() { counter_incrementer(); throw 4; }));
{
std::unique_lock<std::mutex> lock(mtx);
ASSERT_NE(std::cv_status::timeout, cond.wait_for(lock, std::chrono::seconds(5)));
boost::unique_lock<boost::mutex> lock(mtx);
ASSERT_NE(boost::cv_status::timeout, cond.wait_for(lock, boost::chrono::seconds(5)));
ASSERT_EQ(4, counter);
}
@ -124,8 +123,8 @@ TEST(boosted_tcp_server, worker_threads_are_exception_resistant)
ASSERT_TRUE(srv.async_call(counter_incrementer));
{
std::unique_lock<std::mutex> lock(mtx);
ASSERT_NE(std::cv_status::timeout, cond.wait_for(lock, std::chrono::seconds(5)));
boost::unique_lock<boost::mutex> lock(mtx);
ASSERT_NE(boost::cv_status::timeout, cond.wait_for(lock, boost::chrono::seconds(5)));
ASSERT_EQ(4, counter);
}

View file

@ -28,8 +28,8 @@
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include <mutex>
#include <thread>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include "gtest/gtest.h"
@ -59,7 +59,7 @@ namespace
virtual int invoke(int command, const std::string& in_buff, std::string& buff_out, test_levin_connection_context& context)
{
m_invoke_counter.inc();
std::unique_lock<std::mutex> lock(m_mutex);
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_command = command;
m_last_in_buf = in_buff;
buff_out = m_invoke_out_buf;
@ -69,7 +69,7 @@ namespace
virtual int notify(int command, const std::string& in_buff, test_levin_connection_context& context)
{
m_notify_counter.inc();
std::unique_lock<std::mutex> lock(m_mutex);
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_command = command;
m_last_in_buf = in_buff;
return m_return_code;
@ -115,7 +115,7 @@ namespace
unit_test::call_counter m_new_connection_counter;
unit_test::call_counter m_close_connection_counter;
std::mutex m_mutex;
boost::mutex m_mutex;
int m_return_code;
std::string m_invoke_out_buf;
@ -144,7 +144,7 @@ namespace
{
//std::cout << "test_connection::do_send()" << std::endl;
m_send_counter.inc();
std::unique_lock<std::mutex> lock(m_mutex);
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_send_data.append(reinterpret_cast<const char*>(ptr), cb);
return m_send_return;
}
@ -159,7 +159,7 @@ namespace
size_t send_counter() const { return m_send_counter.get(); }
const std::string& last_send_data() const { return m_last_send_data; }
void reset_last_send_data() { std::unique_lock<std::mutex> lock(m_mutex); m_last_send_data.clear(); }
void reset_last_send_data() { boost::unique_lock<boost::mutex> lock(m_mutex); m_last_send_data.clear(); }
bool send_return() const { return m_send_return; }
void send_return(bool v) { m_send_return = v; }
@ -172,7 +172,7 @@ namespace
test_levin_connection_context m_context;
unit_test::call_counter m_send_counter;
std::mutex m_mutex;
boost::mutex m_mutex;
std::string m_last_send_data;
@ -305,14 +305,14 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, concurent_handl
}
};
const size_t thread_count = std::thread::hardware_concurrency();
std::vector<std::thread> threads(thread_count);
for (std::thread& th : threads)
const size_t thread_count = boost::thread::hardware_concurrency();
std::vector<boost::thread> threads(thread_count);
for (boost::thread& th : threads)
{
th = std::thread(create_and_destroy_connections);
th = boost::thread(create_and_destroy_connections);
}
for (std::thread& th : threads)
for (boost::thread& th : threads)
{
th.join();
}