Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 1 | #ifndef AOS_COMMON_UTIL_THREAD_H_ |
| 2 | #define AOS_COMMON_UTIL_THREAD_H_ |
| 3 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 4 | #include <functional> |
Brian Silverman | d4c4832 | 2014-09-06 21:17:29 -0400 | [diff] [blame^] | 5 | #include <atomic> |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 6 | |
Brian Silverman | d4c4832 | 2014-09-06 21:17:29 -0400 | [diff] [blame^] | 7 | #include <pthread.h> |
| 8 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 9 | #include "aos/common/macros.h" |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | namespace util { |
| 13 | |
| 14 | // A nice wrapper around a pthreads thread. |
| 15 | // |
| 16 | // TODO(aschuh): Test this. |
| 17 | class Thread { |
| 18 | public: |
| 19 | Thread(); |
Brian Silverman | 3d5e697 | 2013-09-06 17:30:36 -0700 | [diff] [blame] | 20 | virtual ~Thread(); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 21 | |
| 22 | // Actually creates the thread. |
| 23 | void Start(); |
| 24 | |
| 25 | // Asks the code to stop and then waits until it has done so. |
| 26 | void Join(); |
| 27 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 28 | // Waits until the code has stopped. Does not ask it to do so. |
| 29 | void WaitUntilDone(); |
| 30 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 31 | protected: |
| 32 | // Subclasses need to call this periodically if they are going to loop to |
| 33 | // check whether they have been asked to stop. |
| 34 | bool should_continue() { |
Brian Silverman | d4c4832 | 2014-09-06 21:17:29 -0400 | [diff] [blame^] | 35 | return !should_terminate_.load(); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | private: |
| 39 | // Where subclasses actually do something. |
| 40 | // |
| 41 | // They should not block for long periods of time without checking |
| 42 | // should_continue(). |
| 43 | virtual void Run() = 0; |
| 44 | |
| 45 | static void *StaticRun(void *self); |
| 46 | |
| 47 | pthread_t thread_; |
| 48 | bool started_; |
| 49 | bool joined_; |
Brian Silverman | d4c4832 | 2014-09-06 21:17:29 -0400 | [diff] [blame^] | 50 | ::std::atomic_bool should_terminate_; |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 53 | }; |
| 54 | |
| 55 | class FunctionThread : public Thread { |
| 56 | public: |
| 57 | FunctionThread(::std::function<void(FunctionThread *)> function) |
| 58 | : function_(function) {} |
| 59 | |
| 60 | private: |
| 61 | virtual void Run() override { |
| 62 | function_(this); |
| 63 | } |
| 64 | |
| 65 | const ::std::function<void(FunctionThread *)> function_; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // namespace util |
| 69 | } // namespace aos |
| 70 | |
| 71 | #endif // AOS_COMMON_UTIL_THREAD_H_ |