1
// Copyright 2011 Google Inc.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
15
// Author: morlovich@google.com (Maksim Orlovich)
17
#ifndef PAGESPEED_KERNEL_THREAD_QUEUED_ALARM_H_
18
#define PAGESPEED_KERNEL_THREAD_QUEUED_ALARM_H_
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"
26
namespace net_instaweb {
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 {
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).
39
// This constructor must be invoked from that sequence as well.
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,
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.
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.
61
virtual ~QueuedAlarm();
63
// Runs in an arbitrary thread.
66
// Runs in the sequence case.
67
void SequencePortionOfRun();
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();
74
scoped_ptr<AbstractMutex> mutex_;
75
Scheduler* scheduler_;
76
QueuedWorkerPool::Sequence* sequence_;
78
Scheduler::Alarm* alarm_;
81
bool queued_sequence_portion_;
84
} // namespace net_instaweb
86
#endif // PAGESPEED_KERNEL_THREAD_QUEUED_ALARM_H_