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