Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide worker_handle and move it to cxx file #137

Merged
merged 1 commit into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions include/libnuraft/global_mgr.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
#pragma once

#include "basic_types.hxx"
#include "event_awaiter.h"
#include "pp_util.hxx"
#include "ptr.hxx"

Expand Down Expand Up @@ -121,22 +120,7 @@ public:
void request_commit(ptr<raft_server> server);

private:
struct worker_handle {
worker_handle(size_t id = 0);
~worker_handle();
void shutdown();

enum status {
SLEEPING = 0,
WORKING = 1,
};

size_t id_;
EventAwaiter ea_;
ptr<std::thread> thread_;
bool stopping_;
std::atomic<status> status_;
};
struct worker_handle;

static std::mutex instance_lock_;
static std::atomic<nuraft_global_mgr*> instance_;
Expand Down
50 changes: 31 additions & 19 deletions src/global_mgr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.

#include "global_mgr.hxx"

#include "event_awaiter.h"
#include "logger.hxx"
#include "raft_server.hxx"
#include "tracer.hxx"
Expand All @@ -28,29 +29,40 @@ namespace nuraft {
std::atomic<nuraft_global_mgr*> nuraft_global_mgr::instance_(nullptr);
std::mutex nuraft_global_mgr::instance_lock_;

struct nuraft_global_mgr::worker_handle {
worker_handle(size_t id = 0)
: id_(id)
, thread_(nullptr)
, stopping_(false)
, status_(SLEEPING)
{}

nuraft_global_mgr::worker_handle::worker_handle(size_t id)
: id_(id)
, thread_(nullptr)
, stopping_(false)
, status_(SLEEPING)
{
}

nuraft_global_mgr::worker_handle::~worker_handle() {
shutdown();
}
~worker_handle() {
shutdown();
}

void nuraft_global_mgr::worker_handle::shutdown() {
stopping_ = true;
if (thread_) {
if (thread_->joinable()) {
ea_.invoke();
thread_->join();
void shutdown() {
stopping_ = true;
if (thread_) {
if (thread_->joinable()) {
ea_.invoke();
thread_->join();
}
thread_.reset();
}
thread_.reset();
}
}

enum status {
SLEEPING = 0,
WORKING = 1,
};

size_t id_;
EventAwaiter ea_;
ptr<std::thread> thread_;
bool stopping_;
std::atomic<status> status_;
};

nuraft_global_mgr::nuraft_global_mgr()
: thread_id_counter_(0)
Expand Down