Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scheduler.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc.
16 
17 #ifndef PAGESPEED_KERNEL_THREAD_SCHEDULER_H_
18 #define PAGESPEED_KERNEL_THREAD_SCHEDULER_H_
19 
20 #include <set>
21 
26 #include "pagespeed/kernel/base/thread_annotations.h"
30 
34 #define SCHEDULER_CANCEL_OUTSTANDING_ALARMS_ON_DESTRUCTION 0
35 
36 namespace net_instaweb {
37 
47 class Scheduler {
48  public:
52  class Alarm;
53 
56  class Sequence;
57 
60  struct CompareAlarms {
61  bool operator()(const Alarm* a, const Alarm* b) const;
62  };
63 
65  virtual ~Scheduler();
66 
67  ThreadSystem::CondvarCapableMutex* mutex() LOCK_RETURNED(mutex_) {
68  return mutex_.get();
69  }
70 
72  void DCheckLocked() EXCLUSIVE_LOCKS_REQUIRED(mutex()) {
73  mutex_->DCheckLocked();
74  }
75 
79 
81  void BlockingTimedWaitMs(int64 timeout_ms) EXCLUSIVE_LOCKS_REQUIRED(mutex()) {
82  BlockingTimedWaitUs(timeout_ms * Timer::kMsUs);
83  }
84  void BlockingTimedWaitUs(int64 timeout_us) EXCLUSIVE_LOCKS_REQUIRED(mutex());
85 
91  void TimedWaitMs(int64 timeout_ms, Function* callback)
92  EXCLUSIVE_LOCKS_REQUIRED(mutex());
93 
99  void Signal() EXCLUSIVE_LOCKS_REQUIRED(mutex());
100 
103 
118  Alarm* AddAlarmAtUs(int64 wakeup_time_us, Function* callback)
119  LOCKS_EXCLUDED(mutex());
120 
123  Alarm* AddAlarmAtUsMutexHeld(int64 wakeup_time_us, Function* callback)
124  EXCLUSIVE_LOCKS_REQUIRED(mutex());
125 
138  bool CancelAlarm(Alarm* alarm) EXCLUSIVE_LOCKS_REQUIRED(mutex());
139 
149  bool ProcessAlarmsOrWaitUs(int64 timeout_us)
150  EXCLUSIVE_LOCKS_REQUIRED(mutex());
151 
154  Timer* timer() { return timer_; }
155 
157  ThreadSystem* thread_system() { return thread_system_; }
158 
162  void Wakeup() { condvar_->Broadcast(); }
163 
168  virtual void UnregisterWorker(QueuedWorkerPool::Sequence* w);
169 
174  int64 RunAlarms(bool* ran_alarms) EXCLUSIVE_LOCKS_REQUIRED(mutex());
175 
178 
179  protected:
185  virtual void AwaitWakeupUntilUs(int64 wakeup_time_us);
186 
187  bool running_waiting_alarms() const { return running_waiting_alarms_; }
188 
189  private:
190  class CondVarTimeout;
191  class CondVarCallbackTimeout;
192  friend class SchedulerTest;
193 
194  typedef std::set<Alarm*, CompareAlarms> AlarmSet;
195 
198  void InsertAlarmAtUsMutexHeld(int64 wakeup_time_us,
199  bool broadcast_on_wakeup_change,
200  Alarm* alarm);
201  void CancelWaiting(Alarm* alarm);
202  bool NoPendingAlarms();
203 
204  ThreadSystem* thread_system_;
205  Timer* timer_;
206  scoped_ptr<ThreadSystem::CondvarCapableMutex> mutex_;
209  scoped_ptr<ThreadSystem::Condvar> condvar_;
210  uint32 index_;
211  AlarmSet outstanding_alarms_;
212  int64 signal_count_;
215  AlarmSet waiting_alarms_;
216  bool running_waiting_alarms_;
217 
219 };
220 
228  public:
229  explicit SchedulerBlockingFunction(Scheduler* scheduler);
230  virtual ~SchedulerBlockingFunction();
231  virtual void Run();
232  virtual void Cancel();
234  bool Block();
235  private:
236  Scheduler* scheduler_;
237  bool success_;
238  bool done_;
239 
240 };
241 
242 }
243 
244 #endif
Definition: scheduler_sequence.h:33
void DCheckLocked() EXCLUSIVE_LOCKS_REQUIRED(mutex())
Optionally check that mutex is locked for debugging purposes.
Definition: scheduler.h:72
virtual void AwaitWakeupUntilUs(int64 wakeup_time_us)
Timer * timer()
Definition: scheduler.h:154
Alarm * AddAlarmAtUsMutexHeld(int64 wakeup_time_us, Function *callback) EXCLUSIVE_LOCKS_REQUIRED(mutex())
Definition: sequence.h:33
Definition: scheduler.h:60
Definition: scheduler.h:227
void BlockingTimedWaitMs(int64 timeout_ms) EXCLUSIVE_LOCKS_REQUIRED(mutex())
Wait at most timeout_ms, or until Signal() is called.
Definition: scheduler.h:81
int64 RunAlarms(bool *ran_alarms) EXCLUSIVE_LOCKS_REQUIRED(mutex())
Alarm * AddAlarmAtUs(int64 wakeup_time_us, Function *callback) LOCKS_EXCLUDED(mutex())
Definition: function.h:47
bool CancelAlarm(Alarm *alarm) EXCLUSIVE_LOCKS_REQUIRED(mutex())
Definition: queued_worker_pool.h:65
void Wakeup()
Definition: scheduler.h:162
void TimedWaitMs(int64 timeout_ms, Function *callback) EXCLUSIVE_LOCKS_REQUIRED(mutex())
Definition: thread_system.h:40
void Signal() EXCLUSIVE_LOCKS_REQUIRED(mutex())
Definition: scheduler.h:47
virtual void RegisterWorker(QueuedWorkerPool::Sequence *w)
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
bool ProcessAlarmsOrWaitUs(int64 timeout_us) EXCLUSIVE_LOCKS_REQUIRED(mutex())
ThreadSystem * thread_system()
Obtain the thread system used by the scheduler.
Definition: scheduler.h:157
bool Block()
Block until called back, returning true for Run and false for Cancel.
Sequence * NewSequence()
Creates a new sequence, controlled by the scheduler.