modules/up/src/Core/sched/Timer.cc
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- Timer
- Timer
- ITimer
- handle
- restart
- stop
1 //
2 // $Id: Timer.cc,v 1.1.1.1 2000/03/10 16:32:22 engin Exp $
3 //
4 // Timer.cc
5 // Author(s): Ramesh Govindan <govindan@isi.edu>
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include "util/Types.hh"
12 #include "util/Handler.hh"
13 #include "util/List.hh"
14 #include "util/Trail.hh"
15
16 #include "sys/Time.hh"
17 #include "sched/Timer.hh"
18 #include "sched/Dispatcher.hh"
19
20 // File local variables
21 static TraceCode traceTimer("timer");
22
23 // The one-shot timer callback is initiated by the dispatcher.
24 // Typically, we emulate periodic timers by re-creating a new
25 // timer from the handler.
26
27 Timer::Timer(const Handler& h,
/* [<][>][^][v][top][bottom][index][help] */
28 const TimeLong& at)
29 : ListNode(), expireAt(at), handler(h), pending_(true)
30 {
31 dispatcher.timers.insertSorted(this);
32 TRACE(traceTimer,
33 "creating new timer\n");
34 }
35
36 Timer::~Timer()
/* [<][>][^][v][top][bottom][index][help] */
37 {
38 if (pending_) {
39 dispatcher.timers.remove(this);
40 TRACE(traceTimer,
41 "dequeuing and freeing timer\n");
42 }
43 }
44
45 bool
46 Timer::operator<(const Timer& t) const
47 {
48 return (expireAt < t.expireAt);
49 }
50
51 bool
52 Timer::operator==(const Timer& t) const
53 {
54 return (expireAt == t.expireAt);
55 }
56
57 ITimer::ITimer(const Handler& h,
/* [<][>][^][v][top][bottom][index][help] */
58 const TimeShort& ival)
59 : Timer(h, dispatcher.systemClock+ival),
60 ival_(ival) {
61 }
62
63 void
64 ITimer::handle() {
/* [<][>][^][v][top][bottom][index][help] */
65 restart();
66 if (!handler.null())
67 handler.callBack((void*) this);
68 }
69 void
70 ITimer::restart() {
/* [<][>][^][v][top][bottom][index][help] */
71 if (pending_)
72 dispatcher.timers.remove(this);
73 expireAt= dispatcher.systemClock + ival_;
74 dispatcher.timers.insertSorted(this);
75 pending_= true;
76 TRACE(traceTimer,
77 "rescheduled interval timer\n");
78 }
79 void
80 ITimer::stop() {
/* [<][>][^][v][top][bottom][index][help] */
81 if (pending_) {
82 pending_= false;
83 dispatcher.timers.remove(this);
84 }
85 }
86 //
87 // Copyright (c) 1994 by the University of Southern California.
88 // All rights reserved.
89 //
90 // Permission to use, copy, modify, and distribute this software and
91 // its documentation in source and binary forms for lawful
92 // non-commercial purposes and without fee is hereby granted, provided
93 // that the above copyright notice appear in all copies and that both
94 // the copyright notice and this permission notice appear in supporting
95 // documentation, and that any documentation, advertising materials,
96 // and other materials related to such distribution and use acknowledge
97 // that the software was developed by the University of Southern
98 // California and/or Information Sciences Institute.
99 // The name of the University of Southern California may not
100 // be used to endorse or promote products derived from this software
101 // without specific prior written permission.
102 //
103 // THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
104 // ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE. THIS SOFTWARE IS
105 // PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
106 // INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
107 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
108 // NON-INFRINGEMENT.
109 //
110 // IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
111 // SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
112 // TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
113 // THE USE OR PERFORMANCE OF THIS SOFTWARE.
114 //
115 // Questions concerning this software should be directed to
116 // info-ra@isi.edu.
117 //