worker_thread.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_
  15. #define CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_
  16. #include <memory>
  17. #include "base/macros.h"
  18. #include "util/synchronization/semaphore.h"
  19. namespace crashpad {
  20. namespace internal {
  21. class WorkerThreadImpl;
  22. } // namespace internal
  23. //! \brief A WorkerThread executes its Delegate's DoWork method repeatedly on a
  24. //! dedicated thread at a set time interval.
  25. class WorkerThread {
  26. public:
  27. //! \brief An interface for doing work on a WorkerThread.
  28. class Delegate {
  29. public:
  30. //! \brief The work function executed by the WorkerThread every work
  31. //! interval.
  32. virtual void DoWork(const WorkerThread* thread) = 0;
  33. protected:
  34. virtual ~Delegate() {}
  35. };
  36. //! \brief A delay or interval argument that causes an indefinite wait.
  37. static constexpr double kIndefiniteWait = Semaphore::kIndefiniteWait;
  38. //! \brief Creates a new WorkerThread that is not yet running.
  39. //!
  40. //! \param[in] work_interval The time interval in seconds at which the \a
  41. //! delegate runs. The interval counts from the completion of
  42. //! Delegate::DoWork() to the next invocation. This can be
  43. //! #kIndefiniteWait if work should only be done when DoWorkNow() is
  44. //! called.
  45. //! \param[in] delegate The work delegate to invoke every interval.
  46. WorkerThread(double work_interval, Delegate* delegate);
  47. ~WorkerThread();
  48. //! \brief Starts the worker thread.
  49. //!
  50. //! This may not be called if the thread is_running().
  51. //!
  52. //! \param[in] initial_work_delay The amount of time in seconds to wait
  53. //! before invoking the \a delegate for the first time. Pass `0` for
  54. //! no delay. This can be #kIndefiniteWait if work should not be done
  55. //! until DoWorkNow() is called.
  56. void Start(double initial_work_delay);
  57. //! \brief Stops the worker thread from running.
  58. //!
  59. //! This may only be called if the thread is_running().
  60. //!
  61. //! If the work function is currently executing, this will not interrupt it.
  62. //! This method stops any future work from occurring. This method is safe
  63. //! to call from any thread with the exception of the worker thread itself,
  64. //! as this joins the thread.
  65. void Stop();
  66. //! \brief Interrupts a \a work_interval to execute the work function
  67. //! immediately. This invokes Delegate::DoWork() on the thread, without
  68. //! waiting for the current \a work_interval to expire. After the
  69. //! delegate is invoked, the WorkerThread will start waiting for a new
  70. //! \a work_interval.
  71. void DoWorkNow();
  72. //! \return `true` if the thread is running, `false` if it is not.
  73. bool is_running() const { return running_; }
  74. private:
  75. friend class internal::WorkerThreadImpl;
  76. double work_interval_;
  77. Delegate* delegate_; // weak
  78. std::unique_ptr<internal::WorkerThreadImpl> impl_;
  79. bool running_;
  80. DISALLOW_COPY_AND_ASSIGN(WorkerThread);
  81. };
  82. } // namespace crashpad
  83. #endif // CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_