Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

637 lines
14 KiB

  1. /*
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. spxtimer.c
  5. Abstract:
  6. This file implements the timer routines used by the stack.
  7. Author:
  8. Jameel Hyder (jameelh@microsoft.com)
  9. Nikhil Kamkolkar (nikhilk@microsoft.com)
  10. Revision History:
  11. 23 Feb 1993 Initial Version
  12. Notes: Tab stop: 4
  13. --*/
  14. #include "precomp.h"
  15. #pragma hdrstop
  16. // Define module number for event logging entries
  17. #define FILENUM SPXTIMER
  18. // Discardable code after Init time
  19. #ifdef ALLOC_PRAGMA
  20. #pragma alloc_text(INIT, SpxTimerInit)
  21. #endif
  22. // Globals for this module
  23. PTIMERLIST spxTimerList = NULL;
  24. PTIMERLIST spxTimerTable[TIMER_HASH_TABLE] = {0};
  25. PTIMERLIST spxTimerActive = NULL;
  26. CTELock spxTimerLock = {0};
  27. LARGE_INTEGER spxTimerTick = {0};
  28. KTIMER spxTimer = {0};
  29. KDPC spxTimerDpc = {0};
  30. ULONG spxTimerId = 1;
  31. LONG spxTimerCount = 0;
  32. USHORT spxTimerDispatchCount = 0;
  33. BOOLEAN spxTimerStopped = FALSE;
  34. NTSTATUS
  35. SpxTimerInit(
  36. VOID
  37. )
  38. /*++
  39. Routine Description:
  40. Initialize the timer component for the appletalk stack.
  41. Arguments:
  42. Return Value:
  43. --*/
  44. {
  45. #if !defined(_PNP_POWER)
  46. BOOLEAN TimerStarted;
  47. #endif !_PNP_POWER
  48. // Initialize the timer and its associated Dpc. timer will be kicked
  49. // off when we get the first card arrival notification from ipx
  50. KeInitializeTimer(&spxTimer);
  51. CTEInitLock(&spxTimerLock);
  52. KeInitializeDpc(&spxTimerDpc, spxTimerDpcRoutine, NULL);
  53. spxTimerTick = RtlConvertLongToLargeInteger(SPX_TIMER_TICK);
  54. #if !defined(_PNP_POWER)
  55. TimerStarted = KeSetTimer(&spxTimer,
  56. spxTimerTick,
  57. &spxTimerDpc);
  58. CTEAssert(!TimerStarted);
  59. #endif !_PNP_POWER
  60. return STATUS_SUCCESS;
  61. }
  62. ULONG
  63. SpxTimerScheduleEvent(
  64. IN TIMER_ROUTINE Worker, // Routine to invoke when time expires
  65. IN ULONG MsTime, // Schedule after this much time
  66. IN PVOID pContext // Context(s) to pass to the routine
  67. )
  68. /*++
  69. Routine Description:
  70. Insert an event in the timer event list. If the list is empty, then
  71. fire off a timer. The time is specified in ms. We convert to ticks.
  72. Each tick is currently 100ms. It may not be zero or negative. The internal
  73. timer fires at 100ms granularity.
  74. Arguments:
  75. Return Value:
  76. --*/
  77. {
  78. PTIMERLIST pList;
  79. CTELockHandle lockHandle;
  80. ULONG DeltaTime;
  81. ULONG Id = 0;
  82. // Convert to ticks.
  83. DeltaTime = MsTime/SPX_MS_TO_TICKS;
  84. if (DeltaTime == 0)
  85. {
  86. DBGPRINT(SYSTEM, INFO,
  87. ("SpxTimerScheduleEvent: Converting %ld to ticks %ld\n",
  88. MsTime, DeltaTime));
  89. DeltaTime = 1;
  90. }
  91. DBGPRINT(SYSTEM, INFO,
  92. ("SpxTimerScheduleEvent: Converting %ld to ticks %ld\n",
  93. MsTime, DeltaTime));
  94. // Negative or Zero DeltaTime is invalid.
  95. CTEAssert (DeltaTime > 0);
  96. DBGPRINT(SYSTEM, INFO,
  97. ("SpxTimerScheduleEvent: Routine %lx, Time %d, Context %lx\n",
  98. Worker, DeltaTime, pContext));
  99. CTEGetLock(&spxTimerLock, &lockHandle);
  100. if (spxTimerStopped)
  101. {
  102. DBGPRINT(SYSTEM, FATAL,
  103. ("SpxTimerScheduleEvent: Called after Flush !!\n"));
  104. }
  105. else do
  106. {
  107. pList = SpxBPAllocBlock(BLKID_TIMERLIST);
  108. if (pList == NULL)
  109. {
  110. break;
  111. }
  112. #if DBG
  113. pList->tmr_Signature = TMR_SIGNATURE;
  114. #endif
  115. pList->tmr_Cancelled = FALSE;
  116. pList->tmr_Worker = Worker;
  117. pList->tmr_AbsTime = DeltaTime;
  118. pList->tmr_Context = pContext;
  119. Id = pList->tmr_Id = spxTimerId++;
  120. // Take care of wrap around
  121. if (spxTimerId == 0)
  122. spxTimerId = 1;
  123. // Enqueue this handler
  124. spxTimerEnqueue(pList);
  125. } while (FALSE);
  126. CTEFreeLock(&spxTimerLock, lockHandle);
  127. return Id;
  128. }
  129. VOID
  130. spxTimerDpcRoutine(
  131. IN PKDPC pKDpc,
  132. IN PVOID pContext,
  133. IN PVOID SystemArgument1,
  134. IN PVOID SystemArgument2
  135. )
  136. /*++
  137. Routine Description:
  138. This is called in at DISPATCH_LEVEL when the timer expires. The entry at
  139. the head of the list is decremented and if ZERO unlinked and dispatched.
  140. If the list is non-empty, the timer is fired again.
  141. Arguments:
  142. Return Value:
  143. --*/
  144. {
  145. PTIMERLIST pList, *ppList;
  146. BOOLEAN TimerStarted;
  147. ULONG ReEnqueueTime;
  148. CTELockHandle lockHandle;
  149. pKDpc; pContext; SystemArgument1; SystemArgument2;
  150. #if defined(_PNP_POWER)
  151. CTEGetLock(&spxTimerLock, &lockHandle);
  152. if (spxTimerStopped)
  153. {
  154. DBGPRINT(SYSTEM, ERR,
  155. ("spxTimerDpc: Enetered after Flush !!!\n"));
  156. CTEFreeLock(&spxTimerLock, lockHandle);
  157. return;
  158. }
  159. #else
  160. if (spxTimerStopped)
  161. {
  162. DBGPRINT(SYSTEM, ERR,
  163. ("spxTimerDpc: Enetered after Flush !!!\n"));
  164. return;
  165. }
  166. CTEGetLock(&spxTimerLock, &lockHandle);
  167. #endif _PNP_POWER
  168. SpxTimerCurrentTime ++; // Update our relative time
  169. #ifdef PROFILING
  170. // This is the only place where this is changed. And it always increases.
  171. SpxStatistics.stat_ElapsedTime = SpxTimerCurrentTime;
  172. #endif
  173. // We should never be here if we have no work to do
  174. if ((spxTimerList != NULL))
  175. {
  176. // Careful here. If two guys wanna go off together - let them !!
  177. if (spxTimerList->tmr_RelDelta != 0)
  178. (spxTimerList->tmr_RelDelta)--;
  179. // Dispatch the entry if it is ready to go
  180. if (spxTimerList->tmr_RelDelta == 0)
  181. {
  182. pList = spxTimerList;
  183. CTEAssert(VALID_TMR(pList));
  184. // Unlink from the list
  185. spxTimerList = pList->tmr_Next;
  186. if (spxTimerList != NULL)
  187. spxTimerList->tmr_Prev = &spxTimerList;
  188. // Unlink from the hash table now
  189. for (ppList = &spxTimerTable[pList->tmr_Id % TIMER_HASH_TABLE];
  190. *ppList != NULL;
  191. ppList = &((*ppList)->tmr_Overflow))
  192. {
  193. CTEAssert(VALID_TMR(*ppList));
  194. if (*ppList == pList)
  195. {
  196. *ppList = pList->tmr_Overflow;
  197. break;
  198. }
  199. }
  200. CTEAssert (*ppList == pList->tmr_Overflow);
  201. DBGPRINT(SYSTEM, INFO,
  202. ("spxTimerDpcRoutine: Dispatching %lx\n",
  203. pList->tmr_Worker));
  204. spxTimerDispatchCount ++;
  205. spxTimerCount --;
  206. spxTimerActive = pList;
  207. CTEFreeLock(&spxTimerLock, lockHandle);
  208. // If reenqueue time is 0, do not requeue. If 1, then requeue with
  209. // current value, else use value specified.
  210. ReEnqueueTime = (*pList->tmr_Worker)(pList->tmr_Context, FALSE);
  211. DBGPRINT(SYSTEM, INFO,
  212. ("spxTimerDpcRoutine: Reenequeu time %lx.%lx\n",
  213. ReEnqueueTime, pList->tmr_AbsTime));
  214. CTEGetLock(&spxTimerLock, &lockHandle);
  215. spxTimerActive = NULL;
  216. spxTimerDispatchCount --;
  217. if (ReEnqueueTime != TIMER_DONT_REQUEUE)
  218. {
  219. // If this chappie was cancelled while it was running
  220. // and it wants to be re-queued, do it right away.
  221. if (pList->tmr_Cancelled)
  222. {
  223. (*pList->tmr_Worker)(pList->tmr_Context, FALSE);
  224. SpxBPFreeBlock(pList, BLKID_TIMERLIST);
  225. }
  226. else
  227. {
  228. if (ReEnqueueTime != TIMER_REQUEUE_CUR_VALUE)
  229. {
  230. pList->tmr_AbsTime = ReEnqueueTime/SPX_MS_TO_TICKS;
  231. if (pList->tmr_AbsTime == 0)
  232. {
  233. DBGPRINT(SYSTEM, INFO,
  234. ("SpxTimerDispatch: Requeue at %ld\n",
  235. pList->tmr_AbsTime));
  236. }
  237. DBGPRINT(SYSTEM, INFO,
  238. ("SpxTimerDispatch: Requeue at %ld.%ld\n",
  239. ReEnqueueTime, pList->tmr_AbsTime));
  240. }
  241. spxTimerEnqueue(pList);
  242. }
  243. }
  244. else
  245. {
  246. SpxBPFreeBlock(pList, BLKID_TIMERLIST);
  247. }
  248. }
  249. }
  250. #if defined(_PNP_POWER)
  251. if (!spxTimerStopped)
  252. {
  253. TimerStarted = KeSetTimer(&spxTimer,
  254. spxTimerTick,
  255. &spxTimerDpc);
  256. // it is possible that while we were here in Dpc, PNP_ADD_DEVICE
  257. // restarted the timer, so this assert is commented out for PnP
  258. // CTEAssert(!TimerStarted);
  259. }
  260. CTEFreeLock(&spxTimerLock, lockHandle);
  261. #else
  262. CTEFreeLock(&spxTimerLock, lockHandle);
  263. if (!spxTimerStopped)
  264. {
  265. TimerStarted = KeSetTimer(&spxTimer,
  266. spxTimerTick,
  267. &spxTimerDpc);
  268. CTEAssert(!TimerStarted);
  269. }
  270. #endif _PNP_POWER
  271. }
  272. VOID
  273. spxTimerEnqueue(
  274. IN PTIMERLIST pListNew
  275. )
  276. /*++
  277. Routine Description:
  278. Here is a thesis on the code that follows.
  279. The timer events are maintained as a list which the timer dpc routine
  280. looks at every timer tick. The list is maintained in such a way that only
  281. the head of the list needs to be updated every tick i.e. the entire list
  282. is never scanned. The way this is achieved is by keeping delta times
  283. relative to the previous entry.
  284. Every timer tick, the relative time at the head of the list is decremented.
  285. When that goes to ZERO, the head of the list is unlinked and dispatched.
  286. To give an example, we have the following events queued at time slots
  287. X Schedule A after 10 ticks.
  288. X+3 Schedule B after 5 ticks.
  289. X+5 Schedule C after 4 ticks.
  290. X+8 Schedule D after 6 ticks.
  291. So A will schedule at X+10, B at X+8 (X+3+5), C at X+9 (X+5+4) and
  292. D at X+14 (X+8+6).
  293. The above example covers all the situations.
  294. - NULL List.
  295. - Inserting at head of list.
  296. - Inserting in the middle of the list.
  297. - Appending to the list tail.
  298. The list will look as follows.
  299. BEFORE AFTER
  300. ------ -----
  301. X Head -->| Head -> A(10) ->|
  302. A(10)
  303. X+3 Head -> A(7) ->| Head -> B(5) -> A(2) ->|
  304. B(5)
  305. X+5 Head -> B(3) -> A(2) ->| Head -> B(3) -> C(1) -> A(1) ->|
  306. C(4)
  307. X+8 Head -> C(1) -> A(1) ->| Head -> C(1) -> A(1) -> D(4) ->|
  308. D(6)
  309. The granularity is one tick. THIS MUST BE CALLED WITH THE TIMER LOCK HELD.
  310. Arguments:
  311. Return Value:
  312. --*/
  313. {
  314. PTIMERLIST pList, *ppList;
  315. ULONG DeltaTime = pListNew->tmr_AbsTime;
  316. // The DeltaTime is adjusted in every pass of the loop to reflect the
  317. // time after the previous entry that the new entry will schedule.
  318. for (ppList = &spxTimerList;
  319. (pList = *ppList) != NULL;
  320. ppList = &pList->tmr_Next)
  321. {
  322. CTEAssert(VALID_TMR(pList));
  323. if (DeltaTime <= pList->tmr_RelDelta)
  324. {
  325. pList->tmr_RelDelta -= DeltaTime;
  326. break;
  327. }
  328. DeltaTime -= pList->tmr_RelDelta;
  329. }
  330. // Link this in the chain
  331. pListNew->tmr_RelDelta = DeltaTime;
  332. pListNew->tmr_Next = pList;
  333. pListNew->tmr_Prev = ppList;
  334. *ppList = pListNew;
  335. if (pList != NULL)
  336. {
  337. pList->tmr_Prev = &pListNew->tmr_Next;
  338. }
  339. // Now link it in the hash table
  340. pListNew->tmr_Overflow = spxTimerTable[pListNew->tmr_Id % TIMER_HASH_TABLE];
  341. spxTimerTable[pListNew->tmr_Id % TIMER_HASH_TABLE] = pListNew;
  342. spxTimerCount ++;
  343. }
  344. VOID
  345. SpxTimerFlushAndStop(
  346. VOID
  347. )
  348. /*++
  349. Routine Description:
  350. Force all entries in the timer queue to be dispatched immediately. No
  351. more queue'ing of timer routines is permitted after this. The timer
  352. essentially shuts down.
  353. Arguments:
  354. Return Value:
  355. --*/
  356. {
  357. PTIMERLIST pList;
  358. CTELockHandle lockHandle;
  359. CTEAssert (KeGetCurrentIrql() == LOW_LEVEL);
  360. DBGPRINT(SYSTEM, ERR,
  361. ("SpxTimerFlushAndStop: Entered\n"));
  362. CTEGetLock(&spxTimerLock, &lockHandle);
  363. spxTimerStopped = TRUE;
  364. KeCancelTimer(&spxTimer);
  365. if (spxTimerList != NULL)
  366. {
  367. // Dispatch all entries right away
  368. while (spxTimerList != NULL)
  369. {
  370. pList = spxTimerList;
  371. CTEAssert(VALID_TMR(pList));
  372. spxTimerList = pList->tmr_Next;
  373. DBGPRINT(SYSTEM, INFO,
  374. ("spxTimerFlushAndStop: Dispatching %lx\n",
  375. pList->tmr_Worker));
  376. // The timer routines assume they are being called at DISPATCH
  377. // level. This is OK since we are calling with SpinLock held.
  378. (*pList->tmr_Worker)(pList->tmr_Context, TRUE);
  379. spxTimerCount --;
  380. SpxBPFreeBlock(pList, BLKID_TIMERLIST);
  381. }
  382. RtlZeroMemory(spxTimerTable, sizeof(spxTimerTable));
  383. }
  384. CTEFreeLock(&spxTimerLock, lockHandle);
  385. // Wait for all timer routines to complete
  386. while (spxTimerDispatchCount != 0)
  387. {
  388. SpxSleep(SPX_TIMER_WAIT);
  389. }
  390. }
  391. BOOLEAN
  392. SpxTimerCancelEvent(
  393. IN ULONG TimerId,
  394. IN BOOLEAN ReEnqueue
  395. )
  396. /*++
  397. Routine Description:
  398. Cancel a previously scheduled timer event, if it hasn't fired already.
  399. Arguments:
  400. Return Value:
  401. --*/
  402. {
  403. PTIMERLIST pList, *ppList;
  404. CTELockHandle lockHandle;
  405. DBGPRINT(SYSTEM, INFO,
  406. ("SpxTimerCancelEvent: Entered for TimerId %ld\n", TimerId));
  407. CTEAssert(TimerId != 0);
  408. CTEGetLock(&spxTimerLock, &lockHandle);
  409. for (ppList = &spxTimerTable[TimerId % TIMER_HASH_TABLE];
  410. (pList = *ppList) != NULL;
  411. ppList = &pList->tmr_Overflow)
  412. {
  413. CTEAssert(VALID_TMR(pList));
  414. // If we find it, cancel it
  415. if (pList->tmr_Id == TimerId)
  416. {
  417. // Unlink this from the hash table
  418. *ppList = pList->tmr_Overflow;
  419. // ... and from the list
  420. if (pList->tmr_Next != NULL)
  421. {
  422. pList->tmr_Next->tmr_RelDelta += pList->tmr_RelDelta;
  423. pList->tmr_Next->tmr_Prev = pList->tmr_Prev;
  424. }
  425. *(pList->tmr_Prev) = pList->tmr_Next;
  426. spxTimerCount --;
  427. if (ReEnqueue)
  428. spxTimerEnqueue(pList);
  429. else SpxBPFreeBlock(pList, BLKID_TIMERLIST);
  430. break;
  431. }
  432. }
  433. // If we could not find it in the list, see if it currently running.
  434. // If so mark him to not reschedule itself, only if reenqueue was false.
  435. if (pList == NULL)
  436. {
  437. if ((spxTimerActive != NULL) &&
  438. (spxTimerActive->tmr_Id == TimerId) &&
  439. !ReEnqueue)
  440. {
  441. spxTimerActive->tmr_Cancelled = TRUE;
  442. }
  443. }
  444. CTEFreeLock(&spxTimerLock, lockHandle);
  445. DBGPRINT(SYSTEM, INFO,
  446. ("SpxTimerCancelEvent: %s for Id %ld\n",
  447. (pList != NULL) ? "Success" : "Failure", TimerId));
  448. return (pList != NULL);
  449. }
  450. #if DBG
  451. VOID
  452. SpxTimerDumpList(
  453. VOID
  454. )
  455. {
  456. PTIMERLIST pList;
  457. ULONG CumTime = 0;
  458. CTELockHandle lockHandle;
  459. DBGPRINT(DUMP, FATAL,
  460. ("TIMER LIST: (Times are in %dms units\n", 1000));
  461. DBGPRINT(DUMP, FATAL,
  462. ("\tTimerId Time(Abs) Time(Rel) Routine Address\n"));
  463. CTEGetLock(&spxTimerLock, &lockHandle);
  464. for (pList = spxTimerList;
  465. pList != NULL;
  466. pList = pList->tmr_Next)
  467. {
  468. CumTime += pList->tmr_RelDelta;
  469. DBGPRINT(DUMP, FATAL,
  470. ("\t% 6lx %5d %5ld %lx\n",
  471. pList->tmr_Id, pList->tmr_AbsTime, CumTime, pList->tmr_Worker));
  472. }
  473. CTEFreeLock(&spxTimerLock, lockHandle);
  474. }
  475. #endif
  476.