Skip to content

Commit

Permalink
Get rid of auto_destoyer
Browse files Browse the repository at this point in the history
* The destruction order of global static variables is non-deterministic.
  • Loading branch information
greensky00 committed Apr 5, 2021
1 parent 365f3ae commit 9ee5c25
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 84 deletions.
24 changes: 7 additions & 17 deletions include/libnuraft/global_mgr.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ struct nuraft_global_config {

static nuraft_global_config __DEFAULT_NURAFT_GLOBAL_CONFIG;

// Singleton class.
class nuraft_global_mgr {
public:
nuraft_global_mgr();

~nuraft_global_mgr();

__nocopy__(nuraft_global_mgr);
public:

/**
* Initialize the global instance.
*
Expand Down Expand Up @@ -145,12 +151,6 @@ public:
private:
struct worker_handle;

nuraft_global_mgr();

~nuraft_global_mgr();

__nocopy__(nuraft_global_mgr);

/**
* Initialize thread pool with the given config.
*/
Expand All @@ -166,16 +166,6 @@ private:
*/
void append_worker_loop(ptr<worker_handle> handle);

/**
* Lock for global manager instance.
*/
static std::mutex instance_lock_;

/**
* Global manager instance.
*/
static std::atomic<nuraft_global_mgr*> instance_;

/**
* Lock for global Asio service instance.
*/
Expand Down
57 changes: 42 additions & 15 deletions src/global_mgr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,44 @@ limitations under the License.
#include "raft_server.hxx"
#include "tracer.hxx"

#include <memory>

namespace nuraft {

std::atomic<nuraft_global_mgr*> nuraft_global_mgr::instance_(nullptr);
std::mutex nuraft_global_mgr::instance_lock_;
class ngm_singleton {
public:
static ngm_singleton& get_instance() {
static ngm_singleton instance;
return instance;
}

nuraft_global_mgr* get() {
return internal_.get();
}

bool create() {
std::lock_guard<std::mutex> l(lock_);
if (internal_.get()) {
// Already created.
return false;
}
// C++11 doesn't have `make_unique`.
internal_ =
std::move( std::unique_ptr<nuraft_global_mgr>( new nuraft_global_mgr() ) );
return true;
}

void clear() {
std::lock_guard<std::mutex> l(lock_);
internal_.reset();
}

private:
ngm_singleton() : internal_(nullptr) {}

std::unique_ptr<nuraft_global_mgr> internal_;
std::mutex lock_;
};

struct nuraft_global_mgr::worker_handle {
worker_handle(size_t id = 0)
Expand Down Expand Up @@ -84,13 +118,11 @@ nuraft_global_mgr::~nuraft_global_mgr() {
}

nuraft_global_mgr* nuraft_global_mgr::init(const nuraft_global_config& config) {
nuraft_global_mgr* mgr = instance_.load();
nuraft_global_mgr* mgr = ngm_singleton::get_instance().get();
if (!mgr) {
std::lock_guard<std::mutex> l(instance_lock_);
mgr = instance_.load();
if (!mgr) {
mgr = new nuraft_global_mgr();
instance_.store(mgr);
bool created = ngm_singleton::get_instance().create();
mgr = ngm_singleton::get_instance().get();
if (created) {
mgr->config_ = config;
mgr->init_thread_pool();
}
Expand All @@ -99,16 +131,11 @@ nuraft_global_mgr* nuraft_global_mgr::init(const nuraft_global_config& config) {
}

void nuraft_global_mgr::shutdown() {
std::lock_guard<std::mutex> l(instance_lock_);
nuraft_global_mgr* mgr = instance_.load();
if (mgr) {
delete mgr;
instance_.store(nullptr);
}
ngm_singleton::get_instance().clear();
}

nuraft_global_mgr* nuraft_global_mgr::get_instance() {
return instance_.load();
return ngm_singleton::get_instance().get();
}

void nuraft_global_mgr::init_thread_pool() {
Expand Down
8 changes: 0 additions & 8 deletions src/raft_server.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ const int raft_server::default_snapshot_sync_block_size = 4 * 1024;

raft_server::limits raft_server::raft_limits_;

struct auto_destroyer {
~auto_destroyer() {
stat_mgr::destroy();
nuraft_global_mgr::shutdown();
}
};
static auto_destroyer auto_destroyer_;

raft_server::raft_server(context* ctx, const init_options& opt)
: bg_append_ea_(nullptr)
, initialized_(false)
Expand Down
40 changes: 3 additions & 37 deletions src/stat_mgr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ limitations under the License.

namespace nuraft {

std::atomic<stat_mgr*> stat_mgr::instance_(nullptr);
std::mutex stat_mgr::instance_lock_;

// === stat_elem ==============================================================

stat_elem::stat_elem(Type _type, const std::string& _name)
Expand Down Expand Up @@ -53,43 +50,12 @@ stat_mgr::~stat_mgr() {
for (auto& entry: stat_map_) {
delete entry.second;
}
}

stat_mgr* stat_mgr::init() {
stat_mgr* mgr = instance_.load();
if (!mgr) {
std::lock_guard<std::mutex> l(instance_lock_);
mgr = instance_.load();
if (!mgr) {
mgr = new stat_mgr();
instance_.store(mgr);
}
}
return mgr;
stat_map_.clear();
}

stat_mgr* stat_mgr::get_instance() {
#ifndef ENABLE_RAFT_STATS
static stat_mgr dummy_mgr;
return &dummy_mgr;
#endif

stat_mgr* mgr = instance_.load();
if (!mgr) return init();
return mgr;
}

void stat_mgr::destroy() {
#ifndef ENABLE_RAFT_STATS
return;
#endif

std::lock_guard<std::mutex> l(instance_lock_);
stat_mgr* mgr = instance_.load();
if (mgr) {
delete mgr;
instance_.store(nullptr);
}
static stat_mgr mgr_instance;
return &mgr_instance;
}

stat_elem* stat_mgr::get_stat(const std::string& stat_name) {
Expand Down
7 changes: 0 additions & 7 deletions src/stat_mgr.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,8 @@ private:
// Singleton class
class stat_mgr {
public:
static stat_mgr* init();

static stat_mgr* get_instance();

static void destroy();

stat_elem* get_stat(const std::string& stat_name);

stat_elem* create_stat(stat_elem::Type type, const std::string& stat_name);
Expand All @@ -180,9 +176,6 @@ public:
void reset_all_stats();

private:
static std::mutex instance_lock_;
static std::atomic<stat_mgr*> instance_;

stat_mgr();
~stat_mgr();

Expand Down

0 comments on commit 9ee5c25

Please sign in to comment.