From 17772ef53e57620f47c426fa4557671a86f1b3ae Mon Sep 17 00:00:00 2001 From: Jeffrey Date: Wed, 30 Mar 2022 13:18:32 -0500 Subject: 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`. Sometimes, a member was being used a boolean flag, so I replaced it with `std::atomic`. 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 --- contrib/epee/include/net/abstract_tcp_server2.h | 3 +-- contrib/epee/include/net/abstract_tcp_server2.inl | 10 +++---- contrib/epee/include/net/connection_basic.hpp | 2 +- .../include/net/levin_protocol_handler_async.h | 31 +++++++++++----------- contrib/epee/include/net/net_helper.h | 11 ++++---- contrib/epee/include/net/network_throttle.hpp | 2 -- contrib/epee/include/reg_exp_definer.h | 14 +++++----- src/cryptonote_basic/miner.cpp | 15 +++++------ src/cryptonote_basic/miner.h | 4 +-- .../cryptonote_protocol_handler.inl | 1 - 10 files changed, 43 insertions(+), 50 deletions(-) diff --git a/contrib/epee/include/net/abstract_tcp_server2.h b/contrib/epee/include/net/abstract_tcp_server2.h index f40cd108a..51aa9f275 100644 --- a/contrib/epee/include/net/abstract_tcp_server2.h +++ b/contrib/epee/include/net/abstract_tcp_server2.h @@ -49,7 +49,6 @@ #include #include #include -#include #include #include "byte_slice.h" #include "net_utils_base.h" @@ -393,7 +392,7 @@ namespace net_utils std::vector > 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 m_thread_index; t_connection_type m_connection_type; diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl index 89971bea2..58cec5520 100644 --- a/contrib/epee/include/net/abstract_tcp_server2.inl +++ b/contrib/epee/include/net/abstract_tcp_server2.inl @@ -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::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); diff --git a/contrib/epee/include/net/connection_basic.hpp b/contrib/epee/include/net/connection_basic.hpp index 23873f65b..01599aa4e 100644 --- a/contrib/epee/include/net/connection_basic.hpp +++ b/contrib/epee/include/net/connection_basic.hpp @@ -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 m_want_close_connection; std::atomic m_was_shutdown; critical_section m_send_que_lock; std::deque m_send_que; diff --git a/contrib/epee/include/net/levin_protocol_handler_async.h b/contrib/epee/include/net/levin_protocol_handler_async.h index a6816cafc..7cc42b5c4 100644 --- a/contrib/epee/include/net/levin_protocol_handler_async.h +++ b/contrib/epee/include/net/levin_protocol_handler_async.h @@ -28,7 +28,6 @@ #include #include #include -#include #include #include @@ -166,7 +165,7 @@ public: }; std::atomic m_protocol_released; - volatile uint32_t m_invoke_buf_ready; + std::atomic 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 m_wait_count; + std::atomic 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*)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) { diff --git a/contrib/epee/include/net/net_helper.h b/contrib/epee/include/net/net_helper.h index a1f44cab0..0a35797fd 100644 --- a/contrib/epee/include/net/net_helper.h +++ b/contrib/epee/include/net/net_helper.h @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -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 m_shutdowned; std::atomic m_bytes_sent; std::atomic m_bytes_received; }; diff --git a/contrib/epee/include/net/network_throttle.hpp b/contrib/epee/include/net/network_throttle.hpp index b6f7592c2..73caa396c 100644 --- a/contrib/epee/include/net/network_throttle.hpp +++ b/contrib/epee/include/net/network_throttle.hpp @@ -42,11 +42,9 @@ #include #include #include -#include #include #include -#include #include #include "syncobj.h" diff --git a/contrib/epee/include/reg_exp_definer.h b/contrib/epee/include/reg_exp_definer.h index 386a45f9a..d1b800a32 100644 --- a/contrib/epee/include/reg_exp_definer.h +++ b/contrib/epee/include/reg_exp_definer.h @@ -28,7 +28,7 @@ #ifndef _REG_EXP_DEFINER_H_ #define _REG_EXP_DEFINER_H_ -#include +#include #include #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 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(®exp_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 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(®exp_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 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(®exp_initialized_3, 1);\ + regexp_initialized_3 = true;\ gregexplock.get_lock().lock().unlock();\ } } diff --git a/src/cryptonote_basic/miner.cpp b/src/cryptonote_basic/miner.cpp index ae514aac6..f35a87d47 100644 --- a/src/cryptonote_basic/miner.cpp +++ b/src/cryptonote_basic/miner.cpp @@ -30,7 +30,6 @@ #include #include -#include #include #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; diff --git a/src/cryptonote_basic/miner.h b/src/cryptonote_basic/miner.h index df3f56f68..5da81e3d1 100644 --- a/src/cryptonote_basic/miner.h +++ b/src/cryptonote_basic/miner.h @@ -118,14 +118,14 @@ namespace cryptonote }; - volatile uint32_t m_stop; + std::atomic m_stop; epee::critical_section m_template_lock; block m_template; std::atomic m_template_no; std::atomic m_starter_nonce; difficulty_type m_diffic; uint64_t m_height; - volatile uint32_t m_thread_index; + std::atomic m_thread_index; volatile uint32_t m_threads_total; std::atomic m_threads_active; std::atomic m_pausers_count; diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 39d562fd1..ee4517184 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -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 #include #include -- cgit v1.2.3