From 9445b9de902182f413c61d9483b770b746f48d29 Mon Sep 17 00:00:00 2001 From: Dave Peterson Date: Thu, 5 Nov 2015 08:28:01 -0800 Subject: [PATCH] eliminate unnecessary TOpt from TFdManagedThread implementation --- src/thread/fd_managed_thread.cc | 17 ++++++++--------- src/thread/fd_managed_thread.h | 10 +++++----- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/thread/fd_managed_thread.cc b/src/thread/fd_managed_thread.cc index 7f9f5dc..55802a0 100644 --- a/src/thread/fd_managed_thread.cc +++ b/src/thread/fd_managed_thread.cc @@ -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"); } @@ -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"); } @@ -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(); @@ -112,7 +111,7 @@ void TFdManagedThread::Join() { void TFdManagedThread::ShutdownOnDestroy() { assert(this); - if (OptThread.IsKnown()) { + if (Thread.joinable()) { ShutdownRequestedSem.Push(); try { @@ -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()); } diff --git a/src/thread/fd_managed_thread.h b/src/thread/fd_managed_thread.h index a3ed2f3..dbd4858 100644 --- a/src/thread/fd_managed_thread.h +++ b/src/thread/fd_managed_thread.h @@ -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: @@ -162,7 +162,7 @@ namespace Thread { Base::TEventSemaphore ShutdownFinishedSem; - Base::TOpt OptThread; + std::thread Thread; bool ThreadThrewUnknownException;