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