Bug 346849: Add a "Save Image as..." entry to the context menu for <canvas>, Original...
[mozilla-central.git] / xpcom / threads / nsTimerImpl.h
blob5dcba79ec7a034063b1456d80fad179e95910687
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 "nsVoidArray.h"
48 #include "nsIThread.h"
49 #include "nsIObserver.h"
51 #include "nsCOMPtr.h"
53 #include "prlog.h"
55 #if defined(PR_LOGGING)
56 static PRLogModuleInfo *gTimerLog = PR_NewLogModule("nsTimerImpl");
57 #define DEBUG_TIMERS 1
58 #else
59 #undef DEBUG_TIMERS
60 #endif
62 #define NS_TIMER_CLASSNAME "Timer"
63 #define NS_TIMER_CID \
64 { /* 5ff24248-1dd2-11b2-8427-fbab44f29bc8 */ \
65 0x5ff24248, \
66 0x1dd2, \
67 0x11b2, \
68 {0x84, 0x27, 0xfb, 0xab, 0x44, 0xf2, 0x9b, 0xc8} \
71 enum {
72 CALLBACK_TYPE_UNKNOWN = 0,
73 CALLBACK_TYPE_INTERFACE = 1,
74 CALLBACK_TYPE_FUNC = 2,
75 CALLBACK_TYPE_OBSERVER = 3
78 // Two timer deadlines must differ by less than half the PRIntervalTime domain.
79 #define DELAY_INTERVAL_LIMIT PR_BIT(8 * sizeof(PRIntervalTime) - 1)
81 // Maximum possible delay (XXX rework to use ms rather than interval ticks).
82 #define DELAY_INTERVAL_MAX (DELAY_INTERVAL_LIMIT - 1)
84 // Is interval-time t less than u, even if t has wrapped PRIntervalTime?
85 #define TIMER_LESS_THAN(t, u) ((t) - (u) > DELAY_INTERVAL_LIMIT)
87 class nsTimerImpl : public nsITimer
89 public:
91 nsTimerImpl();
93 static NS_HIDDEN_(nsresult) Startup();
94 static NS_HIDDEN_(void) Shutdown();
96 friend class TimerThread;
98 void Fire();
99 void PostTimerEvent();
100 void SetDelayInternal(PRUint32 aDelay);
102 NS_DECL_ISUPPORTS
103 NS_DECL_NSITIMER
105 PRInt32 GetGeneration() { return mGeneration; }
107 private:
108 ~nsTimerImpl();
109 nsresult InitCommon(PRUint32 aType, PRUint32 aDelay);
111 void ReleaseCallback()
113 if (mCallbackType == CALLBACK_TYPE_INTERFACE)
114 NS_RELEASE(mCallback.i);
115 else if (mCallbackType == CALLBACK_TYPE_OBSERVER)
116 NS_RELEASE(mCallback.o);
119 nsCOMPtr<nsIThread> mCallingThread;
121 void * mClosure;
123 union {
124 nsTimerCallbackFunc c;
125 nsITimerCallback * i;
126 nsIObserver * o;
127 } mCallback;
129 // These members are set by Init (called from NS_NewTimer) and never reset.
130 PRUint8 mCallbackType;
132 // These members are set by the initiating thread, when the timer's type is
133 // changed and during the period where it fires on that thread.
134 PRUint8 mType;
135 PRPackedBool mFiring;
138 // Use a PRBool (int) here to isolate loads and stores of these two members
139 // done on various threads under the protection of TimerThread::mLock, from
140 // loads and stores done on the initiating/type-changing/timer-firing thread
141 // to the above PRUint8/PRPackedBool members.
142 PRBool mArmed;
143 PRBool mCanceled;
145 // The generation number of this timer, re-generated each time the timer is
146 // initialized so one-shot timers can be canceled and re-initialized by the
147 // arming thread without any bad race conditions.
148 PRInt32 mGeneration;
150 PRUint32 mDelay;
151 PRIntervalTime mTimeout;
153 #ifdef DEBUG_TIMERS
154 PRIntervalTime mStart, mStart2;
155 static double sDeltaSum;
156 static double sDeltaSumSquared;
157 static double sDeltaNum;
158 #endif
162 #endif /* nsTimerImpl_h___ */