Chromium Code Reviews| Index: util/thread/worker_thread.h |
| diff --git a/util/thread/worker_thread.h b/util/thread/worker_thread.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..77d5f87cbef5d68b61270fb5d65c045bc14f57de |
| --- /dev/null |
| +++ b/util/thread/worker_thread.h |
| @@ -0,0 +1,89 @@ |
| +// Copyright 2015 The Crashpad Authors. All rights reserved. |
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +#ifndef CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_ |
| +#define CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_ |
| + |
| +#include "base/basictypes.h" |
| +#include "base/memory/scoped_ptr.h" |
| + |
| +namespace crashpad { |
| + |
| +namespace internal { |
| +class WorkerThreadImpl; |
| +} |
|
Mark Mentovai
2015/12/15 17:56:58
// namespace internal
Robert Sesek
2015/12/23 19:22:32
Done.
|
| + |
| +//! \brief A WorkerThread executes its Delegate's DoWork method repeatedly on a |
| +//! dedicated thread at a set time interval. |
| +class WorkerThread { |
| + public: |
| + //! \brief An interface for doing work on a WorkerThread. |
| + class Delegate { |
| + public: |
| + //! \brief The work function executed by the WorkerThread every work |
| + //! interval. |
| + virtual void DoWork(const WorkerThread* thread) = 0; |
| + |
| + protected: |
| + virtual ~Delegate() {} |
| + }; |
| + |
| + //! \brief Creates a new WorkerThread that is not yet running. |
| + //! |
| + //! \param[in] work_interval The time interval in seconds at which the \a |
|
Mark Mentovai
2015/12/15 17:56:59
Counting from one call to DoWork() to the next cal
Robert Sesek
2015/12/23 19:22:32
Done.
|
| + //! delegate runs. |
| + //! \param[in] delegate The work delegate to invoke every the interval. |
|
Mark Mentovai
2015/12/15 17:56:59
every the interval
Robert Sesek
2015/12/23 19:22:32
Done.
|
| + WorkerThread(double work_interval, Delegate* delegate); |
| + ~WorkerThread(); |
| + |
| + //! \brief Starts the worker thread. |
| + //! |
| + //! This may not be called if the thread is_running(). |
| + //! |
| + //! \param[in] initial_work_delay The amount of time in seconds to wait |
| + //! before invoking the \a delegate for the first time. Pass `0` for |
| + //! no delay. |
| + void Start(double initial_work_delay); |
|
Mark Mentovai
2015/12/15 17:56:59
Is there a reason that the initial delay and the s
Robert Sesek
2015/12/23 19:22:32
Yes, I think this interface is friendlier. Current
Mark Mentovai
2016/01/04 16:01:31
Robert Sesek wrote:
Robert Sesek
2016/01/04 22:09:45
I think this as-is is less ambiguous than a two-do
|
| + |
| + //! \brief Stops the worker thread from running. |
| + //! |
| + //! This may only be called if the thread is_running(). |
| + //! |
| + //! If the work function is currently executing, this will not interrupt it. |
| + //! This method stops any future work from occurring. This method is safe |
| + //! to call from any thread with the exception of the worker thread itself, |
| + //! as this joins the thread. |
| + void Stop(); |
| + |
| + //! \brief Interrupts a \a work_interval to execute the work function |
| + //! immediately. |
|
Mark Mentovai
2015/12/15 17:56:58
Does the next work after “now” happen after a new
Robert Sesek
2015/12/23 19:22:32
Done.
|
| + void DoWorkNow(); |
| + |
| + //! \return `true` if the thread is running, `false` if it is not. |
| + bool is_running() const { return running_; } |
| + |
| + private: |
| + friend class internal::WorkerThreadImpl; |
| + |
| + double work_interval_; |
| + Delegate* delegate_; // weak |
| + scoped_ptr<internal::WorkerThreadImpl> impl_; |
| + bool running_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WorkerThread); |
| +}; |
| + |
| +} // namespace crashpad |
| + |
| +#endif // CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_ |