]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/tqueue.h
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // tqueue.h -- timer queues
25 #include <Security/utilities.h>
26 #include <Security/cssmalloc.h>
27 #include <Security/debugging.h>
29 #ifdef _CPP_CDSA_UTILITIES_TQUEUE
38 // A TimerQueue is a container of elements that have relative "timer" positions.
39 // TimerQueues are concerned with shuffling these elements around as their "times"
40 // change, and with processing elements that fall off the front of the queue as
42 // We put "time" into quotes because nothing here really cares what kind of time
43 // you are playing with. It could be seconds, points scored, etc. The only requirement
44 // is that "time" doesn't ever flow backwards...
49 ScheduleQueue() { first
.fwd
= first
.back
= &first
; }
50 virtual ~ScheduleQueue() { }
54 friend class ScheduleQueue
;
56 Event() : mScheduled(false) { }
57 ~Event() { if (scheduled()) unschedule(); }
61 Time
when() const { return fireTime
; }
62 bool scheduled() const { return mScheduled
; }
65 Time fireTime
; // when will it happen?
66 bool mScheduled
; // are we scheduled?
67 Event
*back
, *fwd
; // doubly-linked interior list
69 void putBefore(Event
*ev
)
70 { back
= ev
->back
; fwd
= ev
; ev
->back
= back
->fwd
= this; mScheduled
= true; }
74 void schedule(Event
*event
, Time when
);
75 void unschedule(Event
*event
)
76 { event
->unschedule(); }
78 bool empty() const { return first
.fwd
== &first
; }
79 Time
next() const { assert(!empty()); return first
.fwd
->fireTime
; }
84 Event first
; // root of active timers list
88 void ScheduleQueue
<Time
>::Event::unschedule()
91 back
->fwd
= fwd
; fwd
->back
= back
;
93 debug("schedq", "event %p unscheduled", this);
97 inline void ScheduleQueue
<Time
>::schedule(Event
*event
, Time when
)
99 Event
*ev
= first
.fwd
;
100 if (event
->scheduled()) {
101 if (when
== event
->fireTime
) // no change
103 else if (when
> event
->fireTime
) // forward move
107 event
->fireTime
= when
;
108 // newly schedule the event
109 for (; ev
!= &first
; ev
= ev
->fwd
)
110 if (ev
->fireTime
> when
) {
111 event
->putBefore(ev
);
114 // hit the end-of-queue; put at end
115 event
->putBefore(&first
);
116 debug("schedq", "event %p set for %.3f", event
, double(when
));
119 template <class Time
>
120 inline ScheduleQueue
<Time
>::Event
*ScheduleQueue
<Time
>::pop(Time now
)
123 Event
*top
= first
.fwd
;
124 if (top
->fireTime
<= now
) {
126 debug("schedq", "event %p delivered at %.3f", top
, double(now
));
133 } // end namespace Security
135 #ifdef _CPP_CDSA_UTILITIES_TQUEUE