Bug 448909 - Need more controls WHATWG Video tag. r=mconnor, r=bz
[wine-gecko.git] / xpcom / threads / nsTimerImpl.h
blob602b9cc2f8b8d635cd7a65e8eda8f82dd1c9edf2
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
15 * License.
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.
24 * Contributor(s):
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 */
46 #include "nsITimer.h"
47 #include "nsIEventTarget.h"
48 #include "nsIObserver.h"
50 #include "nsCOMPtr.h"
52 #include "prlog.h"
54 #if defined(PR_LOGGING)
55 static PRLogModuleInfo *gTimerLog = PR_NewLogModule("nsTimerImpl");
56 #define DEBUG_TIMERS 1
57 #else
58 #undef DEBUG_TIMERS
59 #endif
61 #define NS_TIMER_CLASSNAME "Timer"
62 #define NS_TIMER_CID \
63 { /* 5ff24248-1dd2-11b2-8427-fbab44f29bc8 */ \
64 0x5ff24248, \
65 0x1dd2, \
66 0x11b2, \
67 {0x84, 0x27, 0xfb, 0xab, 0x44, 0xf2, 0x9b, 0xc8} \
70 enum {
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
88 public:
90 nsTimerImpl();
92 static NS_HIDDEN_(nsresult) Startup();
93 static NS_HIDDEN_(void) Shutdown();
95 friend class TimerThread;
97 void Fire();
98 nsresult PostTimerEvent();
99 void SetDelayInternal(PRUint32 aDelay);
101 NS_DECL_ISUPPORTS
102 NS_DECL_NSITIMER
104 PRInt32 GetGeneration() { return mGeneration; }
106 private:
107 ~nsTimerImpl();
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;
126 void * mClosure;
128 union CallbackUnion {
129 nsTimerCallbackFunc c;
130 nsITimerCallback * i;
131 nsIObserver * o;
132 } mCallback;
134 // Some callers expect to be able to access the callback while the
135 // timer is firing.
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.
143 PRUint8 mType;
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.
151 PRBool mArmed;
152 PRBool mCanceled;
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.
157 PRInt32 mGeneration;
159 PRUint32 mDelay;
160 PRIntervalTime mTimeout;
162 #ifdef DEBUG_TIMERS
163 PRIntervalTime mStart, mStart2;
164 static double sDeltaSum;
165 static double sDeltaSumSquared;
166 static double sDeltaNum;
167 #endif
171 #endif /* nsTimerImpl_h___ */