Remove /aos/util/thread.cc and thread.h
Removed all instances and usage of thread.cc and thread.h and replaced
with the standard thread class where relevant.
Change-Id: I992cc72ac071ac44e9172c14fecd0b4a9dd3581f
diff --git a/aos/util/BUILD b/aos/util/BUILD
index 4d7a3d1..f487892 100644
--- a/aos/util/BUILD
+++ b/aos/util/BUILD
@@ -55,7 +55,6 @@
],
deps = [
":run_command",
- ":thread",
"//aos/logging",
"//aos/testing:googletest",
],
@@ -146,20 +145,6 @@
)
cc_library(
- name = "thread",
- srcs = [
- "thread.cc",
- ],
- hdrs = [
- "thread.h",
- ],
- deps = [
- "//aos:macros",
- "@com_github_google_glog//:glog",
- ],
-)
-
-cc_library(
name = "trapezoid_profile",
srcs = [
"trapezoid_profile.cc",
diff --git a/aos/util/run_command_test.cc b/aos/util/run_command_test.cc
index 775b716..400d2f9 100644
--- a/aos/util/run_command_test.cc
+++ b/aos/util/run_command_test.cc
@@ -1,8 +1,6 @@
#include "aos/util/run_command.h"
-
#include "gtest/gtest.h"
-
-#include "aos/util/thread.h"
+#include <thread>
namespace aos {
namespace util {
@@ -38,16 +36,14 @@
TEST(RunCommandTest, MultipleThreads) {
int result1, result2;
- util::FunctionThread t1([&result1](util::Thread *) {
+ std::thread t1([&result1]() {
result1 = RunCommand("true");
});
- util::FunctionThread t2([&result2](util::Thread *) {
+ std::thread t2([&result2]() {
result2 = RunCommand("true");
});
- t1.Start();
- t2.Start();
- t1.WaitUntilDone();
- t2.WaitUntilDone();
+ t1.join();
+ t2.join();
ASSERT_NE(-1, result1);
ASSERT_NE(-1, result2);
ASSERT_TRUE(WIFEXITED(result1));
diff --git a/aos/util/thread.cc b/aos/util/thread.cc
deleted file mode 100644
index bb5999f..0000000
--- a/aos/util/thread.cc
+++ /dev/null
@@ -1,79 +0,0 @@
-#include "aos/util/thread.h"
-
-#include <pthread.h>
-#include <signal.h>
-
-#include "glog/logging.h"
-
-namespace aos {
-namespace util {
-
-Thread::Thread() : started_(false), joined_(false), should_terminate_(false) {}
-
-Thread::~Thread() {
- CHECK(!(started_ && !joined_));
-}
-
-void Thread::Start() {
- CHECK(!started_);
- started_ = true;
- CHECK(pthread_create(&thread_, NULL, &Thread::StaticRun, this) == 0);
-}
-
-void Thread::Join() {
- CHECK(!joined_ && started_);
- joined_ = true;
- should_terminate_.store(true);
- CHECK(pthread_join(thread_, NULL) == 0);
-}
-
-bool Thread::TryJoin() {
- CHECK(!joined_ && started_);
-#ifdef AOS_SANITIZER_thread
- // ThreadSanitizer misses the tryjoin and then complains about leaking the
- // thread. Instead, we'll just check if the thread is still around and then
- // do a regular Join() iff it isn't.
- // TODO(brians): Remove this once tsan learns about pthread_tryjoin_np.
- const int kill_ret = pthread_kill(thread_, 0);
- // If it's still around.
- if (kill_ret == 0) return false;
- // If it died, we'll get ESRCH. Otherwise, something went wrong.
- if (kill_ret != ESRCH) {
- errno = kill_ret;
- PLOG(FATAL) << "pthread_kill(thread_, 0) failed";
- }
- Join();
- return true;
-#else
- const int ret = pthread_tryjoin_np(thread_, nullptr);
- if (ret == 0) {
- joined_ = true;
- return true;
- } else if (ret == EBUSY) {
- return false;
- } else {
- errno = ret;
- PLOG(FATAL) << "pthread_tryjoin_np(thread_, nullptr) failed";
- return false;
- }
-#endif
-}
-
-void Thread::RequestStop() {
- CHECK(!joined_ && started_);
- should_terminate_.store(true);
-}
-
-void Thread::WaitUntilDone() {
- CHECK(!joined_ && started_);
- joined_ = true;
- CHECK(pthread_join(thread_, NULL) == 0);
-}
-
-void *Thread::StaticRun(void *self) {
- static_cast<Thread *>(self)->Run();
- return NULL;
-}
-
-} // namespace util
-} // namespace aos
diff --git a/aos/util/thread.h b/aos/util/thread.h
deleted file mode 100644
index 337ea48..0000000
--- a/aos/util/thread.h
+++ /dev/null
@@ -1,90 +0,0 @@
-#ifndef AOS_UTIL_THREAD_H_
-#define AOS_UTIL_THREAD_H_
-
-#include <functional>
-#include <atomic>
-
-#include <pthread.h>
-
-#include "aos/macros.h"
-
-namespace aos {
-namespace util {
-
-// A nice wrapper around a pthreads thread.
-//
-// TODO(aschuh): replace this with std::thread
-class Thread {
- public:
- Thread();
- virtual ~Thread();
-
- // Actually creates the thread.
- void Start();
-
- // Asks the code to stop and then waits until it has done so.
- // This or TryJoin() (returning true) must be called exactly once for every
- // instance.
- void Join();
-
- // If the code has already finished, returns true. Does not block waiting if
- // it isn't.
- // Join() must not be called on this instance if this returns true.
- // This must return true or Join() must be called exactly once for every
- // instance.
- bool TryJoin();
-
- // Asks the code to stop (in preparation for a Join()).
- void RequestStop();
-
- // Waits until the code has stopped. Does not ask it to do so.
- void WaitUntilDone();
-
- protected:
- // Subclasses need to call this periodically if they are going to loop to
- // check whether they have been asked to stop.
- bool should_continue() {
- return !should_terminate_.load();
- }
-
- private:
- // Where subclasses actually do something.
- //
- // They should not block for long periods of time without checking
- // should_continue().
- virtual void Run() = 0;
-
- static void *StaticRun(void *self);
-
- pthread_t thread_;
- bool started_;
- bool joined_;
- ::std::atomic_bool should_terminate_;
-
- DISALLOW_COPY_AND_ASSIGN(Thread);
-};
-
-class FunctionThread : public Thread {
- public:
- FunctionThread(::std::function<void(FunctionThread *)> function)
- : function_(function) {}
-
- // Runs function in a new thread and waits for it to return.
- static void RunInOtherThread(::std::function<void()> function) {
- FunctionThread t([&function](FunctionThread *) { function(); });
- t.Start();
- t.Join();
- }
-
- private:
- virtual void Run() override {
- function_(this);
- }
-
- const ::std::function<void(FunctionThread *)> function_;
-};
-
-} // namespace util
-} // namespace aos
-
-#endif // AOS_UTIL_THREAD_H_