~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/thread/queued_alarm.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2011 Google Inc.
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
 
// Author: morlovich@google.com (Maksim Orlovich)
16
 
 
17
 
#ifndef PAGESPEED_KERNEL_THREAD_QUEUED_ALARM_H_
18
 
#define PAGESPEED_KERNEL_THREAD_QUEUED_ALARM_H_
19
 
 
20
 
#include "pagespeed/kernel/base/basictypes.h"
21
 
#include "pagespeed/kernel/base/function.h"
22
 
#include "pagespeed/kernel/base/scoped_ptr.h"
23
 
#include "pagespeed/kernel/thread/queued_worker_pool.h"
24
 
#include "pagespeed/kernel/thread/scheduler.h"
25
 
 
26
 
namespace net_instaweb {
27
 
 
28
 
class AbstractMutex;
29
 
 
30
 
// A helper for managing alarms that need to both run in a sequence and be
31
 
// cancellable (in the CancelAlarm sense) safely; note that
32
 
// QueuedWorkerPool::Sequence::AddFunction does not provide alarm awareness.
33
 
class QueuedAlarm : public Function {
34
 
 public:
35
 
  // Schedules a function to run at a given time in a given sequence.
36
 
  // (Note that the function's invocation may be delayed by other work
37
 
  //  present in the sequence at time of alarm going off).
38
 
  //
39
 
  // This constructor must be invoked from that sequence as well.
40
 
  //
41
 
  // The object will be destroyed automatically when either the callback
42
 
  // is invoked or the cancellation is complete. You should not free the
43
 
  // sequence until one of these points is reached.
44
 
  QueuedAlarm(Scheduler* scheduler,
45
 
              QueuedWorkerPool::Sequence* sequence,
46
 
              int64 wakeup_time_us,
47
 
              Function* callback);
48
 
 
49
 
  // Cancels the alarm. This method must be run from the sequence given to the
50
 
  // constructor; and should not be called when the callback has already been
51
 
  // invoked. It is suggested that as both invocations of CancelAlarm and
52
 
  // the callback are deallocation points that you defensively clear any
53
 
  // pointers to the QueuedAlarm object when they occur.
54
 
  //
55
 
  // The function's Cancel method will be invoked; but no guarantee is made
56
 
  // as to when or in what thread context. The class does guarantee, however,
57
 
  // that it will not access the sequence_ once CancelAlarm() completes.
58
 
  void CancelAlarm();
59
 
 
60
 
 private:
61
 
  virtual ~QueuedAlarm();
62
 
 
63
 
  // Runs in an arbitrary thread.
64
 
  virtual void Run();
65
 
 
66
 
  // Runs in the sequence case.
67
 
  void SequencePortionOfRun();
68
 
 
69
 
  // This can get invoked if our client freed the sequence upon calling
70
 
  // CancelAlarm, in the case where what normally would be SequencePortionOfRun
71
 
  // is already on the queue.
72
 
  void SequencePortionOfRunCancelled();
73
 
 
74
 
  scoped_ptr<AbstractMutex> mutex_;
75
 
  Scheduler* scheduler_;
76
 
  QueuedWorkerPool::Sequence* sequence_;
77
 
  Function* callback_;
78
 
  Scheduler::Alarm* alarm_;
79
 
 
80
 
  bool canceled_;
81
 
  bool queued_sequence_portion_;
82
 
};
83
 
 
84
 
}  // namespace net_instaweb
85
 
 
86
 
#endif  // PAGESPEED_KERNEL_THREAD_QUEUED_ALARM_H_