John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 1 | #ifndef AOS_UTIL_THREAD_H_ |
| 2 | #define AOS_UTIL_THREAD_H_ |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 9 | #include "aos/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. |
Brian Silverman | 325c597 | 2014-09-04 16:12:09 -0400 | [diff] [blame] | 26 | // This or TryJoin() (returning true) must be called exactly once for every |
| 27 | // instance. |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 28 | void Join(); |
| 29 | |
Brian Silverman | 325c597 | 2014-09-04 16:12:09 -0400 | [diff] [blame] | 30 | // If the code has already finished, returns true. Does not block waiting if |
| 31 | // it isn't. |
| 32 | // Join() must not be called on this instance if this returns true. |
| 33 | // This must return true or Join() must be called exactly once for every |
| 34 | // instance. |
| 35 | bool TryJoin(); |
| 36 | |
| 37 | // Asks the code to stop (in preparation for a Join()). |
| 38 | void RequestStop(); |
| 39 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 40 | // Waits until the code has stopped. Does not ask it to do so. |
| 41 | void WaitUntilDone(); |
| 42 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 43 | protected: |
| 44 | // Subclasses need to call this periodically if they are going to loop to |
| 45 | // check whether they have been asked to stop. |
| 46 | bool should_continue() { |
Brian Silverman | d4c4832 | 2014-09-06 21:17:29 -0400 | [diff] [blame] | 47 | return !should_terminate_.load(); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | private: |
| 51 | // Where subclasses actually do something. |
| 52 | // |
| 53 | // They should not block for long periods of time without checking |
| 54 | // should_continue(). |
| 55 | virtual void Run() = 0; |
| 56 | |
| 57 | static void *StaticRun(void *self); |
| 58 | |
| 59 | pthread_t thread_; |
| 60 | bool started_; |
| 61 | bool joined_; |
Brian Silverman | d4c4832 | 2014-09-06 21:17:29 -0400 | [diff] [blame] | 62 | ::std::atomic_bool should_terminate_; |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 65 | }; |
| 66 | |
| 67 | class FunctionThread : public Thread { |
| 68 | public: |
| 69 | FunctionThread(::std::function<void(FunctionThread *)> function) |
| 70 | : function_(function) {} |
| 71 | |
Brian Silverman | 8cfd92c | 2014-09-04 16:12:43 -0400 | [diff] [blame] | 72 | // Runs function in a new thread and waits for it to return. |
| 73 | static void RunInOtherThread(::std::function<void()> function) { |
| 74 | FunctionThread t([&function](FunctionThread *) { function(); }); |
| 75 | t.Start(); |
| 76 | t.Join(); |
| 77 | } |
| 78 | |
Brian Silverman | 653491d | 2014-05-13 16:53:29 -0700 | [diff] [blame] | 79 | private: |
| 80 | virtual void Run() override { |
| 81 | function_(this); |
| 82 | } |
| 83 | |
| 84 | const ::std::function<void(FunctionThread *)> function_; |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | } // namespace util |
| 88 | } // namespace aos |
| 89 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 90 | #endif // AOS_UTIL_THREAD_H_ |