Skip to content

Commit

Permalink
fix locking in count-peers thread (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfree2monero committed Feb 24, 2015
1 parent 0198ffb commit f79821a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ int main(int argc, char* argv[])
ccore.set_cryptonote_protocol(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);
return 0;

Expand Down
8 changes: 7 additions & 1 deletion src/p2p/data_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ namespace net_utils
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;
}

Expand Down Expand Up @@ -82,7 +88,7 @@ namespace net_utils
}

void data_logger::kill_instance() {
m_state = m_state = data_logger_state::state_dying;
m_state = data_logger_state::state_dying;
m_obj.reset();
}

Expand Down
11 changes: 10 additions & 1 deletion src/p2p/net_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace nodetool
{
m_current_number_of_out_peers = 0;
m_save_graph = false;
is_closing = false;
}

static void init_options(boost::program_options::options_description& desc);
Expand Down Expand Up @@ -209,6 +210,13 @@ namespace nodetool
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);

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
std::string print_connections_container();

Expand Down Expand Up @@ -247,7 +255,8 @@ namespace nodetool
bool m_hide_my_port;
bool m_no_igd;
std::atomic<bool> m_save_graph;

std::atomic<bool> is_closing;
std::unique_ptr<std::thread> mPeersLoggerThread;
//critical_section m_connections_lock;
//connections_indexed_container m_connections;

Expand Down
19 changes: 10 additions & 9 deletions src/p2p/net_node.inl
Original file line number Diff line number Diff line change
Expand Up @@ -291,28 +291,28 @@ namespace nodetool
std::vector<std::vector<std::string>> dns_results;
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;
while (1)
{
_note("Thread monitor number of peers - start");
while (!is_closing)
{ // main loop of thread
//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)
{
if(!cntxt.m_is_income)
++number_of_peers;
if (!cntxt.m_is_income) ++number_of_peers;
return true;
}); // lambda

m_current_number_of_out_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));
}
} // main loop of thread
_note("Thread monitor number of peers - done");
})); // lambda

peersLoggerThread->detach();

std::list<boost::thread*> dns_threads;
uint64_t result_index = 0;
Expand Down Expand Up @@ -509,6 +509,7 @@ namespace nodetool
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::deinit()
{
kill();
m_peerlist.deinit();
m_net_server.deinit_server();

Expand Down

0 comments on commit f79821a

Please sign in to comment.