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
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
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/
11 : *
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.
16 : *
17 : * The Original Code is mozilla.org code.
18 : *
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.
23 : *
24 : * Contributor(s):
25 : * Stuart Parmenter <pavlov@netscape.com>
26 : *
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.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef nsTimerImpl_h___
42 : #define nsTimerImpl_h___
43 :
44 : //#define FORCE_PR_LOG /* Allow logging in the release build */
45 :
46 : #include "nsITimer.h"
47 : #include "nsIEventTarget.h"
48 : #include "nsIObserver.h"
49 :
50 : #include "nsCOMPtr.h"
51 :
52 : #include "prlog.h"
53 : #include "mozilla/TimeStamp.h"
54 :
55 : #if defined(PR_LOGGING)
56 4392 : static PRLogModuleInfo *gTimerLog = PR_NewLogModule("nsTimerImpl");
57 : #define DEBUG_TIMERS 1
58 : #else
59 : #undef DEBUG_TIMERS
60 : #endif
61 :
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} \
69 : }
70 :
71 : enum {
72 : CALLBACK_TYPE_UNKNOWN = 0,
73 : CALLBACK_TYPE_INTERFACE = 1,
74 : CALLBACK_TYPE_FUNC = 2,
75 : CALLBACK_TYPE_OBSERVER = 3
76 : };
77 :
78 : class nsTimerImpl : public nsITimer
79 : {
80 : public:
81 : typedef mozilla::TimeStamp TimeStamp;
82 :
83 : nsTimerImpl();
84 :
85 : static NS_HIDDEN_(nsresult) Startup();
86 : static NS_HIDDEN_(void) Shutdown();
87 :
88 : friend class TimerThread;
89 :
90 : void Fire();
91 : nsresult PostTimerEvent();
92 : void SetDelayInternal(PRUint32 aDelay);
93 :
94 : NS_DECL_ISUPPORTS
95 : NS_DECL_NSITIMER
96 :
97 4298 : PRInt32 GetGeneration() { return mGeneration; }
98 :
99 : private:
100 : ~nsTimerImpl();
101 : nsresult InitCommon(PRUint32 aType, PRUint32 aDelay);
102 :
103 58475 : void ReleaseCallback()
104 : {
105 : // if we're the last owner of the callback object, make
106 : // sure that we don't recurse into ReleaseCallback in case
107 : // the callback's destructor calls Cancel() or similar.
108 58475 : PRUint8 cbType = mCallbackType;
109 58475 : mCallbackType = CALLBACK_TYPE_UNKNOWN;
110 :
111 58475 : if (cbType == CALLBACK_TYPE_INTERFACE)
112 9817 : NS_RELEASE(mCallback.i);
113 48658 : else if (cbType == CALLBACK_TYPE_OBSERVER)
114 2825 : NS_RELEASE(mCallback.o);
115 58475 : }
116 :
117 4243 : bool IsRepeating() const {
118 : PR_STATIC_ASSERT(TYPE_ONE_SHOT < TYPE_REPEATING_SLACK);
119 : PR_STATIC_ASSERT(TYPE_REPEATING_SLACK < TYPE_REPEATING_PRECISE);
120 : PR_STATIC_ASSERT(TYPE_REPEATING_PRECISE < TYPE_REPEATING_PRECISE_CAN_SKIP);
121 4243 : return mType >= TYPE_REPEATING_SLACK;
122 : }
123 :
124 8541 : bool IsRepeatingPrecisely() const {
125 8541 : return mType >= TYPE_REPEATING_PRECISE;
126 : }
127 :
128 : nsCOMPtr<nsIEventTarget> mEventTarget;
129 :
130 : void * mClosure;
131 :
132 : union CallbackUnion {
133 : nsTimerCallbackFunc c;
134 : nsITimerCallback * i;
135 : nsIObserver * o;
136 : } mCallback;
137 :
138 : // Some callers expect to be able to access the callback while the
139 : // timer is firing.
140 : nsCOMPtr<nsITimerCallback> mTimerCallbackWhileFiring;
141 :
142 : // These members are set by Init (called from NS_NewTimer) and never reset.
143 : PRUint8 mCallbackType;
144 :
145 : // These members are set by the initiating thread, when the timer's type is
146 : // changed and during the period where it fires on that thread.
147 : PRUint8 mType;
148 : bool mFiring;
149 :
150 :
151 : // Use a bool (int) here to isolate loads and stores of these two members
152 : // done on various threads under the protection of TimerThread::mLock, from
153 : // loads and stores done on the initiating/type-changing/timer-firing thread
154 : // to the above PRUint8/bool members.
155 : bool mArmed;
156 : bool mCanceled;
157 :
158 : // The generation number of this timer, re-generated each time the timer is
159 : // initialized so one-shot timers can be canceled and re-initialized by the
160 : // arming thread without any bad race conditions.
161 : PRInt32 mGeneration;
162 :
163 : PRUint32 mDelay;
164 : TimeStamp mTimeout;
165 :
166 : #ifdef DEBUG_TIMERS
167 : TimeStamp mStart, mStart2;
168 : static double sDeltaSum;
169 : static double sDeltaSumSquared;
170 : static double sDeltaNum;
171 : #endif
172 :
173 : };
174 :
175 : #endif /* nsTimerImpl_h___ */
|