1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is mozilla.org code.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2001
22 * the Initial Developer. All Rights Reserved.
25 * Stuart Parmenter <pavlov@netscape.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef nsTimerImpl_h___
42 #define nsTimerImpl_h___
44 //#define FORCE_PR_LOG /* Allow logging in the release build */
47 #include "nsIEventTarget.h"
48 #include "nsIObserver.h"
54 #if defined(PR_LOGGING)
55 static PRLogModuleInfo
*gTimerLog
= PR_NewLogModule("nsTimerImpl");
56 #define DEBUG_TIMERS 1
61 #define NS_TIMER_CLASSNAME "Timer"
62 #define NS_TIMER_CID \
63 { /* 5ff24248-1dd2-11b2-8427-fbab44f29bc8 */ \
67 {0x84, 0x27, 0xfb, 0xab, 0x44, 0xf2, 0x9b, 0xc8} \
71 CALLBACK_TYPE_UNKNOWN
= 0,
72 CALLBACK_TYPE_INTERFACE
= 1,
73 CALLBACK_TYPE_FUNC
= 2,
74 CALLBACK_TYPE_OBSERVER
= 3
77 // Two timer deadlines must differ by less than half the PRIntervalTime domain.
78 #define DELAY_INTERVAL_LIMIT PR_BIT(8 * sizeof(PRIntervalTime) - 1)
80 // Maximum possible delay (XXX rework to use ms rather than interval ticks).
81 #define DELAY_INTERVAL_MAX (DELAY_INTERVAL_LIMIT - 1)
83 // Is interval-time t less than u, even if t has wrapped PRIntervalTime?
84 #define TIMER_LESS_THAN(t, u) ((t) - (u) > DELAY_INTERVAL_LIMIT)
86 class nsTimerImpl
: public nsITimer
92 static NS_HIDDEN_(nsresult
) Startup();
93 static NS_HIDDEN_(void) Shutdown();
95 friend class TimerThread
;
98 nsresult
PostTimerEvent();
99 void SetDelayInternal(PRUint32 aDelay
);
104 PRInt32
GetGeneration() { return mGeneration
; }
108 nsresult
InitCommon(PRUint32 aType
, PRUint32 aDelay
);
110 void ReleaseCallback()
112 // if we're the last owner of the callback object, make
113 // sure that we don't recurse into ReleaseCallback in case
114 // the callback's destructor calls Cancel() or similar.
115 PRUint8 cbType
= mCallbackType
;
116 mCallbackType
= CALLBACK_TYPE_UNKNOWN
;
118 if (cbType
== CALLBACK_TYPE_INTERFACE
)
119 NS_RELEASE(mCallback
.i
);
120 else if (cbType
== CALLBACK_TYPE_OBSERVER
)
121 NS_RELEASE(mCallback
.o
);
124 nsCOMPtr
<nsIEventTarget
> mEventTarget
;
128 union CallbackUnion
{
129 nsTimerCallbackFunc c
;
130 nsITimerCallback
* i
;
134 // Some callers expect to be able to access the callback while the
136 nsCOMPtr
<nsITimerCallback
> mTimerCallbackWhileFiring
;
138 // These members are set by Init (called from NS_NewTimer) and never reset.
139 PRUint8 mCallbackType
;
141 // These members are set by the initiating thread, when the timer's type is
142 // changed and during the period where it fires on that thread.
144 PRPackedBool mFiring
;
147 // Use a PRBool (int) here to isolate loads and stores of these two members
148 // done on various threads under the protection of TimerThread::mLock, from
149 // loads and stores done on the initiating/type-changing/timer-firing thread
150 // to the above PRUint8/PRPackedBool members.
154 // The generation number of this timer, re-generated each time the timer is
155 // initialized so one-shot timers can be canceled and re-initialized by the
156 // arming thread without any bad race conditions.
160 PRIntervalTime mTimeout
;
163 PRIntervalTime mStart
, mStart2
;
164 static double sDeltaSum
;
165 static double sDeltaSumSquared
;
166 static double sDeltaNum
;
171 #endif /* nsTimerImpl_h___ */