[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 5 | #ifndef CHROME_COMMON_WORKER_THREAD_TICKER_H_ |
6 | #define CHROME_COMMON_WORKER_THREAD_TICKER_H_ | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include <vector> |
9 | |||||
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 10 | #include "base/synchronization/lock.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 11 | #include "base/threading/thread.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | |
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 13 | // This class provides the following functionality: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | // It invokes a set of registered handlers at periodic intervals in |
15 | // the context of an arbitrary worker thread. | ||||
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 16 | // The timer runs on a separate thread, so it will run even if the current |
17 | // thread is hung. Similarly, the callbacks will be called on a separate | ||||
18 | // thread so they won't block the main thread. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | class WorkerThreadTicker { |
20 | public: | ||||
21 | // This callback interface to be implemented by clients of this | ||||
22 | // class | ||||
23 | class Callback { | ||||
24 | public: | ||||
25 | // Gets invoked when the timer period is up | ||||
26 | virtual void OnTick() = 0; | ||||
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 27 | |
28 | protected: | ||||
29 | virtual ~Callback() {} | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | }; |
31 | |||||
32 | // tick_interval is the periodic interval in which to invoke the | ||||
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 33 | // registered handlers (in milliseconds) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | explicit WorkerThreadTicker(int tick_interval); |
35 | |||||
36 | ~WorkerThreadTicker(); | ||||
37 | |||||
38 | // Registers a callback handler interface | ||||
39 | // tick_handler is the handler interface to register. The ownership of this | ||||
40 | // object is not transferred to this class. | ||||
41 | bool RegisterTickHandler(Callback *tick_handler); | ||||
42 | |||||
43 | // Unregisters a callback handler interface | ||||
44 | // tick_handler is the handler interface to unregister | ||||
45 | bool UnregisterTickHandler(Callback *tick_handler); | ||||
46 | |||||
47 | // Starts the ticker. Returns false if the ticker is already running | ||||
48 | // or if the Start fails. | ||||
49 | bool Start(); | ||||
50 | // Stops the ticker and waits for all callbacks. to be done. This method | ||||
51 | // does not provide a way to stop without waiting for the callbacks to be | ||||
52 | // done because this is inherently risky. | ||||
53 | // Returns false is the ticker is not running | ||||
54 | bool Stop(); | ||||
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 55 | bool IsRunning() const { |
56 | return is_running_; | ||||
57 | } | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | |
59 | void set_tick_interval(int tick_interval) { | ||||
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 60 | tick_interval_ = base::TimeDelta::FromMilliseconds(tick_interval); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | } |
62 | |||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | private: |
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 64 | void ScheduleTimerTask(); |
65 | |||||
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 66 | void TimerTask(); |
67 | |||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | // A list type that holds all registered callback interfaces |
69 | typedef std::vector<Callback*> TickHandlerListType; | ||||
70 | |||||
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 71 | // Lock to protect is_running_ and tick_handler_list_ |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 72 | base::Lock lock_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | |
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 74 | base::Thread timer_thread_; |
75 | bool is_running_; | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | // The interval at which the callbacks are to be invoked |
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 78 | base::TimeDelta tick_interval_; |
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 79 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | // A list that holds all registered callback interfaces |
81 | TickHandlerListType tick_handler_list_; | ||||
82 | |||||
[email protected] | d195f60 | 2009-01-14 17:23:19 | [diff] [blame] | 83 | DISALLOW_COPY_AND_ASSIGN(WorkerThreadTicker); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | }; |
85 | |||||
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 86 | #endif // CHROME_COMMON_WORKER_THREAD_TICKER_H_ |