Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(794)

Side by Side Diff: nspr/pr/src/misc/pralarm.c

Issue 2078763002: Delete bundled copy of NSS and replace with README. (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/nss@master
Patch Set: Delete bundled copy of NSS and replace with README. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « nspr/pr/src/memory/prshma.c ('k') | nspr/pr/src/misc/pratom.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 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 PRAlarmID *result = id;
50 PRIntervalTime now = PR_IntervalNow();
51
52 if (!PR_CLIST_IS_EMPTY(&alarm->timers))
53 {
54 if (id != NULL) /* have to put this id back in */
55 {
56 PRIntervalTime idDelta = now - id->nextNotify;
57 timer = alarm->timers.next;
58 do
59 {
60 result = (PRAlarmID*)timer;
61 if ((PRIntervalTime)(now - result->nextNotify) > idDelta)
62 {
63 PR_INSERT_BEFORE(&id->list, &alarm->timers);
64 break;
65 }
66 timer = timer->next;
67 } while (timer != &alarm->timers);
68 }
69 result = (PRAlarmID*)(timer = PR_LIST_HEAD(&alarm->timers));
70 PR_REMOVE_LINK(timer); /* remove it from the list */
71 }
72
73 return result;
74 } /* pr_getNextAlarm */
75
76 static PRIntervalTime pr_PredictNextNotifyTime(PRAlarmID *id)
77 {
78 PRIntervalTime delta;
79 PRFloat64 baseRate = (PRFloat64)id->period / (PRFloat64)id->rate;
80 PRFloat64 offsetFromEpoch = (PRFloat64)id->accumulator * baseRate;
81
82 id->accumulator += 1; /* every call advances to next period */
83 id->lastNotify = id->nextNotify; /* just keeping track of things */
84 id->nextNotify = (PRIntervalTime)(offsetFromEpoch + 0.5);
85
86 delta = id->nextNotify - id->lastNotify;
87 return delta;
88 } /* pr_PredictNextNotifyTime */
89
90 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 PRAlarmID *id = NULL;
99 PRAlarm *alarm = (PRAlarm*)arg;
100 enum {notify, abort, scan} why = scan;
101
102 while (why != abort)
103 {
104 PRIntervalTime pause;
105
106 PR_Lock(alarm->lock);
107 while (why == scan)
108 {
109 alarm->current = NULL; /* reset current id */
110 if (alarm->state == alarm_inactive) why = abort; /* we're toast */
111 else if (why == scan) /* the dominant case */
112 {
113 id = pr_getNextAlarm(alarm, id); /* even if it's the same */
114 if (id == NULL) /* there are no alarms set */
115 (void)PR_WaitCondVar(alarm->cond, PR_INTERVAL_NO_TIMEOUT);
116 else
117 {
118 pause = id->nextNotify - (PR_IntervalNow() - id->epoch);
119 if ((PRInt32)pause <= 0) /* is this one's time up? */
120 {
121 why = notify; /* set up to do our thing */
122 alarm->current = id; /* id we're about to schedule */
123 }
124 else
125 (void)PR_WaitCondVar(alarm->cond, pause); /* dally */
126 }
127 }
128 }
129 PR_Unlock(alarm->lock);
130
131 if (why == notify)
132 {
133 (void)pr_PredictNextNotifyTime(id);
134 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 PR_DELETE(id); /* free notifier object */
142 id = NULL; /* so it doesn't get back into the list */
143 }
144 why = scan; /* so we can cycle through the loop again */
145 }
146 }
147
148 } /* pr_alarm_notifier */
149
150 PR_IMPLEMENT(PRAlarm*) PR_CreateAlarm(void)
151 {
152 PRAlarm *alarm = PR_NEWZAP(PRAlarm);
153 if (alarm != NULL)
154 {
155 if ((alarm->lock = PR_NewLock()) == NULL) goto done;
156 if ((alarm->cond = PR_NewCondVar(alarm->lock)) == NULL) goto done;
157 alarm->state = alarm_active;
158 PR_INIT_CLIST(&alarm->timers);
159 alarm->notifier = PR_CreateThread(
160 PR_USER_THREAD, pr_alarmNotifier, alarm,
161 PR_GetThreadPriority(PR_GetCurrentThread()),
162 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
163 if (alarm->notifier == NULL) goto done;
164 }
165 return alarm;
166
167 done:
168 if (alarm->cond != NULL) PR_DestroyCondVar(alarm->cond);
169 if (alarm->lock != NULL) PR_DestroyLock(alarm->lock);
170 PR_DELETE(alarm);
171 return NULL;
172 } /* CreateAlarm */
173
174 PR_IMPLEMENT(PRStatus) PR_DestroyAlarm(PRAlarm *alarm)
175 {
176 PRStatus rv;
177
178 PR_Lock(alarm->lock);
179 alarm->state = alarm_inactive;
180 rv = PR_NotifyCondVar(alarm->cond);
181 PR_Unlock(alarm->lock);
182
183 if (rv == PR_SUCCESS)
184 rv = PR_JoinThread(alarm->notifier);
185 if (rv == PR_SUCCESS)
186 {
187 PR_DestroyCondVar(alarm->cond);
188 PR_DestroyLock(alarm->lock);
189 PR_DELETE(alarm);
190 }
191 return rv;
192 } /* PR_DestroyAlarm */
193
194 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 PRAlarmID *id = PR_NEWZAP(PRAlarmID);
206
207 if (!id)
208 return NULL;
209
210 id->alarm = alarm;
211 PR_INIT_CLIST(&id->list);
212 id->function = function;
213 id->clientData = clientData;
214 id->period = period;
215 id->rate = rate;
216 id->epoch = id->nextNotify = PR_IntervalNow();
217 (void)pr_PredictNextNotifyTime(id);
218
219 PR_Lock(alarm->lock);
220 PR_INSERT_BEFORE(&id->list, &alarm->timers);
221 PR_NotifyCondVar(alarm->cond);
222 PR_Unlock(alarm->lock);
223
224 return id;
225 } /* PR_SetAlarm */
226
227 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 if (id != id->alarm->current)
236 return PR_FAILURE;
237 id->period = period;
238 id->rate = rate;
239 id->accumulator = 1;
240 id->epoch = PR_IntervalNow();
241 (void)pr_PredictNextNotifyTime(id);
242 return PR_SUCCESS;
243 } /* PR_ResetAlarm */
244
245
246
OLDNEW
« no previous file with comments | « nspr/pr/src/memory/prshma.c ('k') | nspr/pr/src/misc/pratom.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698