OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #ifndef CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_ |
| 16 #define CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_ |
| 17 |
| 18 #include "base/basictypes.h" |
| 19 #include "base/memory/scoped_ptr.h" |
| 20 |
| 21 namespace crashpad { |
| 22 |
| 23 namespace internal { |
| 24 class WorkerThreadImpl; |
| 25 } // namespace internal |
| 26 |
| 27 //! \brief A WorkerThread executes its Delegate's DoWork method repeatedly on a |
| 28 //! dedicated thread at a set time interval. |
| 29 class WorkerThread { |
| 30 public: |
| 31 //! \brief An interface for doing work on a WorkerThread. |
| 32 class Delegate { |
| 33 public: |
| 34 //! \brief The work function executed by the WorkerThread every work |
| 35 //! interval. |
| 36 virtual void DoWork(const WorkerThread* thread) = 0; |
| 37 |
| 38 protected: |
| 39 virtual ~Delegate() {} |
| 40 }; |
| 41 |
| 42 //! \brief Creates a new WorkerThread that is not yet running. |
| 43 //! |
| 44 //! \param[in] work_interval The time interval in seconds at which the \a |
| 45 //! delegate runs. The interval counts from the completion of |
| 46 //! Delegate::DoWork() to the next invocation. |
| 47 //! \param[in] delegate The work delegate to invoke every interval. |
| 48 WorkerThread(double work_interval, Delegate* delegate); |
| 49 ~WorkerThread(); |
| 50 |
| 51 //! \brief Starts the worker thread. |
| 52 //! |
| 53 //! This may not be called if the thread is_running(). |
| 54 //! |
| 55 //! \param[in] initial_work_delay The amount of time in seconds to wait |
| 56 //! before invoking the \a delegate for the first time. Pass `0` for |
| 57 //! no delay. |
| 58 void Start(double initial_work_delay); |
| 59 |
| 60 //! \brief Stops the worker thread from running. |
| 61 //! |
| 62 //! This may only be called if the thread is_running(). |
| 63 //! |
| 64 //! If the work function is currently executing, this will not interrupt it. |
| 65 //! This method stops any future work from occurring. This method is safe |
| 66 //! to call from any thread with the exception of the worker thread itself, |
| 67 //! as this joins the thread. |
| 68 void Stop(); |
| 69 |
| 70 //! \brief Interrupts a \a work_interval to execute the work function |
| 71 //! immediately. This invokes Delegate::DoWork() on the thread, without |
| 72 //! waiting for the current \a work_interval to expire. After the |
| 73 //! delegate is invoked, the WorkerThread will start waiting for a new |
| 74 //! \a work_interval. |
| 75 void DoWorkNow(); |
| 76 |
| 77 //! \return `true` if the thread is running, `false` if it is not. |
| 78 bool is_running() const { return running_; } |
| 79 |
| 80 private: |
| 81 friend class internal::WorkerThreadImpl; |
| 82 |
| 83 double work_interval_; |
| 84 Delegate* delegate_; // weak |
| 85 scoped_ptr<internal::WorkerThreadImpl> impl_; |
| 86 bool running_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(WorkerThread); |
| 89 }; |
| 90 |
| 91 } // namespace crashpad |
| 92 |
| 93 #endif // CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_ |
OLD | NEW |