Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "primpl.h"
7 :
8 : /**********************************************************************/
9 : /******************************* PRALARM ******************************/
10 : /**********************************************************************/
11 :
12 : #include "obsolete/pralarm.h"
13 :
14 : struct PRAlarmID { /* typedef'd in pralarm.h */
15 : PRCList list; /* circular list linkage */
16 : PRAlarm *alarm; /* back pointer to owning alarm */
17 : PRPeriodicAlarmFn function; /* function to call for notify */
18 : void *clientData; /* opaque client context */
19 : PRIntervalTime period; /* the client defined period */
20 : PRUint32 rate; /* rate of notification */
21 :
22 : PRUint32 accumulator; /* keeps track of # notifies */
23 : PRIntervalTime epoch; /* when timer was started */
24 : PRIntervalTime nextNotify; /* when we'll next do our thing */
25 : PRIntervalTime lastNotify; /* when we last did our thing */
26 : };
27 :
28 : typedef enum {alarm_active, alarm_inactive} _AlarmState;
29 :
30 : struct PRAlarm { /* typedef'd in pralarm.h */
31 : PRCList timers; /* base of alarm ids list */
32 : PRLock *lock; /* lock used to protect data */
33 : PRCondVar *cond; /* condition that used to wait */
34 : PRThread *notifier; /* thread to deliver notifies */
35 : PRAlarmID *current; /* current alarm being served */
36 : _AlarmState state; /* used to delete the alarm */
37 : };
38 :
39 0 : static PRAlarmID *pr_getNextAlarm(PRAlarm *alarm, PRAlarmID *id)
40 : {
41 : /*
42 : * Puts 'id' back into the sorted list iff it's not NULL.
43 : * Removes the first element from the list and returns it (or NULL).
44 : * List is "assumed" to be short.
45 : *
46 : * NB: Caller is providing locking
47 : */
48 : PRCList *timer;
49 0 : PRAlarmID *result = id;
50 0 : PRIntervalTime now = PR_IntervalNow();
51 :
52 0 : if (!PR_CLIST_IS_EMPTY(&alarm->timers))
53 : {
54 0 : if (id != NULL) /* have to put this id back in */
55 : {
56 0 : PRIntervalTime idDelta = now - id->nextNotify;
57 0 : timer = alarm->timers.next;
58 : do
59 : {
60 0 : result = (PRAlarmID*)timer;
61 0 : if ((PRIntervalTime)(now - result->nextNotify) > idDelta)
62 : {
63 0 : PR_INSERT_BEFORE(&id->list, &alarm->timers);
64 0 : break;
65 : }
66 0 : timer = timer->next;
67 0 : } while (timer != &alarm->timers);
68 : }
69 0 : result = (PRAlarmID*)(timer = PR_LIST_HEAD(&alarm->timers));
70 0 : PR_REMOVE_LINK(timer); /* remove it from the list */
71 : }
72 :
73 0 : return result;
74 : } /* pr_getNextAlarm */
75 :
76 0 : static PRIntervalTime pr_PredictNextNotifyTime(PRAlarmID *id)
77 : {
78 : PRIntervalTime delta;
79 0 : PRFloat64 baseRate = (PRFloat64)id->period / (PRFloat64)id->rate;
80 0 : PRFloat64 offsetFromEpoch = (PRFloat64)id->accumulator * baseRate;
81 :
82 0 : id->accumulator += 1; /* every call advances to next period */
83 0 : id->lastNotify = id->nextNotify; /* just keeping track of things */
84 0 : id->nextNotify = (PRIntervalTime)(offsetFromEpoch + 0.5);
85 :
86 0 : delta = id->nextNotify - id->lastNotify;
87 0 : return delta;
88 : } /* pr_PredictNextNotifyTime */
89 :
90 0 : static void PR_CALLBACK pr_alarmNotifier(void *arg)
91 : {
92 : /*
93 : * This is the root of the notifier thread. There is one such thread
94 : * for each PRAlarm. It may service an arbitrary (though assumed to be
95 : * small) number of alarms using the same thread and structure. It
96 : * continues to run until the alarm is destroyed.
97 : */
98 0 : PRAlarmID *id = NULL;
99 0 : PRAlarm *alarm = (PRAlarm*)arg;
100 0 : enum {notify, abort, scan} why = scan;
101 :
102 0 : while (why != abort)
103 : {
104 : PRIntervalTime pause;
105 :
106 0 : PR_Lock(alarm->lock);
107 0 : while (why == scan)
108 : {
109 0 : alarm->current = NULL; /* reset current id */
110 0 : if (alarm->state == alarm_inactive) why = abort; /* we're toast */
111 0 : else if (why == scan) /* the dominant case */
112 : {
113 0 : id = pr_getNextAlarm(alarm, id); /* even if it's the same */
114 0 : if (id == NULL) /* there are no alarms set */
115 0 : (void)PR_WaitCondVar(alarm->cond, PR_INTERVAL_NO_TIMEOUT);
116 : else
117 : {
118 0 : pause = id->nextNotify - (PR_IntervalNow() - id->epoch);
119 0 : if ((PRInt32)pause <= 0) /* is this one's time up? */
120 : {
121 0 : why = notify; /* set up to do our thing */
122 0 : alarm->current = id; /* id we're about to schedule */
123 : }
124 : else
125 0 : (void)PR_WaitCondVar(alarm->cond, pause); /* dally */
126 : }
127 : }
128 : }
129 0 : PR_Unlock(alarm->lock);
130 :
131 0 : if (why == notify)
132 : {
133 0 : (void)pr_PredictNextNotifyTime(id);
134 0 : if (!id->function(id, id->clientData, ~pause))
135 : {
136 : /*
137 : * Notified function decided not to continue. Free
138 : * the alarm id to make sure it doesn't get back on
139 : * the list.
140 : */
141 0 : PR_DELETE(id); /* free notifier object */
142 0 : id = NULL; /* so it doesn't get back into the list */
143 : }
144 0 : why = scan; /* so we can cycle through the loop again */
145 : }
146 : }
147 :
148 0 : } /* pr_alarm_notifier */
149 :
150 0 : PR_IMPLEMENT(PRAlarm*) PR_CreateAlarm(void)
151 : {
152 0 : PRAlarm *alarm = PR_NEWZAP(PRAlarm);
153 0 : if (alarm != NULL)
154 : {
155 0 : if ((alarm->lock = PR_NewLock()) == NULL) goto done;
156 0 : if ((alarm->cond = PR_NewCondVar(alarm->lock)) == NULL) goto done;
157 0 : alarm->state = alarm_active;
158 0 : PR_INIT_CLIST(&alarm->timers);
159 0 : alarm->notifier = PR_CreateThread(
160 : PR_USER_THREAD, pr_alarmNotifier, alarm,
161 0 : PR_GetThreadPriority(PR_GetCurrentThread()),
162 : PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
163 0 : if (alarm->notifier == NULL) goto done;
164 : }
165 0 : return alarm;
166 :
167 : done:
168 0 : if (alarm->cond != NULL) PR_DestroyCondVar(alarm->cond);
169 0 : if (alarm->lock != NULL) PR_DestroyLock(alarm->lock);
170 0 : PR_DELETE(alarm);
171 0 : return NULL;
172 : } /* CreateAlarm */
173 :
174 0 : PR_IMPLEMENT(PRStatus) PR_DestroyAlarm(PRAlarm *alarm)
175 : {
176 : PRStatus rv;
177 :
178 0 : PR_Lock(alarm->lock);
179 0 : alarm->state = alarm_inactive;
180 0 : rv = PR_NotifyCondVar(alarm->cond);
181 0 : PR_Unlock(alarm->lock);
182 :
183 0 : if (rv == PR_SUCCESS)
184 0 : rv = PR_JoinThread(alarm->notifier);
185 0 : if (rv == PR_SUCCESS)
186 : {
187 0 : PR_DestroyCondVar(alarm->cond);
188 0 : PR_DestroyLock(alarm->lock);
189 0 : PR_DELETE(alarm);
190 : }
191 0 : return rv;
192 : } /* PR_DestroyAlarm */
193 :
194 0 : PR_IMPLEMENT(PRAlarmID*) PR_SetAlarm(
195 : PRAlarm *alarm, PRIntervalTime period, PRUint32 rate,
196 : PRPeriodicAlarmFn function, void *clientData)
197 : {
198 : /*
199 : * Create a new periodic alarm an existing current structure.
200 : * Set up the context and compute the first notify time (immediate).
201 : * Link the new ID into the head of the list (since it's notifying
202 : * immediately).
203 : */
204 :
205 0 : PRAlarmID *id = PR_NEWZAP(PRAlarmID);
206 :
207 0 : if (!id)
208 0 : return NULL;
209 :
210 0 : id->alarm = alarm;
211 0 : PR_INIT_CLIST(&id->list);
212 0 : id->function = function;
213 0 : id->clientData = clientData;
214 0 : id->period = period;
215 0 : id->rate = rate;
216 0 : id->epoch = id->nextNotify = PR_IntervalNow();
217 0 : (void)pr_PredictNextNotifyTime(id);
218 :
219 0 : PR_Lock(alarm->lock);
220 0 : PR_INSERT_BEFORE(&id->list, &alarm->timers);
221 0 : PR_NotifyCondVar(alarm->cond);
222 0 : PR_Unlock(alarm->lock);
223 :
224 0 : return id;
225 : } /* PR_SetAlarm */
226 :
227 0 : PR_IMPLEMENT(PRStatus) PR_ResetAlarm(
228 : PRAlarmID *id, PRIntervalTime period, PRUint32 rate)
229 : {
230 : /*
231 : * Can only be called from within the notify routine. Doesn't
232 : * need locking because it can only be called from within the
233 : * notify routine.
234 : */
235 0 : if (id != id->alarm->current)
236 0 : return PR_FAILURE;
237 0 : id->period = period;
238 0 : id->rate = rate;
239 0 : id->accumulator = 1;
240 0 : id->epoch = PR_IntervalNow();
241 0 : (void)pr_PredictNextNotifyTime(id);
242 0 : return PR_SUCCESS;
243 : } /* PR_ResetAlarm */
244 :
245 :
246 :
|