Eliminate dependence on boost::interprocess #8223

In this repo, `boost::interprocess` was being used soley to make `uint32_t` operations atomic. So I replaced each instance of
`boost::interprocess::ipcdetail::atomic(...)32` with `std::atomic` methods. I replaced member declarations as applicable. For example,
when I needed to change a `volatile uint32_t` into a `std::atomic<uint32_t>`. Sometimes, a member was being used a boolean flag, so
I replaced it with `std::atomic<bool>`.

You may notice that I didn't touch `levin_client_async.h`. That is because this file is entirely unused and will be deleted in PR monero-project#8211.

Additional changes from review:
* Make some local variables const
* Change postfix operators to prefix operators where value was not need
This commit is contained in:
Jeffrey 2022-03-30 13:18:32 -05:00
parent 70ceab6c10
commit 17772ef53e
10 changed files with 43 additions and 50 deletions

View file

@ -49,7 +49,6 @@
#include <boost/asio/ssl.hpp>
#include <boost/array.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/interprocess/detail/atomic.hpp>
#include <boost/thread/thread.hpp>
#include "byte_slice.h"
#include "net_utils_base.h"
@ -393,7 +392,7 @@ namespace net_utils
std::vector<boost::shared_ptr<boost::thread> > m_threads;
boost::thread::id m_main_thread_id;
critical_section m_threads_lock;
volatile uint32_t m_thread_index; // TODO change to std::atomic
std::atomic<uint32_t> m_thread_index;
t_connection_type m_connection_type;

View file

