Skip to content

Commit

Permalink
eliminate unnecessary TOpt from TFdManagedThread implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dspeterson committed Nov 5, 2015
1 parent 88f69ff commit 9445b9d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
17 changes: 8 additions & 9 deletions src/thread/fd_managed_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TFdManagedThread::~TFdManagedThread() noexcept {
void TFdManagedThread::Start() {
assert(this);

if (OptThread.IsKnown()) {
if (Thread.joinable()) {
throw std::logic_error("Worker thread is already started");
}

Expand All @@ -59,18 +59,18 @@ void TFdManagedThread::Start() {
assert(OptThrownByThread.IsUnknown());

/* Start the thread running. */
OptThread.MakeKnown(std::bind(&TFdManagedThread::RunAndTerminate, this));
Thread = std::thread(std::bind(&TFdManagedThread::RunAndTerminate, this));
}

bool TFdManagedThread::IsStarted() const {
assert(this);
return OptThread.IsKnown();
return Thread.joinable();
}

void TFdManagedThread::RequestShutdown() {
assert(this);

if (OptThread.IsUnknown()) {
if (!Thread.joinable()) {
throw std::logic_error(
"Cannot request shutdown on nonexistent worker thread");
}
Expand All @@ -86,13 +86,12 @@ const TFd &TFdManagedThread::GetShutdownWaitFd() const {
void TFdManagedThread::Join() {
assert(this);

if (OptThread.IsUnknown()) {
if (!Thread.joinable()) {
throw std::logic_error("Cannot join nonexistent worker thread");
}

ShutdownFinishedSem.Pop();
OptThread->join();
OptThread.Reset();
Thread.join();
assert(!ShutdownFinishedSem.GetFd().IsReadable());
ShutdownRequestedSem.Reset();

Expand All @@ -112,7 +111,7 @@ void TFdManagedThread::Join() {
void TFdManagedThread::ShutdownOnDestroy() {
assert(this);

if (OptThread.IsKnown()) {
if (Thread.joinable()) {
ShutdownRequestedSem.Push();

try {
Expand All @@ -124,7 +123,7 @@ void TFdManagedThread::ShutdownOnDestroy() {

assert(!ShutdownRequestedSem.GetFd().IsReadable());
assert(!ShutdownFinishedSem.GetFd().IsReadable());
assert(OptThread.IsUnknown());
assert(!Thread.joinable());
assert(!ThreadThrewUnknownException);
assert(OptThrownByThread.IsUnknown());
}
Expand Down
10 changes: 5 additions & 5 deletions src/thread/fd_managed_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ namespace Thread {

const std::thread &GetThread() const {
assert(this);
assert(OptThread.IsKnown());
return *OptThread;
assert(Thread.joinable());
return Thread;
}

std::thread &GetThread() {
assert(this);
assert(OptThread.IsKnown());
return *OptThread;
assert(Thread.joinable());
return Thread;
}

protected:
Expand Down Expand Up @@ -162,7 +162,7 @@ namespace Thread {

Base::TEventSemaphore ShutdownFinishedSem;

Base::TOpt<std::thread> OptThread;
std::thread Thread;

bool ThreadThrewUnknownException;

Expand Down

0 comments on commit 9445b9d

Please sign in to comment.