1 // Copyright 2016 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROMIUMOS_WIDE_PROFILING_COMPAT_EXT_DETAIL_THREAD_H_ 6 #define CHROMIUMOS_WIDE_PROFILING_COMPAT_EXT_DETAIL_THREAD_H_ 7 #include <chrono> 8 #include <condition_variable> 9 #include <mutex> 10 #include <thread> 11 12 namespace quipper { 13 class Thread : public quipper::compat::ThreadInterface { 14 public: Thread(const string & name_prefix)15 explicit Thread(const string& name_prefix) {} 16 Start()17 void Start() override { thread_ = std::thread(&Thread::Run, this); } 18 Join()19 void Join() override { thread_.join(); } 20 tid()21 pid_t tid() override { return thread_.native_handle(); } 22 23 protected: 24 void Run() override = 0; 25 26 private: 27 std::thread thread_; 28 }; 29 30 class Notification : public quipper::compat::NotificationInterface { 31 public: Wait()32 void Wait() override { 33 std::unique_lock<std::mutex> lock(mutex_); 34 event_.wait(lock); 35 } 36 WaitWithTimeout(int timeout_ms)37 bool WaitWithTimeout(int timeout_ms) override { 38 std::unique_lock<std::mutex> lock(mutex_); 39 return event_.wait_for(lock, std::chrono::milliseconds(timeout_ms)) == 40 std::cv_status::no_timeout; 41 } 42 Notify()43 void Notify() override { event_.notify_all(); } 44 45 private: 46 std::condition_variable event_; 47 std::mutex mutex_; 48 }; 49 50 } // namespace quipper 51 52 #endif // CHROMIUMOS_WIDE_PROFILING_COMPAT_EXT_DETAIL_THREAD_H_ 53