@ -384,7 +384,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
{
//_info("[sock " << socket().native_handle() << "] protocol_want_close");
//some error in protocol, protocol handler ask to close connection
boost::interprocess::ipcdetail::atomic_write32(&m_want_close_connection, 1);
m_want_close_connection = true;
bool do_shutdown = false;
CRITICAL_REGION_BEGIN(m_send_que_lock);
if(!m_send_que.size())
@ -478,7 +478,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
if (!handshake(boost::asio::ssl::stream_base::server, boost::asio::const_buffer(buffer_.data(), buffer_ssl_init_fill)))
{
MERROR("SSL handshake failed");
boost::interprocess::ipcdetail::atomic_write32(&m_want_close_connection, 1);
m_want_close_connection = true;
m_ready_to_close = true;
bool do_shutdown = false;
CRITICAL_REGION_BEGIN(m_send_que_lock);
@ -834,7 +834,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
CRITICAL_REGION_BEGIN(m_send_que_lock);
send_que_size = m_send_que.size();
CRITICAL_REGION_END();
boost::interprocess::ipcdetail::atomic_write32(&m_want_close_connection, 1);
m_want_close_connection = true;
if(!send_que_size)
{
shutdown();
@ -889,7 +889,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
m_send_que.pop_front();
if(m_send_que.empty())
{
if(boost::interprocess::ipcdetail::atomic_read32(&m_want_close_connection))
if(m_want_close_connection)
{
do_shutdown = true;
}
@ -1119,7 +1119,7 @@ POP_WARNINGS
bool boosted_tcp_server<t_protocol_handler>::worker_thread()
{
TRY_ENTRY();
uint32_t local_thr_index = boost::interprocess::ipcdetail::atomic_inc32(&m_thread_index);
const uint32_t local_thr_index = m_thread_index++; // atomically increment, getting value before increment
std::string thread_name = std::string("[") + m_thread_name_prefix;
thread_name += boost::to_string(local_thr_index) + "]";
MLOG_SET_THREAD_NAME(thread_name);

View file

@ -106,7 +106,7 @@ class connection_basic { // not-templated base class for rapid developmet of som
std::unique_ptr< connection_basic_pimpl > mI; // my Implementation
// moved here from orginal connecton<> - common member variables that do not depend on template in connection<>
volatile uint32_t m_want_close_connection;
std::atomic<bool> m_want_close_connection;
std::atomic<bool> m_was_shutdown;
critical_section m_send_que_lock;
std::deque<byte_slice> m_send_que;

View file

@ -28,7 +28,6 @@
#include <boost/asio/deadline_timer.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/unordered_map.hpp>
#include <boost/interprocess/detail/atomic.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <atomic>
@ -166,7 +165,7 @@ public:
};
std::atomic<bool> m_protocol_released;
volatile uint32_t m_invoke_buf_ready;
std::atomic<bool> m_invoke_buf_ready;
volatile int m_invoke_result_code;
@ -175,8 +174,8 @@ public:
critical_section m_call_lock;
volatile uint32_t m_wait_count;
volatile uint32_t m_close_called;
std::atomic<uint32_t> m_wait_count;
std::atomic<uint32_t> m_close_called;
bucket_head2 m_current_head;
net_utils::i_service_endpoint* m_pservice_endpoint;
config_type& m_config;
@ -319,7 +318,7 @@ public:
m_wait_count = 0;
m_oponent_protocol_ver = 0;
m_connection_initialized = false;
m_invoke_buf_ready = 0;
m_invoke_buf_ready = false;
m_invoke_result_code = LEVIN_ERROR_CONNECTION;
}
virtual ~async_protocol_handler()
@ -332,11 +331,11 @@ public:
m_config.del_connection(this);
}
for (size_t i = 0; i < 60 * 1000 / 100 && 0 != boost::interprocess::ipcdetail::atomic_read32(&m_wait_count); ++i)
for (size_t i = 0; i < 60 * 1000 / 100 && 0 != m_wait_count; ++i)
{
misc_utils::sleep_no_w(100);
}
CHECK_AND_ASSERT_MES_NO_RET(0 == boost::interprocess::ipcdetail::atomic_read32(&m_wait_count), "Failed to wait for operation completion. m_wait_count = " << m_wait_count);
CHECK_AND_ASSERT_MES_NO_RET(0 == m_wait_count, "Failed to wait for operation completion. m_wait_count = " << m_wait_count.load());
MTRACE(m_connection_context << "~async_protocol_handler()");
@ -352,13 +351,13 @@ public:
MERROR(m_connection_context << "[levin_protocol] -->> start_outer_call failed");
return false;
}
boost::interprocess::ipcdetail::atomic_inc32(&m_wait_count);
++m_wait_count;
return true;
}
bool finish_outer_call()
{
MTRACE(m_connection_context << "[levin_protocol] <<-- finish_outer_call");
boost::interprocess::ipcdetail::atomic_dec32(&m_wait_count);
--m_wait_count;
m_pservice_endpoint->release();
return true;
}
@ -382,7 +381,7 @@ public:
bool close()
{
boost::interprocess::ipcdetail::atomic_inc32(&m_close_called);
++m_close_called;
m_pservice_endpoint->close();
return true;
@ -408,7 +407,7 @@ public:
virtual bool handle_recv(const void* ptr, size_t cb)
{
if(boost::interprocess::ipcdetail::atomic_read32(&m_close_called))
if(m_close_called)
return false; //closing connections
if(!m_config.m_pcommands_handler)
@ -524,7 +523,7 @@ public:
{
invoke_response_handlers_guard.unlock();
//use sync call scenario
if(!boost::interprocess::ipcdetail::atomic_read32(&m_wait_count) && !boost::interprocess::ipcdetail::atomic_read32(&m_close_called))
if(!m_wait_count && !m_close_called)
{
MERROR(m_connection_context << "no active invoke when response came, wtf?");
return false;
@ -535,7 +534,7 @@ public:
buff_to_invoke = epee::span<const uint8_t>((const uint8_t*)NULL, 0);
m_invoke_result_code = m_current_head.m_return_code;
CRITICAL_REGION_END();
boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 1);
m_invoke_buf_ready = true;
}
}
}else
@ -642,7 +641,7 @@ public:
{
CRITICAL_REGION_LOCAL(m_call_lock);
boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0);
m_invoke_buf_ready = false;
CRITICAL_REGION_BEGIN(m_invoke_response_handlers_lock);
if (command == m_connection_context.handshake_command())
@ -681,7 +680,7 @@ public:
CRITICAL_REGION_LOCAL(m_call_lock);
boost::interprocess::ipcdetail::atomic_write32(&m_invoke_buf_ready, 0);
m_invoke_buf_ready = false;
if (command == m_connection_context.handshake_command())
m_max_packet_size = m_config.m_max_packet_size;
@ -695,7 +694,7 @@ public:
uint64_t ticks_start = misc_utils::get_tick_count();
size_t prev_size = 0;
while(!boost::interprocess::ipcdetail::atomic_read32(&m_invoke_buf_ready) && !m_protocol_released)
while(!m_invoke_buf_ready && !m_protocol_released)
{
if(m_cache_in_buffer.size() - prev_size >= MIN_BYTES_WANTED)
{

View file

@ -42,7 +42,6 @@
#include <boost/thread/future.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/interprocess/detail/atomic.hpp>
#include <boost/system/error_code.hpp>
#include <boost/utility/string_ref.hpp>
#include <functional>
@ -110,7 +109,7 @@ namespace net_utils
m_initialized(true),
m_connected(false),
m_deadline(m_io_service, std::chrono::steady_clock::time_point::max()),
m_shutdowned(0),
m_shutdowned(false),
m_bytes_sent(0),
m_bytes_received(0)
{
@ -435,7 +434,7 @@ namespace net_utils
async_read(&buff[0], max_size, boost::asio::transfer_at_least(1), hndlr);
// Block until the asynchronous operation has completed.
while (ec == boost::asio::error::would_block && !boost::interprocess::ipcdetail::atomic_read32(&m_shutdowned))
while (ec == boost::asio::error::would_block && !m_shutdowned)
{
m_io_service.reset();
m_io_service.run_one();
@ -519,7 +518,7 @@ namespace net_utils
async_read((char*)buff.data(), buff.size(), boost::asio::transfer_at_least(buff.size()), hndlr);
// Block until the asynchronous operation has completed.
while (ec == boost::asio::error::would_block && !boost::interprocess::ipcdetail::atomic_read32(&m_shutdowned))
while (ec == boost::asio::error::would_block && !m_shutdowned)
{
m_io_service.run_one();
}
@ -576,7 +575,7 @@ namespace net_utils
m_ssl_socket->next_layer().close(ec);
if(ec)
MDEBUG("Problems at close: " << ec.message());
boost::interprocess::ipcdetail::atomic_write32(&m_shutdowned, 1);
m_shutdowned = true;
m_connected = false;
return true;
}
@ -685,7 +684,7 @@ namespace net_utils
bool m_initialized;
bool m_connected;
boost::asio::steady_timer m_deadline;
volatile uint32_t m_shutdowned;
std::atomic<bool> m_shutdowned;
std::atomic<uint64_t> m_bytes_sent;
std::atomic<uint64_t> m_bytes_received;
};

View file

@ -42,11 +42,9 @@
#include <vector>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <atomic>
#include <boost/array.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/interprocess/detail/atomic.hpp>
#include <boost/thread/thread.hpp>
#include "syncobj.h"

View file

@ -28,7 +28,7 @@
#ifndef _REG_EXP_DEFINER_H_
#define _REG_EXP_DEFINER_H_
#include <boost/interprocess/detail/atomic.hpp>
#include <atomic>
#include <boost/regex.hpp>
#include "syncobj.h"
@ -46,38 +46,38 @@ namespace epee
const static global_regexp_critical_section gregexplock;
#define STATIC_REGEXP_EXPR_1(var_name, xpr_text, reg_exp_flags) \
static volatile uint32_t regexp_initialized_1 = 0;\
static std::atomic<bool> regexp_initialized_1(false);\
volatile uint32_t local_is_initialized_1 = regexp_initialized_1;\
if(!local_is_initialized_1)\
gregexplock.get_lock().lock();\
static const boost::regex var_name(xpr_text , reg_exp_flags);\
if(!local_is_initialized_1)\
{\
boost::interprocess::ipcdetail::atomic_write32(&regexp_initialized_1, 1);\
regexp_initialized_1 = true;\
gregexplock.get_lock().unlock();\
}
#define STATIC_REGEXP_EXPR_2(var_name, xpr_text, reg_exp_flags) \
static volatile uint32_t regexp_initialized_2 = 0;\
static std::atomic<bool> regexp_initialized_2(false);\
volatile uint32_t local_is_initialized_2 = regexp_initialized_2;\
if(!local_is_initialized_2)\
gregexplock.get_lock().lock().lock();\
static const boost::regex var_name(xpr_text , reg_exp_flags);\
if(!local_is_initialized_2)\
{\
boost::interprocess::ipcdetail::atomic_write32(&regexp_initialized_2, 1);\
regexp_initialized_2 = true;\
gregexplock.get_lock().lock().unlock();\
}
#define STATIC_REGEXP_EXPR_3(var_name, xpr_text, reg_exp_flags) \
static volatile uint32_t regexp_initialized_3 = 0;\
static std::atomic<bool> regexp_initialized_3(false);\
volatile uint32_t local_is_initialized_3 = regexp_initialized_3;\
if(!local_is_initialized_3)\
gregexplock.get_lock().lock().lock();\
static const boost::regex var_name(xpr_text , reg_exp_flags);\
if(!local_is_initialized_3)\
{\
boost::interprocess::ipcdetail::atomic_write32(&regexp_initialized_3, 1);\
regexp_initialized_3 = true;\
gregexplock.get_lock().lock().unlock();\
}
}

View file

@ -30,7 +30,6 @@
#include <sstream>
#include <numeric>
#include <boost/interprocess/detail/atomic.hpp>
#include <boost/algorithm/string.hpp>
#include "misc_language.h"
#include "syncobj.h"
@ -271,13 +270,13 @@ namespace cryptonote
// restart all threads
{
CRITICAL_REGION_LOCAL(m_threads_lock);
boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1);
m_stop = true;
while (m_threads_active > 0)
misc_utils::sleep_no_w(100);
m_threads.clear();
}
boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0);
boost::interprocess::ipcdetail::atomic_write32(&m_thread_index, 0);
m_stop = false;
m_thread_index = 0;
for(size_t i = 0; i != m_threads_total; i++)
m_threads.push_back(boost::thread(m_attrs, boost::bind(&miner::worker_thread, this)));
}
@ -394,8 +393,8 @@ namespace cryptonote
request_block_template();//lets update block template
boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0);
boost::interprocess::ipcdetail::atomic_write32(&m_thread_index, 0);
m_stop = false;
m_thread_index = 0;
set_is_background_mining_enabled(do_background);
set_ignore_battery(ignore_battery);
@ -435,7 +434,7 @@ namespace cryptonote
//-----------------------------------------------------------------------------------------------------
void miner::send_stop_signal()
{
boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1);
m_stop = true;
}
extern "C" void rx_stop_mining(void);
//-----------------------------------------------------------------------------------------------------
@ -524,7 +523,7 @@ namespace cryptonote
//-----------------------------------------------------------------------------------------------------
bool miner::worker_thread()
{
uint32_t th_local_index = boost::interprocess::ipcdetail::atomic_inc32(&m_thread_index);
const uint32_t th_local_index = m_thread_index++; // atomically increment, getting value before increment
MLOG_SET_THREAD_NAME(std::string("[miner ") + std::to_string(th_local_index) + "]");
MGINFO("Miner thread was started ["<< th_local_index << "]");
uint32_t nonce = m_starter_nonce + th_local_index;

View file

@ -118,14 +118,14 @@ namespace cryptonote
};
volatile uint32_t m_stop;
std::atomic<bool> m_stop;
epee::critical_section m_template_lock;
block m_template;
std::atomic<uint32_t> m_template_no;
std::atomic<uint32_t> m_starter_nonce;
difficulty_type m_diffic;
uint64_t m_height;
volatile uint32_t m_thread_index;
std::atomic<uint32_t> m_thread_index;
volatile uint32_t m_threads_total;
std::atomic<uint32_t> m_threads_active;
std::atomic<int32_t> m_pausers_count;

View file

@ -35,7 +35,6 @@
// (may contain code and/or modifications by other developers)
// developer rfree: this code is caller of our new network code, and is modded; e.g. for rate limiting
#include <boost/interprocess/detail/atomic.hpp>
#include <list>
#include <ctime>