fix locking in count-peers thread (2)

This commit is contained in:
rfree2monero 2015-02-24 21:02:48 +01:00
parent 0198ffb220
commit f79821ac7e
4 changed files with 28 additions and 12 deletions

View file

@ -320,7 +320,7 @@ int main(int argc, char* argv[])
ccore.set_cryptonote_protocol(NULL); ccore.set_cryptonote_protocol(NULL);
cprotocol.set_p2p_endpoint(NULL); cprotocol.set_p2p_endpoint(NULL);
epee::net_utils::data_logger::get_instance().kill_instance(); epee::net_utils::data_logger::kill_instance();
LOG_PRINT("Node stopped.", LOG_LEVEL_0); LOG_PRINT("Node stopped.", LOG_LEVEL_0);
return 0; return 0;

View file

@ -20,6 +20,12 @@ namespace net_utils
m_state = data_logger_state::state_ready_to_use; m_state = data_logger_state::state_ready_to_use;
} }
); );
if (m_state != data_logger_state::state_ready_to_use) {
_erro ("trying to use not working data_logger");
throw std::runtime_error("data_logger ctor state");
}
return * m_obj; return * m_obj;
} }
@ -82,7 +88,7 @@ namespace net_utils
} }
void data_logger::kill_instance() { void data_logger::kill_instance() {
m_state = m_state = data_logger_state::state_dying; m_state = data_logger_state::state_dying;
m_obj.reset(); m_obj.reset();
} }

View file

@ -89,6 +89,7 @@ namespace nodetool
{ {
m_current_number_of_out_peers = 0; m_current_number_of_out_peers = 0;
m_save_graph = false; m_save_graph = false;
is_closing = false;
} }
static void init_options(boost::program_options::options_description& desc); static void init_options(boost::program_options::options_description& desc);
@ -209,6 +210,13 @@ namespace nodetool
bool set_rate_down_limit(const boost::program_options::variables_map& vm, int64_t limit); bool set_rate_down_limit(const boost::program_options::variables_map& vm, int64_t limit);
bool set_rate_limit(const boost::program_options::variables_map& vm, uint64_t limit); bool set_rate_limit(const boost::program_options::variables_map& vm, uint64_t limit);
void kill() { ///< will be called e.g. from deinit()
_info("Killing the net_node");
is_closing = true;
mPeersLoggerThread->join(); // make sure the thread finishes
_info("Joined extra background net_node threads");
}
//debug functions //debug functions
std::string print_connections_container(); std::string print_connections_container();
@ -247,7 +255,8 @@ namespace nodetool
bool m_hide_my_port; bool m_hide_my_port;
bool m_no_igd; bool m_no_igd;
std::atomic<bool> m_save_graph; std::atomic<bool> m_save_graph;
std::atomic<bool> is_closing;
std::unique_ptr<std::thread> mPeersLoggerThread;
//critical_section m_connections_lock; //critical_section m_connections_lock;
//connections_indexed_container m_connections; //connections_indexed_container m_connections;

View file

@ -291,17 +291,17 @@ namespace nodetool
std::vector<std::vector<std::string>> dns_results; std::vector<std::vector<std::string>> dns_results;
dns_results.resize(m_seed_nodes_list.size()); dns_results.resize(m_seed_nodes_list.size());
std::shared_ptr<std::thread> peersLoggerThread (new std::thread([&]() // creating thread to log number of connections
mPeersLoggerThread.reset(new std::thread([&]()
{ {
unsigned int number_of_peers; _note("Thread monitor number of peers - start");
while (1) while (!is_closing)
{ { // main loop of thread
//number_of_peers = m_net_server.get_config_object().get_connections_count(); //number_of_peers = m_net_server.get_config_object().get_connections_count();
number_of_peers = 0; unsigned int number_of_peers = 0;
m_net_server.get_config_object().foreach_connection([&](const p2p_connection_context& cntxt) m_net_server.get_config_object().foreach_connection([&](const p2p_connection_context& cntxt)
{ {
if(!cntxt.m_is_income) if (!cntxt.m_is_income) ++number_of_peers;
++number_of_peers;
return true; return true;
}); // lambda }); // lambda
@ -309,10 +309,10 @@ namespace nodetool
epee::net_utils::data_logger::get_instance().add_data("peers", number_of_peers); epee::net_utils::data_logger::get_instance().add_data("peers", number_of_peers);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} } // main loop of thread
_note("Thread monitor number of peers - done");
})); // lambda })); // lambda
peersLoggerThread->detach();
std::list<boost::thread*> dns_threads; std::list<boost::thread*> dns_threads;
uint64_t result_index = 0; uint64_t result_index = 0;
@ -509,6 +509,7 @@ namespace nodetool
template<class t_payload_net_handler> template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::deinit() bool node_server<t_payload_net_handler>::deinit()
{ {
kill();
m_peerlist.deinit(); m_peerlist.deinit();
m_net_server.deinit_server(); m_net_server.deinit_server();