Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: util/thread/worker_thread.h

Issue 1526563003: Create WorkerThread, an abstraction to perform some work on an interval. (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
Mark Mentovai 2015/12/15 17:56:58 // namespace internal
Robert Sesek 2015/12/23 19:22:32 Done.
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
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.
45 //! delegate runs.
46 //! \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.
47 WorkerThread(double work_interval, Delegate* delegate);
48 ~WorkerThread();
49
50 //! \brief Starts the worker thread.
51 //!
52 //! This may not be called if the thread is_running().
53 //!
54 //! \param[in] initial_work_delay The amount of time in seconds to wait
55 //! before invoking the \a delegate for the first time. Pass `0` for
56 //! no delay.
57 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
58
59 //! \brief Stops the worker thread from running.
60 //!
61 //! This may only be called if the thread is_running().
62 //!
63 //! If the work function is currently executing, this will not interrupt it.
64 //! This method stops any future work from occurring. This method is safe
65 //! to call from any thread with the exception of the worker thread itself,
66 //! as this joins the thread.
67 void Stop();
68
69 //! \brief Interrupts a \a work_interval to execute the work function
70 //! 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.
71 void DoWorkNow();
72
73 //! \return `true` if the thread is running, `false` if it is not.
74 bool is_running() const { return running_; }
75
76 private:
77 friend class internal::WorkerThreadImpl;
78
79 double work_interval_;
80 Delegate* delegate_; // weak
81 scoped_ptr<internal::WorkerThreadImpl> impl_;
82 bool running_;
83
84 DISALLOW_COPY_AND_ASSIGN(WorkerThread);
85 };
86
87 } // namespace crashpad
88
89 #endif // CRASHPAD_UTIL_THREAD_WORKER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698