libfoedus-core
FOEDUS Core Library
stoppable_thread_impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_THREAD_STOPPABLE_THREAD_IMPL_HPP_
19 #define FOEDUS_THREAD_STOPPABLE_THREAD_IMPL_HPP_
20 #include <atomic>
21 #include <chrono>
22 #include <iosfwd>
23 #include <string>
24 #include <thread>
25 
27 
28 namespace foedus {
29 namespace thread {
45 class StoppableThread final {
46  public:
47  StoppableThread() : sleep_interval_(0),
48  started_(false), stop_requested_(false), stopped_(false) {}
49 
50  // non-copyable assignable. (maybe better to provide move, but no need so far)
51  StoppableThread(const StoppableThread &other) = delete;
52  StoppableThread& operator=(const StoppableThread &other) = delete;
53  StoppableThread(StoppableThread &&other) = delete;
54  StoppableThread& operator=(StoppableThread &&other) = delete;
55 
57  void initialize(const std::string &name,
58  std::thread &&the_thread, const std::chrono::microseconds &sleep_interval);
60  void initialize(const std::string &name_prefix, int32_t name_ordinal,
61  std::thread &&the_thread, const std::chrono::microseconds &sleep_interval);
62 
67  void stop();
68 
74  void request_stop();
75 
79  void wait_for_stop();
80 
85  void wakeup();
86 
100  bool sleep();
101 
103  bool is_stop_requested() const { return stop_requested_; }
105  bool is_stop_requested_weak() const {
106  return stop_requested_.load(std::memory_order_relaxed);
107  }
108 
110  bool is_stopped() const { return stopped_; }
112  bool is_stopped_weak() const { return stopped_.load(std::memory_order_relaxed); }
113 
115  template <typename HANDLE_TYPE>
116  HANDLE_TYPE native_handle() { return static_cast<HANDLE_TYPE>(thread_.native_handle()); }
117 
118  std::string to_string() const;
119  friend std::ostream& operator<<(std::ostream& o, const StoppableThread& v);
120 
121  private:
123  std::string name_;
125  std::thread thread_;
127  std::chrono::microseconds sleep_interval_;
129  ConditionVariable condition_;
131  std::atomic<bool> started_;
133  std::atomic<bool> stop_requested_;
135  std::atomic<bool> stopped_;
136 };
137 
138 
139 } // namespace thread
140 } // namespace foedus
141 #endif // FOEDUS_THREAD_STOPPABLE_THREAD_IMPL_HPP_
bool is_stopped() const
returns whether this thread has stopped (if the thread hasn't started, false too).
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
void initialize(const std::string &name, std::thread &&the_thread, const std::chrono::microseconds &sleep_interval)
Initializes this object for the given thread.
void request_stop()
If the thread is still running, requests the thread to stop and waits until it exists.
void stop()
request_stop() plus wait_for_stop().
void wait_for_stop()
Blocks until the thread stops.
StoppableThread & operator=(const StoppableThread &other)=delete
An analogue of pthread's condition variable and std::condition_variable to avoid glibc's bug in pthre...
bool is_stop_requested() const
returns whether someone has requested to stop this.
HANDLE_TYPE native_handle()
wraps std::thread::native_handle()
void wakeup()
If the thread is still running and also sleeping, requests the thread to immediately wakeup and do it...
bool is_stop_requested_weak() const
non-atomic is_stop_requested().
The frequently appearing quartet of std::thread, condition_varible, stop-request flag, and mutex.
friend std::ostream & operator<<(std::ostream &o, const StoppableThread &v)
bool sleep()
Sleep until the interval elapses or someone requests to stop this thread.
bool is_stopped_weak() const
non-atomic is_stopped().