Source code of Windows XP (NT5)
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.

2980 lines
75 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. timer.c
  5. Abstract:
  6. This module defines functions for the timer thread pool.
  7. Author:
  8. Gurdeep Singh Pall (gurdeep) Nov 13, 1997
  9. Revision History:
  10. lokeshs - extended/modified threadpool.
  11. Rob Earhart (earhart) September 29, 2000
  12. Split off from threads.c
  13. Environment:
  14. These routines are statically linked in the caller's executable
  15. and are callable only from user mode. They make use of Nt system
  16. services.
  17. --*/
  18. #include <ntos.h>
  19. #include <ntrtl.h>
  20. #include "ntrtlp.h"
  21. #include "threads.h"
  22. // Timer Thread Pool
  23. // -----------------
  24. // Clients create one or more Timer Queues and insert one shot or periodic
  25. // timers in them. All timers in a queue are kept in a "Delta List" with each
  26. // timer's firing time relative to the timer before it. All Queues are also
  27. // kept in a "Delta List" with each Queue's firing time (set to the firing time
  28. // of the nearest firing timer) relative to the Queue before it. One NT Timer
  29. // is used to service all timers in all queues.
  30. ULONG StartedTimerInitialization ; // Used by Timer thread startup synchronization
  31. ULONG CompletedTimerInitialization ; // Used for to check if Timer thread is initialized
  32. HANDLE TimerThreadHandle ; // Holds the timer thread handle
  33. ULONG TimerThreadId ; // Used to check if current thread is a timer thread
  34. LIST_ENTRY TimerQueues ; // All timer queues are linked in this list
  35. HANDLE TimerHandle ; // Holds handle of NT Timer used by the Timer Thread
  36. HANDLE TimerThreadStartedEvent ; // Indicates that the timer thread has started
  37. ULONG NumTimerQueues ; // Number of timer queues
  38. RTL_CRITICAL_SECTION TimerCriticalSection ; // Exclusion used by timer threads
  39. LARGE_INTEGER Last64BitTickCount ;
  40. LARGE_INTEGER Resync64BitTickCount ;
  41. LARGE_INTEGER Firing64BitTickCount ;
  42. #if DBG
  43. ULONG RtlpDueTimeMax = 0;
  44. #endif
  45. #if DBG1
  46. ULONG NextTimerDbgId;
  47. #endif
  48. #define RtlpGetResync64BitTickCount() Resync64BitTickCount.QuadPart
  49. #define RtlpSetFiring64BitTickCount(Timeout) \
  50. Firing64BitTickCount.QuadPart = (Timeout)
  51. __inline
  52. LONGLONG
  53. RtlpGet64BitTickCount(
  54. LARGE_INTEGER *Last64BitTickCount
  55. )
  56. /*++
  57. Routine Description:
  58. This routine is used for getting the latest 64bit tick count.
  59. Arguments:
  60. Return Value: 64bit tick count
  61. --*/
  62. {
  63. LARGE_INTEGER liCurTime ;
  64. liCurTime.QuadPart = NtGetTickCount() + Last64BitTickCount->HighPart ;
  65. // see if timer has wrapped.
  66. if (liCurTime.LowPart < Last64BitTickCount->LowPart) {
  67. liCurTime.HighPart++ ;
  68. }
  69. return (Last64BitTickCount->QuadPart = liCurTime.QuadPart) ;
  70. }
  71. __inline
  72. LONGLONG
  73. RtlpResync64BitTickCount(
  74. )
  75. /*++
  76. Routine Description:
  77. This routine is used for getting the latest 64bit tick count.
  78. Arguments:
  79. Return Value: 64bit tick count
  80. Remarks: This call should be made in the first line of any APC queued
  81. to the timer thread and nowhere else. It is used to reduce the drift
  82. --*/
  83. {
  84. return Resync64BitTickCount.QuadPart =
  85. RtlpGet64BitTickCount(&Last64BitTickCount);
  86. }
  87. VOID
  88. RtlpAsyncTimerCallbackCompletion(
  89. PVOID Context
  90. )
  91. /*++
  92. Routine Description:
  93. This routine is called in a (IO)worker thread and is used to decrement the
  94. RefCount at the end and call RtlpDeleteTimer if required
  95. Arguments:
  96. Context - pointer to the Timer object,
  97. Return Value:
  98. --*/
  99. {
  100. PRTLP_TIMER Timer = (PRTLP_TIMER) Context;
  101. #if DBG
  102. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  103. RTLP_THREADPOOL_TRACE_MASK,
  104. "<%d> Calling WaitOrTimer:Timer: fn:%x context:%x bool:%d Thread<%d:%d>\n",
  105. Timer->DbgId,
  106. (ULONG_PTR)Timer->Function, (ULONG_PTR)Timer->Context,
  107. TRUE,
  108. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  109. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  110. #endif
  111. RtlpWaitOrTimerCallout(Timer->Function,
  112. Timer->Context,
  113. TRUE,
  114. Timer->ActivationContext,
  115. Timer->ImpersonationToken);
  116. // decrement RefCount after function is executed so that the context is not deleted
  117. if ( InterlockedDecrement( &Timer->RefCount ) == 0 ) {
  118. RtlpDeleteTimer( Timer ) ;
  119. }
  120. }
  121. VOID
  122. RtlpFireTimers (
  123. PLIST_ENTRY TimersToFireList
  124. )
  125. /*++
  126. Routine Description:
  127. Finally all the timers are fired here.
  128. Arguments:
  129. TimersToFireList: List of timers to fire
  130. --*/
  131. {
  132. PLIST_ENTRY Node ;
  133. PRTLP_TIMER Timer ;
  134. NTSTATUS Status;
  135. for (Node = TimersToFireList->Flink; Node != TimersToFireList; Node = TimersToFireList->Flink)
  136. {
  137. Timer = CONTAINING_RECORD (Node, RTLP_TIMER, TimersToFireList) ;
  138. RemoveEntryList( Node ) ;
  139. InitializeListHead( Node ) ;
  140. if ( (Timer->State & STATE_DONTFIRE)
  141. || (Timer->Queue->State & STATE_DONTFIRE) )
  142. {
  143. //
  144. // Wait timers *never* use STATE_DONTFIRE. Let's just
  145. // make sure this isn't one:
  146. //
  147. ASSERT(Timer->Wait == NULL);
  148. } else if ( Timer->Flags & (WT_EXECUTEINTIMERTHREAD | WT_EXECUTEINWAITTHREAD ) ) {
  149. #if DBG
  150. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  151. RTLP_THREADPOOL_TRACE_MASK,
  152. "<%d> Calling WaitOrTimer(Timer): fn:%x context:%x bool:%d Thread<%d:%d>\n",
  153. Timer->DbgId,
  154. (ULONG_PTR)Timer->Function, (ULONG_PTR)Timer->Context,
  155. TRUE,
  156. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  157. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  158. #endif
  159. RtlpWaitOrTimerCallout(Timer->Function,
  160. Timer->Context,
  161. TRUE,
  162. Timer->ActivationContext,
  163. Timer->ImpersonationToken);
  164. } else {
  165. // timer associated with WaitEvents should be treated differently
  166. if ( Timer->Wait != NULL ) {
  167. InterlockedIncrement( Timer->RefCountPtr ) ;
  168. // Set the low bit of the context to indicate to
  169. // RtlpAsyncWaitCallbackCompletion that this is a
  170. // timer-initiated callback.
  171. Status = RtlQueueWorkItem(RtlpAsyncWaitCallbackCompletion,
  172. (PVOID)(((ULONG_PTR) Timer->Wait) | 1),
  173. Timer->Flags);
  174. } else {
  175. InterlockedIncrement( &Timer->RefCount ) ;
  176. Status = RtlQueueWorkItem(RtlpAsyncTimerCallbackCompletion,
  177. Timer,
  178. Timer->Flags);
  179. }
  180. if (!NT_SUCCESS(Status)) {
  181. // NTRAID#202802-2000/10/12-earhart: we really ought
  182. // to deal with this case in a better way, since we
  183. // can't guarantee (with our current architecture)
  184. // that the enqueue will work.
  185. if ( Timer->Wait != NULL ) {
  186. InterlockedDecrement( Timer->RefCountPtr ) ;
  187. } else {
  188. InterlockedDecrement( &Timer->RefCount ) ;
  189. }
  190. }
  191. }
  192. //
  193. // If it's a singleshot wait timer, we can free it now.
  194. //
  195. if (Timer->Wait != NULL
  196. && Timer->Period == 0) {
  197. RtlpFreeTPHeap(Timer);
  198. }
  199. }
  200. }
  201. VOID
  202. RtlpFireTimersAndReorder (
  203. PRTLP_TIMER_QUEUE Queue,
  204. ULONG *NewFiringTime,
  205. PLIST_ENTRY TimersToFireList
  206. )
  207. /*++
  208. Routine Description:
  209. Fires all timers in TimerList that have DeltaFiringTime == 0. After firing the timers
  210. it reorders the timers based on their periodic times OR frees the fired one shot timers.
  211. Arguments:
  212. TimerList - Timer list to work thru.
  213. NewFiringTime - Location where the new firing time for the first timer in the delta list
  214. is returned.
  215. Return Value:
  216. --*/
  217. {
  218. PLIST_ENTRY TNode ;
  219. PRTLP_TIMER Timer ;
  220. LIST_ENTRY ReinsertTimerList ;
  221. PLIST_ENTRY TimerList = &Queue->TimerList ;
  222. InitializeListHead (&ReinsertTimerList) ;
  223. *NewFiringTime = 0 ;
  224. for (TNode = TimerList->Flink ; (TNode != TimerList) && (*NewFiringTime == 0);
  225. TNode = TimerList->Flink)
  226. {
  227. Timer = CONTAINING_RECORD (TNode, RTLP_TIMER, List) ;
  228. // Fire all timers with delta time of 0
  229. if (Timer->DeltaFiringTime == 0) {
  230. // detach this timer from the list
  231. RemoveEntryList (TNode) ;
  232. // get next firing time
  233. if (!IsListEmpty(TimerList)) {
  234. PRTLP_TIMER TmpTimer ;
  235. TmpTimer = CONTAINING_RECORD (TimerList->Flink, RTLP_TIMER, List) ;
  236. *NewFiringTime = TmpTimer->DeltaFiringTime ;
  237. TmpTimer->DeltaFiringTime = 0 ;
  238. } else {
  239. *NewFiringTime = INFINITE_TIME ;
  240. }
  241. // if timer is not periodic then remove active state. Timer will be deleted
  242. // when cancel timer is called.
  243. if (Timer->Period == 0) {
  244. if ( Timer->Wait ) {
  245. // If one shot wait was timed out, then deactivate the
  246. // wait. Make sure that RtlpDeactivateWait knows
  247. // we're going to continue using the timer's memory.
  248. RtlpDeactivateWait( Timer->Wait, FALSE ) ;
  249. // The timer does *not* go on the uncancelled
  250. // timer list. Initialize its list head to avoid
  251. // refering to other timers.
  252. InitializeListHead( &Timer->List );
  253. }
  254. else {
  255. // If a normal non-periodic timer was timed out,
  256. // then insert it into the uncancelled timer list.
  257. InsertHeadList( &Queue->UncancelledTimerList, &Timer->List ) ;
  258. // should be set at the end
  259. RtlInterlockedClearBitsDiscardReturn(&Timer->State,
  260. STATE_ACTIVE);
  261. }
  262. RtlInterlockedSetBitsDiscardReturn(&Timer->State,
  263. STATE_ONE_SHOT_FIRED);
  264. } else {
  265. // Set the DeltaFiringTime to be the next period
  266. Timer->DeltaFiringTime = Timer->Period ;
  267. // reinsert the timer in the list.
  268. RtlpInsertInDeltaList (TimerList, Timer, *NewFiringTime, NewFiringTime) ;
  269. }
  270. // Call the function associated with this timer. call it in the end
  271. // so that RtlTimer calls can be made in the timer function
  272. if ( (Timer->State & STATE_DONTFIRE)
  273. || (Timer->Queue->State & STATE_DONTFIRE) )
  274. {
  275. //
  276. // Wait timers *never* use STATE_DONTFIRE. Let's just
  277. // make sure this isn't one:
  278. //
  279. ASSERT(Timer->Wait == NULL);
  280. } else {
  281. InsertTailList( TimersToFireList, &Timer->TimersToFireList ) ;
  282. }
  283. } else {
  284. // No more Timers with DeltaFiringTime == 0
  285. break ;
  286. }
  287. }
  288. if ( *NewFiringTime == 0 ) {
  289. *NewFiringTime = INFINITE_TIME ;
  290. }
  291. }
  292. VOID
  293. RtlpInsertTimersIntoDeltaList (
  294. IN PLIST_ENTRY NewTimerList,
  295. IN PLIST_ENTRY DeltaTimerList,
  296. IN ULONG TimeRemaining,
  297. OUT ULONG *NewFiringTime
  298. )
  299. /*++
  300. Routine Description:
  301. This routine walks thru a list of timers in NewTimerList and inserts them into a delta
  302. timers list pointed to by DeltaTimerList. The timeout associated with the first element
  303. in the new list is returned in NewFiringTime.
  304. Arguments:
  305. NewTimerList - List of timers that need to be inserted into the DeltaTimerList
  306. DeltaTimerList - Existing delta list of zero or more timers.
  307. TimeRemaining - Firing time of the first element in the DeltaTimerList
  308. NewFiringTime - Location where the new firing time will be returned
  309. Return Value:
  310. --*/
  311. {
  312. PRTLP_GENERIC_TIMER Timer ;
  313. PLIST_ENTRY TNode ;
  314. PLIST_ENTRY Temp ;
  315. for (TNode = NewTimerList->Flink ; TNode != NewTimerList ; TNode = TNode->Flink) {
  316. Temp = TNode->Blink ;
  317. RemoveEntryList (Temp->Flink) ;
  318. Timer = CONTAINING_RECORD (TNode, RTLP_GENERIC_TIMER, List) ;
  319. if (RtlpInsertInDeltaList (DeltaTimerList, Timer, TimeRemaining, NewFiringTime)) {
  320. TimeRemaining = *NewFiringTime ;
  321. }
  322. TNode = Temp ;
  323. }
  324. }
  325. VOID
  326. RtlpServiceTimer (
  327. PVOID NotUsedArg,
  328. ULONG NotUsedLowTimer,
  329. LONG NotUsedHighTimer
  330. )
  331. /*++
  332. Routine Description:
  333. Services the timer. Runs in an APC.
  334. Arguments:
  335. NotUsedArg - Argument is not used in this function.
  336. NotUsedLowTimer - Argument is not used in this function.
  337. NotUsedHighTimer - Argument is not used in this function.
  338. Return Value:
  339. Remarks:
  340. This APC is called only for timeouts of timer threads.
  341. --*/
  342. {
  343. PRTLP_TIMER Timer ;
  344. PRTLP_TIMER_QUEUE Queue ;
  345. PLIST_ENTRY TNode ;
  346. PLIST_ENTRY QNode ;
  347. PLIST_ENTRY Temp ;
  348. ULONG NewFiringTime ;
  349. LIST_ENTRY ReinsertTimerQueueList ;
  350. LIST_ENTRY TimersToFireList ;
  351. RtlpResync64BitTickCount() ;
  352. #if DBG
  353. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  354. RTLP_THREADPOOL_VERBOSE_MASK,
  355. "Before service timer ThreadId<%x:%x>\n",
  356. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  357. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  358. RtlDebugPrintTimes ();
  359. #endif
  360. ACQUIRE_GLOBAL_TIMER_LOCK();
  361. // fire it if it even 200ms ahead. else reset the timer
  362. if (Firing64BitTickCount.QuadPart > RtlpGet64BitTickCount(&Last64BitTickCount) + 200) {
  363. RtlpResetTimer (TimerHandle, RtlpGetTimeRemaining (TimerHandle), NULL) ;
  364. RELEASE_GLOBAL_TIMER_LOCK() ;
  365. return ;
  366. }
  367. InitializeListHead (&ReinsertTimerQueueList) ;
  368. InitializeListHead (&TimersToFireList) ;
  369. // We run thru all queues with DeltaFiringTime == 0 and fire all timers that
  370. // have DeltaFiringTime == 0. We remove the fired timers and either free them
  371. // (for one shot timers) or put them in aside list (for periodic timers).
  372. // After we have finished firing all timers in a queue we reinsert the timers
  373. // in the aside list back into the queue based on their new firing times.
  374. //
  375. // Similarly, we remove each fired Queue and put it in a aside list. After firing
  376. // all queues with DeltaFiringTime == 0, we reinsert the Queues in the aside list
  377. // and reprogram the NT timer to be the firing time of the first queue in the list
  378. for (QNode = TimerQueues.Flink ; QNode != &TimerQueues ; QNode = QNode->Flink) {
  379. Queue = CONTAINING_RECORD (QNode, RTLP_TIMER_QUEUE, List) ;
  380. // If the delta time in the timer queue is 0 - then this queue
  381. // has timers that are ready to fire. Walk the list and fire all timers with
  382. // Delta time of 0
  383. if (Queue->DeltaFiringTime == 0) {
  384. // Walk all timers with DeltaFiringTime == 0 and fire them. After that
  385. // reinsert the periodic timers in the appropriate place.
  386. RtlpFireTimersAndReorder (Queue, &NewFiringTime, &TimersToFireList) ;
  387. // detach this Queue from the list
  388. QNode = QNode->Blink ;
  389. RemoveEntryList (QNode->Flink) ;
  390. // If there are timers in the queue then prepare to reinsert the queue in
  391. // TimerQueues.
  392. if (NewFiringTime != INFINITE_TIME) {
  393. Queue->DeltaFiringTime = NewFiringTime ;
  394. // put the timer in list that we will process after we have
  395. // fired all elements in this queue
  396. InsertHeadList (&ReinsertTimerQueueList, &Queue->List) ;
  397. } else {
  398. // Queue has no more timers in it. Let the Queue float.
  399. InitializeListHead (&Queue->List) ;
  400. }
  401. } else {
  402. // No more Queues with DeltaFiringTime == 0
  403. break ;
  404. }
  405. }
  406. // At this point we have fired all the ready timers. We have two lists that need to be
  407. // merged - TimerQueues and ReinsertTimerQueueList. The following steps do this - at the
  408. // end of this we will reprogram the NT Timer.
  409. if (!IsListEmpty(&TimerQueues)) {
  410. Queue = CONTAINING_RECORD (TimerQueues.Flink, RTLP_TIMER_QUEUE, List) ;
  411. NewFiringTime = Queue->DeltaFiringTime ;
  412. Queue->DeltaFiringTime = 0 ;
  413. if (!IsListEmpty (&ReinsertTimerQueueList)) {
  414. // TimerQueues and ReinsertTimerQueueList are both non-empty. Merge them.
  415. RtlpInsertTimersIntoDeltaList (&ReinsertTimerQueueList, &TimerQueues,
  416. NewFiringTime, &NewFiringTime) ;
  417. }
  418. // NewFiringTime contains the time the NT Timer should be programmed to.
  419. } else {
  420. if (!IsListEmpty (&ReinsertTimerQueueList)) {
  421. // TimerQueues is empty. ReinsertTimerQueueList is not.
  422. RtlpInsertTimersIntoDeltaList (&ReinsertTimerQueueList, &TimerQueues, 0,
  423. &NewFiringTime) ;
  424. } else {
  425. NewFiringTime = INFINITE_TIME ;
  426. }
  427. // NewFiringTime contains the time the NT Timer should be programmed to.
  428. }
  429. // Reset the timer to reflect the Delta time associated with the first Queue
  430. RtlpResetTimer (TimerHandle, NewFiringTime, NULL) ;
  431. #if DBG
  432. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  433. RTLP_THREADPOOL_VERBOSE_MASK,
  434. "After service timer:ThreadId<%x:%x>\n",
  435. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  436. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  437. RtlDebugPrintTimes ();
  438. #endif
  439. // finally fire all the timers
  440. RtlpFireTimers( &TimersToFireList ) ;
  441. RELEASE_GLOBAL_TIMER_LOCK();
  442. }
  443. VOID
  444. RtlpResetTimer (
  445. HANDLE TimerHandle,
  446. ULONG DueTime,
  447. PRTLP_WAIT_THREAD_CONTROL_BLOCK ThreadCB
  448. )
  449. /*++
  450. Routine Description:
  451. This routine resets the timer object with the new due time.
  452. Arguments:
  453. TimerHandle - Handle to the timer object
  454. DueTime - Relative timer due time in Milliseconds
  455. Return Value:
  456. --*/
  457. {
  458. LARGE_INTEGER LongDueTime ;
  459. NtCancelTimer (TimerHandle, NULL) ;
  460. // If the DueTime is INFINITE_TIME then set the timer to the largest integer possible
  461. if (DueTime >= PSEUDO_INFINITE_TIME) {
  462. LongDueTime.LowPart = 0x0 ;
  463. LongDueTime.HighPart = 0x80000000 ;
  464. } else {
  465. //
  466. // set the absolute time when timer is to be fired
  467. //
  468. if (ThreadCB) {
  469. ThreadCB->Firing64BitTickCount = DueTime
  470. + RtlpGet64BitTickCount(&ThreadCB->Current64BitTickCount) ;
  471. } else {
  472. //
  473. // adjust for drift only if it is a global timer
  474. //
  475. ULONG Drift ;
  476. LONGLONG llCurrentTick ;
  477. llCurrentTick = RtlpGet64BitTickCount(&Last64BitTickCount) ;
  478. Drift = (ULONG) (llCurrentTick - RtlpGetResync64BitTickCount()) ;
  479. DueTime = (DueTime > Drift) ? DueTime-Drift : 1 ;
  480. RtlpSetFiring64BitTickCount(llCurrentTick + DueTime) ;
  481. }
  482. LongDueTime.QuadPart = (LONGLONG) UInt32x32To64( DueTime, 10000 );
  483. LongDueTime.QuadPart *= -1;
  484. }
  485. #if DBG
  486. if ((RtlpDueTimeMax != 0) && (DueTime > RtlpDueTimeMax)) {
  487. DbgPrint("\n*** Requested timer due time %d is greater than max allowed (%d)\n",
  488. DueTime,
  489. RtlpDueTimeMax);
  490. DbgBreakPoint();
  491. }
  492. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  493. RTLP_THREADPOOL_TRACE_MASK,
  494. "RtlpResetTimer: %dms => %p'%p in thread:<%x:%x>\n",
  495. DueTime,
  496. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  497. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  498. #endif
  499. NtSetTimer (
  500. TimerHandle,
  501. &LongDueTime,
  502. ThreadCB ? NULL : RtlpServiceTimer,
  503. NULL,
  504. FALSE,
  505. 0,
  506. NULL
  507. ) ;
  508. }
  509. #if _MSC_FULL_VER >= 13008827
  510. #pragma warning(push)
  511. #pragma warning(disable:4715) // Not all control paths return (due to infinite loop)
  512. #endif
  513. LONG
  514. RtlpTimerThread (
  515. PVOID Parameter
  516. )
  517. /*++
  518. Routine Description:
  519. All the timer activity takes place in APCs.
  520. Arguments:
  521. HandlePtr - Pointer to our handle
  522. Return Value:
  523. --*/
  524. {
  525. LARGE_INTEGER TimeOut ;
  526. // no structure initializations should be done here as new timer thread
  527. // may be created after threadPoolCleanup
  528. UNREFERENCED_PARAMETER(Parameter);
  529. #if DBG
  530. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  531. RTLP_THREADPOOL_TRACE_MASK,
  532. "Starting timer thread\n");
  533. #endif
  534. TimerThreadId = HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread) ;
  535. // Reset the NT Timer to never fire initially
  536. RtlpResetTimer (TimerHandle, -1, NULL) ;
  537. // Signal the thread creation path that we're ready to go
  538. NtSetEvent(TimerThreadStartedEvent, NULL);
  539. // Sleep alertably so that all the activity can take place
  540. // in APCs
  541. for ( ; ; ) {
  542. // Set timeout for the largest timeout possible
  543. TimeOut.LowPart = 0 ;
  544. TimeOut.HighPart = 0x80000000 ;
  545. NtDelayExecution (TRUE, &TimeOut) ;
  546. }
  547. return 0 ; // Keep compiler happy
  548. }
  549. #if _MSC_FULL_VER >= 13008827
  550. #pragma warning(pop)
  551. #endif
  552. NTSTATUS
  553. RtlpInitializeTimerThreadPool (
  554. )
  555. /*++
  556. Routine Description:
  557. This routine is used to initialize structures used for Timer Thread
  558. Arguments:
  559. Return Value:
  560. --*/
  561. {
  562. NTSTATUS Status = STATUS_SUCCESS;
  563. LARGE_INTEGER TimeOut ;
  564. PRTLP_EVENT Event;
  565. // In order to avoid an explicit RtlInitialize() function to initialize the wait thread pool
  566. // we use StartedTimerInitialization and CompletedTimerInitialization to provide us the
  567. // necessary synchronization to avoid multiple threads from initializing the thread pool.
  568. // This scheme does not work if RtlInitializeCriticalSection() or NtCreateEvent fails - but in this case the
  569. // caller has not choices left.
  570. if (!InterlockedExchange(&StartedTimerInitialization, 1L)) {
  571. if (CompletedTimerInitialization)
  572. InterlockedExchange(&CompletedTimerInitialization, 0 ) ;
  573. do {
  574. // Initialize global timer lock
  575. Status = RtlInitializeCriticalSection( &TimerCriticalSection ) ;
  576. if (! NT_SUCCESS( Status )) {
  577. break ;
  578. }
  579. Status = NtCreateTimer(
  580. &TimerHandle,
  581. TIMER_ALL_ACCESS,
  582. NULL,
  583. NotificationTimer
  584. ) ;
  585. if (!NT_SUCCESS(Status) ) {
  586. RtlDeleteCriticalSection( &TimerCriticalSection );
  587. break ;
  588. }
  589. InitializeListHead (&TimerQueues) ; // Initialize Timer Queue Structures
  590. // initialize tick count
  591. Resync64BitTickCount.QuadPart = NtGetTickCount() ;
  592. Firing64BitTickCount.QuadPart = 0 ;
  593. Event = RtlpGetWaitEvent();
  594. if (! Event) {
  595. Status = STATUS_NO_MEMORY;
  596. RtlDeleteCriticalSection(&TimerCriticalSection);
  597. NtClose(TimerHandle);
  598. TimerHandle = NULL;
  599. break;
  600. }
  601. TimerThreadStartedEvent = Event->Handle;
  602. Status = RtlpStartThreadpoolThread (RtlpTimerThread,
  603. NULL,
  604. &TimerThreadHandle);
  605. if (!NT_SUCCESS(Status) ) {
  606. RtlpFreeWaitEvent(Event);
  607. RtlDeleteCriticalSection( &TimerCriticalSection );
  608. NtClose(TimerHandle);
  609. TimerHandle = NULL;
  610. break ;
  611. }
  612. Status = NtWaitForSingleObject(TimerThreadStartedEvent,
  613. FALSE,
  614. NULL);
  615. RtlpFreeWaitEvent(Event);
  616. TimerThreadStartedEvent = NULL;
  617. if (! NT_SUCCESS(Status)) {
  618. RtlDeleteCriticalSection( &TimerCriticalSection );
  619. NtClose(TimerHandle);
  620. TimerHandle = NULL;
  621. break ;
  622. }
  623. } while(FALSE ) ;
  624. if (!NT_SUCCESS(Status) ) {
  625. StartedTimerInitialization = 0 ;
  626. InterlockedExchange (&CompletedTimerInitialization, ~0) ;
  627. return Status ;
  628. }
  629. InterlockedExchange (&CompletedTimerInitialization, 1L) ;
  630. } else {
  631. // Sleep 1 ms and see if the other thread has completed initialization
  632. ONE_MILLISECOND_TIMEOUT(TimeOut) ;
  633. while (!*((ULONG volatile *)&CompletedTimerInitialization)) {
  634. NtDelayExecution (FALSE, &TimeOut) ;
  635. }
  636. if (CompletedTimerInitialization != 1)
  637. Status = STATUS_NO_MEMORY ;
  638. }
  639. return NT_SUCCESS(Status) ? STATUS_SUCCESS : Status ;
  640. }
  641. NTSTATUS
  642. RtlCreateTimerQueue(
  643. OUT PHANDLE TimerQueueHandle
  644. )
  645. /*++
  646. Routine Description:
  647. This routine creates a queue that can be used to queue time based tasks.
  648. Arguments:
  649. TimerQueueHandle - Returns back the Handle identifying the timer queue created.
  650. Return Value:
  651. NTSTATUS - Result code from call. The following are returned
  652. STATUS_SUCCESS - Timer Queue created successfully.
  653. STATUS_NO_MEMORY - There was not sufficient heap to perform the
  654. requested operation.
  655. --*/
  656. {
  657. PRTLP_TIMER_QUEUE Queue ;
  658. NTSTATUS Status;
  659. if (LdrpShutdownInProgress) {
  660. return STATUS_UNSUCCESSFUL;
  661. }
  662. // Initialize the timer component if it hasnt been done already
  663. if (CompletedTimerInitialization != 1) {
  664. Status = RtlpInitializeTimerThreadPool () ;
  665. if ( !NT_SUCCESS(Status) )
  666. return Status ;
  667. }
  668. InterlockedIncrement( &NumTimerQueues ) ;
  669. // Allocate a Queue structure
  670. Queue = (PRTLP_TIMER_QUEUE) RtlpAllocateTPHeap (
  671. sizeof (RTLP_TIMER_QUEUE),
  672. HEAP_ZERO_MEMORY
  673. ) ;
  674. if (Queue == NULL) {
  675. InterlockedDecrement( &NumTimerQueues ) ;
  676. return STATUS_NO_MEMORY ;
  677. }
  678. Queue->RefCount = 1 ;
  679. // Initialize the allocated queue
  680. InitializeListHead (&Queue->List) ;
  681. InitializeListHead (&Queue->TimerList) ;
  682. InitializeListHead (&Queue->UncancelledTimerList) ;
  683. SET_TIMER_QUEUE_SIGNATURE( Queue ) ;
  684. Queue->DeltaFiringTime = 0 ;
  685. #if DBG1
  686. Queue->DbgId = ++NextTimerDbgId ;
  687. Queue->ThreadId = HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread) ;
  688. #endif
  689. #if DBG
  690. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  691. RTLP_THREADPOOL_TRACE_MASK,
  692. "<%d:%d> TimerQueue %x created by thread:<%x:%x>\n",
  693. Queue->DbgId, 1, (ULONG_PTR)Queue,
  694. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  695. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess)) ;
  696. #endif
  697. *TimerQueueHandle = Queue ;
  698. return STATUS_SUCCESS ;
  699. }
  700. ULONG
  701. RtlpGetQueueRelativeTime (
  702. PRTLP_TIMER_QUEUE Queue
  703. )
  704. /*++
  705. Routine Description:
  706. Walks the list of queues and returns the relative firing time by adding all the
  707. DeltaFiringTimes for all queues before it.
  708. Arguments:
  709. Queue - Queue for which to find the relative firing time
  710. Return Value:
  711. Time in milliseconds
  712. --*/
  713. {
  714. PLIST_ENTRY Node ;
  715. ULONG RelativeTime ;
  716. PRTLP_TIMER_QUEUE CurrentQueue ;
  717. RelativeTime = 0 ;
  718. // It the Queue is not attached to TimerQueues List because it has no timer
  719. // associated with it simply returns 0 as the relative time. Else run thru
  720. // all queues before it in the list and compute the relative firing time
  721. if (!IsListEmpty (&Queue->List)) {
  722. for (Node = TimerQueues.Flink; Node != &Queue->List; Node=Node->Flink) {
  723. CurrentQueue = CONTAINING_RECORD (Node, RTLP_TIMER_QUEUE, List) ;
  724. RelativeTime += CurrentQueue->DeltaFiringTime ;
  725. }
  726. // Add the queue's delta firing time as well
  727. RelativeTime += Queue->DeltaFiringTime ;
  728. }
  729. return RelativeTime ;
  730. }
  731. VOID
  732. RtlpDeactivateTimer (
  733. PRTLP_TIMER_QUEUE Queue,
  734. PRTLP_TIMER Timer
  735. )
  736. /*++
  737. Routine Description:
  738. This routine executes in an APC and cancels the specified timer if it exists
  739. Arguments:
  740. Timer - Specifies pointer to a timer structure that contains Queue and Timer information
  741. Return Value:
  742. --*/
  743. {
  744. ULONG TimeRemaining, QueueRelTimeRemaining ;
  745. ULONG NewFiringTime ;
  746. // Remove the timer from the appropriate queue
  747. TimeRemaining = RtlpGetTimeRemaining (TimerHandle) ;
  748. QueueRelTimeRemaining = TimeRemaining + RtlpGetQueueRelativeTime (Queue) ;
  749. #if DBG
  750. if ((RtlpDueTimeMax != 0)
  751. && (QueueRelTimeRemaining > RtlpDueTimeMax)) {
  752. DbgPrint("\n*** Queue due time %d is greater than max allowed (%d) in RtlpDeactivateTimer\n",
  753. QueueRelTimeRemaining,
  754. RtlpDueTimeMax);
  755. DbgBreakPoint();
  756. }
  757. #endif
  758. if (RtlpRemoveFromDeltaList (&Queue->TimerList, Timer, QueueRelTimeRemaining, &NewFiringTime)) {
  759. // If we removed the last timer from the queue then we should remove the queue
  760. // from TimerQueues, else we should readjust its position based on the delta time change
  761. if (IsListEmpty (&Queue->TimerList)) {
  762. // Remove the queue from TimerQueues
  763. if (RtlpRemoveFromDeltaList (&TimerQueues, Queue, TimeRemaining, &NewFiringTime)) {
  764. // There is a new element at the head of the queue we need to reset the NT
  765. // timer to fire later
  766. RtlpResetTimer (TimerHandle, NewFiringTime, NULL) ;
  767. }
  768. InitializeListHead (&Queue->List) ;
  769. } else {
  770. // If we remove from the head of the timer delta list we will need to
  771. // make sure the queue delta list is readjusted
  772. if (RtlpReOrderDeltaList (&TimerQueues, Queue, TimeRemaining, &NewFiringTime, NewFiringTime)) {
  773. // There is a new element at the head of the queue we need to reset the NT
  774. // timer to fire later
  775. RtlpResetTimer (TimerHandle, NewFiringTime, NULL) ;
  776. }
  777. }
  778. }
  779. }
  780. VOID
  781. RtlpCancelTimerEx (
  782. PRTLP_TIMER Timer,
  783. BOOLEAN DeletingQueue
  784. )
  785. /*++
  786. Routine Description:
  787. This routine cancels the specified timer.
  788. Arguments:
  789. Timer - Specifies pointer to a timer structure that contains Queue and Timer information
  790. DeletingQueue - FALSE: routine executing in an APC. Delete timer only.
  791. TRUE : routine called by timer queue which is being deleted. So dont
  792. reset the queue's position
  793. Return Value:
  794. --*/
  795. {
  796. PRTLP_TIMER_QUEUE Queue ;
  797. RtlpResync64BitTickCount() ;
  798. CHECK_SIGNATURE( Timer ) ;
  799. #if DBG
  800. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  801. RTLP_THREADPOOL_TRACE_MASK,
  802. "<%d:%d> RtlpCancelTimerEx: Timer: %p Thread<%d:%d>\n",
  803. Timer->Queue->DbgId,
  804. Timer->DbgId,
  805. Timer,
  806. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  807. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  808. #endif
  809. Queue = Timer->Queue ;
  810. if ( Timer->State & STATE_ACTIVE ) {
  811. // if queue is being deleted, then the timer should not be reset
  812. if ( ! DeletingQueue )
  813. RtlpDeactivateTimer( Queue, Timer ) ;
  814. } else {
  815. // remove one shot Inactive timer from Queue->UncancelledTimerList
  816. // called only when the time queue is being deleted
  817. RemoveEntryList( &Timer->List ) ;
  818. }
  819. // Set the State to deleted
  820. RtlInterlockedSetBitsDiscardReturn(&Timer->State,
  821. STATE_DELETE);
  822. // delete timer if refcount == 0
  823. if ( InterlockedDecrement( &Timer->RefCount ) == 0 ) {
  824. RtlpDeleteTimer( Timer ) ;
  825. }
  826. }
  827. VOID
  828. RtlpDeleteTimerQueueComplete (
  829. PRTLP_TIMER_QUEUE Queue
  830. )
  831. /*++
  832. Routine Description:
  833. This routine frees the queue and sets the event.
  834. Arguments:
  835. Queue - queue to delete
  836. Event - Event Handle used for signalling completion of request
  837. Return Value:
  838. --*/
  839. {
  840. #if DBG
  841. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  842. RTLP_THREADPOOL_TRACE_MASK,
  843. "<%d> Queue: %x: deleted\n", Queue->DbgId,
  844. (ULONG_PTR)Queue) ;
  845. #endif
  846. InterlockedDecrement( &NumTimerQueues ) ;
  847. // Notify the thread issuing the cancel that the request is completed
  848. if ( Queue->CompletionEvent )
  849. NtSetEvent (Queue->CompletionEvent, NULL) ;
  850. RtlpFreeTPHeap( Queue ) ;
  851. }
  852. NTSTATUS
  853. RtlpDeleteTimerQueue (
  854. PRTLP_TIMER_QUEUE Queue
  855. )
  856. /*++
  857. Routine Description:
  858. This routine deletes the queue specified in the Request and frees all timers
  859. Arguments:
  860. Queue - queue to delete
  861. Event - Event Handle used for signalling completion of request
  862. Return Value:
  863. --*/
  864. {
  865. ULONG TimeRemaining ;
  866. ULONG NewFiringTime ;
  867. PLIST_ENTRY Node ;
  868. PRTLP_TIMER Timer ;
  869. RtlpResync64BitTickCount() ;
  870. SET_DEL_TIMERQ_SIGNATURE( Queue ) ;
  871. // If there are no timers in the queue then it is not attached to TimerQueues
  872. // In this case simply free the memory and return. Otherwise we have to first
  873. // remove the queue from the TimerQueues List, update the firing time if this
  874. // was the first queue in the list and then walk all the timers and free them
  875. // before freeing the Timer Queue.
  876. if (!IsListEmpty (&Queue->List)) {
  877. TimeRemaining = RtlpGetTimeRemaining (TimerHandle)
  878. + RtlpGetQueueRelativeTime (Queue) ;
  879. #if DBG
  880. if ((RtlpDueTimeMax != 0)
  881. && (TimeRemaining > RtlpDueTimeMax)) {
  882. DbgPrint("\n*** Queue due time %d is greater than max allowed (%d) in RtlpDeleteTimerQueue\n",
  883. TimeRemaining,
  884. RtlpDueTimeMax);
  885. DbgBreakPoint();
  886. }
  887. #endif
  888. if (RtlpRemoveFromDeltaList (&TimerQueues, Queue, TimeRemaining,
  889. &NewFiringTime))
  890. {
  891. // If removed from head of queue list, reset the timer
  892. RtlpResetTimer (TimerHandle, NewFiringTime, NULL) ;
  893. }
  894. // Free all the timers associated with this queue
  895. for (Node = Queue->TimerList.Flink ; Node != &Queue->TimerList ; ) {
  896. Timer = CONTAINING_RECORD (Node, RTLP_TIMER, List) ;
  897. Node = Node->Flink ;
  898. RtlpCancelTimerEx( Timer ,TRUE ) ; // Queue being deleted
  899. }
  900. }
  901. // Free all the uncancelled one shot timers in this queue
  902. for (Node = Queue->UncancelledTimerList.Flink ; Node != &Queue->UncancelledTimerList ; ) {
  903. Timer = CONTAINING_RECORD (Node, RTLP_TIMER, List) ;
  904. Node = Node->Flink ;
  905. RtlpCancelTimerEx( Timer ,TRUE ) ; // Queue being deleted
  906. }
  907. // delete the queue completely if the RefCount is 0
  908. if ( InterlockedDecrement( &Queue->RefCount ) == 0 ) {
  909. RtlpDeleteTimerQueueComplete( Queue ) ;
  910. return STATUS_SUCCESS ;
  911. } else {
  912. return STATUS_PENDING ;
  913. }
  914. }
  915. NTSTATUS
  916. RtlDeleteTimerQueueEx (
  917. HANDLE QueueHandle,
  918. HANDLE Event
  919. )
  920. /*++
  921. Routine Description:
  922. This routine deletes the queue specified in the Request and frees all timers.
  923. This call is blocking or non-blocking depending on the value passed for Event.
  924. Blocking calls cannot be made from ANY Timer callbacks. After this call returns,
  925. no new Callbacks will be fired for any timer associated with the queue.
  926. Arguments:
  927. QueueHandle - queue to delete
  928. Event - Event to wait upon.
  929. (HANDLE)-1: The function creates an event and waits on it.
  930. Event : The caller passes an event. The function marks the queue for deletion,
  931. but does not wait for all callbacks to complete. The event is
  932. signalled after all callbacks have completed.
  933. NULL : The function is non-blocking. The function marks the queue for deletion,
  934. but does not wait for all callbacks to complete.
  935. Return Value:
  936. STATUS_SUCCESS - All timer callbacks have completed.
  937. STATUS_PENDING - Non-Blocking call. Some timer callbacks associated with timers
  938. in this queue may not have completed.
  939. --*/
  940. {
  941. NTSTATUS Status;
  942. LARGE_INTEGER TimeOut ;
  943. PRTLP_EVENT CompletionEvent = NULL ;
  944. PRTLP_TIMER_QUEUE Queue = (PRTLP_TIMER_QUEUE)QueueHandle ;
  945. #if DBG
  946. ULONG QueueDbgId;
  947. #endif
  948. if (LdrpShutdownInProgress) {
  949. return STATUS_SUCCESS;
  950. }
  951. if (!Queue) {
  952. return STATUS_INVALID_PARAMETER_1 ;
  953. }
  954. CHECK_DEL_SIGNATURE( Queue ) ;
  955. SET_DEL_SIGNATURE( Queue ) ;
  956. #if DBG1
  957. Queue->ThreadId2 = HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread) ;
  958. #endif
  959. #if DBG
  960. QueueDbgId = Queue->DbgId;
  961. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  962. RTLP_THREADPOOL_TRACE_MASK,
  963. "<%d:%d> Queue Delete(Queue:%x Event:%x by Thread:<%x:%x>)\n",
  964. QueueDbgId, Queue->RefCount, (ULONG_PTR)Queue, (ULONG_PTR)Event,
  965. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  966. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess)) ;
  967. #endif
  968. if (Event == (HANDLE)-1 ) {
  969. // Get an event from the event cache
  970. CompletionEvent = RtlpGetWaitEvent () ;
  971. if (!CompletionEvent) {
  972. return STATUS_NO_MEMORY ;
  973. }
  974. }
  975. Queue->CompletionEvent = CompletionEvent
  976. ? CompletionEvent->Handle
  977. : Event ;
  978. // once this flag is set, no timer will be fired
  979. ACQUIRE_GLOBAL_TIMER_LOCK();
  980. RtlInterlockedSetBitsDiscardReturn(&Queue->State,
  981. STATE_DONTFIRE);
  982. RELEASE_GLOBAL_TIMER_LOCK();
  983. // queue an APC
  984. Status = NtQueueApcThread(
  985. TimerThreadHandle,
  986. (PPS_APC_ROUTINE)RtlpDeleteTimerQueue,
  987. (PVOID) QueueHandle,
  988. NULL,
  989. NULL
  990. );
  991. if (! NT_SUCCESS(Status)) {
  992. if ( CompletionEvent ) {
  993. RtlpFreeWaitEvent( CompletionEvent ) ;
  994. }
  995. return Status ;
  996. }
  997. if (CompletionEvent) {
  998. // wait for Event to be fired. Return if the thread has been killed.
  999. #if DBG
  1000. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1001. RTLP_THREADPOOL_TRACE_MASK,
  1002. "<%d> Queue %p delete waiting Thread<%d:%d>\n",
  1003. QueueDbgId,
  1004. (ULONG_PTR)Queue,
  1005. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  1006. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess)) ;
  1007. #endif
  1008. Status = RtlpWaitForEvent( CompletionEvent->Handle, TimerThreadHandle ) ;
  1009. #if DBG
  1010. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1011. RTLP_THREADPOOL_TRACE_MASK,
  1012. "<%d> Queue %p delete completed\n",
  1013. QueueDbgId,
  1014. (ULONG_PTR) Queue) ;
  1015. #endif
  1016. RtlpFreeWaitEvent( CompletionEvent ) ;
  1017. return NT_SUCCESS( Status ) ? STATUS_SUCCESS : Status ;
  1018. } else {
  1019. return STATUS_PENDING ;
  1020. }
  1021. }
  1022. NTSTATUS
  1023. RtlDeleteTimerQueue(
  1024. IN HANDLE TimerQueueHandle
  1025. )
  1026. /*++
  1027. Routine Description:
  1028. This routine deletes a previously created queue. This call is non-blocking and
  1029. can be made from Callbacks. Pending callbacks already queued to worker threads
  1030. are not cancelled.
  1031. Arguments:
  1032. TimerQueueHandle - Handle identifying the timer queue created.
  1033. Return Value:
  1034. NTSTATUS - Result code from call.
  1035. STATUS_PENDING - Timer Queue created successfully.
  1036. --*/
  1037. {
  1038. return RtlDeleteTimerQueueEx( TimerQueueHandle, NULL ) ;
  1039. }
  1040. VOID
  1041. RtlpAddTimer (
  1042. PRTLP_TIMER Timer
  1043. )
  1044. /*++
  1045. Routine Description:
  1046. This routine runs as an APC into the Timer thread. It adds a new timer to the
  1047. specified queue.
  1048. Arguments:
  1049. Timer - Pointer to the timer to add
  1050. Return Value:
  1051. --*/
  1052. {
  1053. PRTLP_TIMER_QUEUE Queue = Timer->Queue;
  1054. ULONG TimeRemaining, QueueRelTimeRemaining ;
  1055. ULONG NewFiringTime ;
  1056. RtlpResync64BitTickCount() ;
  1057. // the timer was set to be deleted in a callback function.
  1058. if (Timer->State & STATE_DELETE ) {
  1059. RtlpDeleteTimer( Timer ) ;
  1060. return ;
  1061. }
  1062. // check if timer queue already deleted
  1063. if (IS_DEL_SIGNATURE_SET(Queue)) {
  1064. RtlpDeleteTimer(Timer);
  1065. return;
  1066. }
  1067. #if DBG
  1068. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1069. RTLP_THREADPOOL_TRACE_MASK,
  1070. "<%d:%d> RtlpAddTimer: Timer: %p Delta: %dms Period: %dms Thread<%d:%d>\n",
  1071. Timer->Queue->DbgId,
  1072. Timer->DbgId,
  1073. Timer,
  1074. Timer->DeltaFiringTime,
  1075. Timer->Period,
  1076. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  1077. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  1078. #endif
  1079. // TimeRemaining is the time left in the current timer + the relative time of
  1080. // the queue it is being inserted into
  1081. TimeRemaining = RtlpGetTimeRemaining (TimerHandle) ;
  1082. QueueRelTimeRemaining = TimeRemaining + RtlpGetQueueRelativeTime (Queue) ;
  1083. #if DBG
  1084. if ((RtlpDueTimeMax != 0)
  1085. && (QueueRelTimeRemaining > RtlpDueTimeMax)) {
  1086. DbgPrint("\n*** Queue due time %d is greater than max allowed (%d) in RtlpAddTimer\n",
  1087. QueueRelTimeRemaining,
  1088. RtlpDueTimeMax);
  1089. DbgBreakPoint();
  1090. }
  1091. #endif
  1092. if (RtlpInsertInDeltaList (&Queue->TimerList, Timer, QueueRelTimeRemaining,
  1093. &NewFiringTime))
  1094. {
  1095. // If the Queue is not attached to TimerQueues since it had no timers
  1096. // previously then insert the queue into the TimerQueues list, else just
  1097. // reorder its existing position.
  1098. if (IsListEmpty (&Queue->List)) {
  1099. Queue->DeltaFiringTime = NewFiringTime ;
  1100. if (RtlpInsertInDeltaList (&TimerQueues, Queue, TimeRemaining,
  1101. &NewFiringTime))
  1102. {
  1103. // There is a new element at the head of the queue we need to reset the NT
  1104. // timer to fire sooner.
  1105. RtlpResetTimer (TimerHandle, NewFiringTime, NULL) ;
  1106. }
  1107. } else {
  1108. // If we insert at the head of the timer delta list we will need to
  1109. // make sure the queue delta list is readjusted
  1110. if (RtlpReOrderDeltaList(&TimerQueues, Queue, TimeRemaining, &NewFiringTime, NewFiringTime)){
  1111. // There is a new element at the head of the queue we need to reset the NT
  1112. // timer to fire sooner.
  1113. RtlpResetTimer (TimerHandle, NewFiringTime, NULL) ;
  1114. }
  1115. }
  1116. }
  1117. RtlInterlockedSetBitsDiscardReturn(&Timer->State,
  1118. STATE_REGISTERED | STATE_ACTIVE);
  1119. }
  1120. VOID
  1121. RtlpTimerReleaseWorker(ULONG Flags)
  1122. {
  1123. if (! (Flags & WT_EXECUTEINTIMERTHREAD)) {
  1124. RtlpReleaseWorker(Flags);
  1125. }
  1126. }
  1127. NTSTATUS
  1128. RtlCreateTimer(
  1129. IN HANDLE TimerQueueHandle,
  1130. OUT HANDLE *Handle,
  1131. IN WAITORTIMERCALLBACKFUNC Function,
  1132. IN PVOID Context,
  1133. IN ULONG DueTime,
  1134. IN ULONG Period,
  1135. IN ULONG Flags
  1136. )
  1137. /*++
  1138. Routine Description:
  1139. This routine puts a timer request in the queue identified in by TimerQueueHandle.
  1140. The timer request can be one shot or periodic.
  1141. Arguments:
  1142. TimerQueueHandle - Handle identifying the timer queue in which to insert the timer
  1143. request.
  1144. Handle - Specifies a location to return a handle to this timer request
  1145. Function - Routine that is called when the timer fires
  1146. Context - Opaque pointer passed in as an argument to WorkerProc
  1147. DueTime - Specifies the time in milliseconds after which the timer fires.
  1148. Period - Specifies the period of the timer in milliseconds. This should be 0 for
  1149. one shot requests.
  1150. Flags - Can be one of:
  1151. WT_EXECUTEINTIMERTHREAD - if WorkerProc should be invoked in the wait thread
  1152. it this should only be used for small routines.
  1153. WT_EXECUTELONGFUNCTION - if WorkerProc can possibly block for a long time.
  1154. WT_EXECUTEINIOTHREAD - if WorkerProc should be invoked in IO worker thread
  1155. Return Value:
  1156. NTSTATUS - Result code from call. The following are returned
  1157. STATUS_SUCCESS - Timer Queue created successfully.
  1158. STATUS_NO_MEMORY - There was not sufficient heap to perform the
  1159. requested operation.
  1160. --*/
  1161. {
  1162. NTSTATUS Status;
  1163. PRTLP_TIMER Timer ;
  1164. PRTLP_TIMER_QUEUE Queue = (PRTLP_TIMER_QUEUE) TimerQueueHandle;
  1165. if (LdrpShutdownInProgress) {
  1166. return STATUS_UNSUCCESSFUL;
  1167. }
  1168. if (Flags&0xffff0000) {
  1169. MaxThreads = (Flags & 0xffff0000)>>16;
  1170. }
  1171. // check if timer queue already deleted
  1172. if (IS_DEL_SIGNATURE_SET(Queue)) {
  1173. return STATUS_INVALID_HANDLE;
  1174. }
  1175. if (! (Flags & WT_EXECUTEINTIMERTHREAD)) {
  1176. Status = RtlpAcquireWorker(Flags);
  1177. if (! NT_SUCCESS(Status)) {
  1178. return Status;
  1179. }
  1180. }
  1181. Timer = (PRTLP_TIMER) RtlpAllocateTPHeap (
  1182. sizeof (RTLP_TIMER),
  1183. HEAP_ZERO_MEMORY
  1184. ) ;
  1185. if (Timer == NULL) {
  1186. RtlpTimerReleaseWorker(Flags);
  1187. return STATUS_NO_MEMORY ;
  1188. }
  1189. // Initialize the allocated timer
  1190. if (NtCurrentTeb()->IsImpersonating) {
  1191. Status = NtOpenThreadToken(NtCurrentThread(),
  1192. MAXIMUM_ALLOWED,
  1193. TRUE,
  1194. &Timer->ImpersonationToken);
  1195. if (! NT_SUCCESS(Status)) {
  1196. RtlpFreeTPHeap(Timer);
  1197. RtlpTimerReleaseWorker(Flags);
  1198. return Status;
  1199. }
  1200. } else {
  1201. Timer->ImpersonationToken = NULL;
  1202. }
  1203. Status = RtlpThreadPoolGetActiveActivationContext(&Timer->ActivationContext);
  1204. if (!NT_SUCCESS(Status)) {
  1205. if (Status == STATUS_SXS_THREAD_QUERIES_DISABLED) {
  1206. Timer->ActivationContext = INVALID_ACTIVATION_CONTEXT;
  1207. Status = STATUS_SUCCESS;
  1208. } else {
  1209. if (Timer->ImpersonationToken) {
  1210. NtClose(Timer->ImpersonationToken);
  1211. }
  1212. RtlpFreeTPHeap(Timer);
  1213. RtlpTimerReleaseWorker(Flags);
  1214. return Status;
  1215. }
  1216. }
  1217. Timer->DeltaFiringTime = DueTime ;
  1218. Timer->Queue = Queue;
  1219. Timer->RefCount = 1 ;
  1220. Timer->Flags = Flags ;
  1221. Timer->Function = Function ;
  1222. Timer->Context = Context ;
  1223. //todo:remove below
  1224. Timer->Period = (Period == -1) ? 0 : Period;
  1225. InitializeListHead( &Timer->TimersToFireList ) ;
  1226. InitializeListHead( &Timer->List ) ;
  1227. SET_TIMER_SIGNATURE( Timer ) ;
  1228. #if DBG1
  1229. Timer->DbgId = ++ Timer->Queue->NextDbgId ;
  1230. Timer->ThreadId = HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread) ;
  1231. #endif
  1232. #if DBG
  1233. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1234. RTLP_THREADPOOL_TRACE_MASK,
  1235. "<%d:%d:%d> Timer: created by Thread:<%x:%x>\n",
  1236. Timer->Queue->DbgId, Timer->DbgId, 1,
  1237. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  1238. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess)) ;
  1239. #endif
  1240. // Increment the total number of timers in the queue
  1241. InterlockedIncrement( &((PRTLP_TIMER_QUEUE)TimerQueueHandle)->RefCount ) ;
  1242. // Queue APC to timer thread
  1243. Status = NtQueueApcThread(
  1244. TimerThreadHandle,
  1245. (PPS_APC_ROUTINE)RtlpAddTimer,
  1246. (PVOID)Timer,
  1247. NULL,
  1248. NULL
  1249. ) ;
  1250. if (!NT_SUCCESS (Status)) {
  1251. InterlockedDecrement( &((PRTLP_TIMER_QUEUE)TimerQueueHandle)->RefCount ) ;
  1252. if (Timer->ActivationContext != INVALID_ACTIVATION_CONTEXT)
  1253. RtlReleaseActivationContext (Timer->ActivationContext);
  1254. if (Timer->ImpersonationToken) {
  1255. NtClose(Timer->ImpersonationToken);
  1256. }
  1257. RtlpFreeTPHeap(Timer);
  1258. RtlpTimerReleaseWorker(Flags);
  1259. } else {
  1260. // We successfully queued the APC -- the timer is now valid
  1261. *Handle = Timer ;
  1262. }
  1263. return Status ;
  1264. }
  1265. VOID
  1266. RtlpUpdateTimer (
  1267. PRTLP_TIMER Timer,
  1268. PRTLP_TIMER UpdatedTimer
  1269. )
  1270. /*++
  1271. Routine Description:
  1272. This routine executes in an APC and updates the specified timer if it exists
  1273. Arguments:
  1274. Timer - Timer that is actually updated
  1275. UpdatedTimer - Specifies pointer to a timer structure that contains Queue and
  1276. Timer information
  1277. Return Value:
  1278. --*/
  1279. {
  1280. PRTLP_TIMER_QUEUE Queue ;
  1281. ULONG TimeRemaining, QueueRelTimeRemaining ;
  1282. ULONG NewFiringTime ;
  1283. #if DBG
  1284. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1285. RTLP_THREADPOOL_TRACE_MASK,
  1286. "<%d:%d> RtlpUpdateTimer: Timer: %p Updated: %p Delta: %dms Period: %dms Thread<%d:%d>\n",
  1287. Timer->Queue->DbgId,
  1288. Timer->DbgId,
  1289. Timer,
  1290. UpdatedTimer,
  1291. UpdatedTimer->DeltaFiringTime,
  1292. UpdatedTimer->Period,
  1293. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  1294. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  1295. #endif
  1296. try {
  1297. RtlpResync64BitTickCount( ) ;
  1298. CHECK_SIGNATURE(Timer) ;
  1299. Queue = Timer->Queue ;
  1300. if (IS_DEL_SIGNATURE_SET(Queue)) {
  1301. leave;
  1302. }
  1303. // Update the periodic time on the timer
  1304. Timer->Period = UpdatedTimer->Period ;
  1305. // if timer is not in active state, then dont update it
  1306. if ( ! ( Timer->State & STATE_ACTIVE ) ) {
  1307. leave;
  1308. }
  1309. // Get the time remaining on the NT timer
  1310. TimeRemaining = RtlpGetTimeRemaining (TimerHandle) ;
  1311. QueueRelTimeRemaining = TimeRemaining + RtlpGetQueueRelativeTime (Queue) ;
  1312. #if DBG
  1313. if ((RtlpDueTimeMax != 0)
  1314. && (QueueRelTimeRemaining > RtlpDueTimeMax)) {
  1315. DbgPrint("\n*** Queue due time %d is greater than max allowed (%d) in RtlpUpdateTimer\n",
  1316. QueueRelTimeRemaining,
  1317. RtlpDueTimeMax);
  1318. DbgBreakPoint();
  1319. }
  1320. #endif
  1321. // Update the timer based on the due time
  1322. if (RtlpReOrderDeltaList (&Queue->TimerList, Timer, QueueRelTimeRemaining,
  1323. &NewFiringTime,
  1324. UpdatedTimer->DeltaFiringTime))
  1325. {
  1326. // If this update caused the timer at the head of the queue to change, then reinsert
  1327. // this queue in the list of queues.
  1328. if (RtlpReOrderDeltaList (&TimerQueues, Queue, TimeRemaining, &NewFiringTime, NewFiringTime)) {
  1329. // NT timer needs to be updated since the change caused the queue at the head of
  1330. // the TimerQueues to change.
  1331. RtlpResetTimer (TimerHandle, NewFiringTime, NULL) ;
  1332. }
  1333. }
  1334. } finally {
  1335. RtlpFreeTPHeap( UpdatedTimer ) ;
  1336. }
  1337. }
  1338. NTSTATUS
  1339. RtlUpdateTimer(
  1340. IN HANDLE TimerQueueHandle,
  1341. IN HANDLE Timer,
  1342. IN ULONG DueTime,
  1343. IN ULONG Period
  1344. )
  1345. /*++
  1346. Routine Description:
  1347. This routine updates the timer
  1348. Arguments:
  1349. TimerQueueHandle - Handle identifying the queue in which the timer to be updated exists
  1350. Timer - Specifies a handle to the timer which needs to be updated
  1351. DueTime - Specifies the time in milliseconds after which the timer fires.
  1352. Period - Specifies the period of the timer in milliseconds. This should be
  1353. 0 for one shot requests.
  1354. Return Value:
  1355. NTSTATUS - Result code from call. The following are returned
  1356. STATUS_SUCCESS - Timer updated successfully.
  1357. --*/
  1358. {
  1359. NTSTATUS Status;
  1360. PRTLP_TIMER TmpTimer, ActualTimer=(PRTLP_TIMER)Timer ;
  1361. PRTLP_TIMER_QUEUE Queue = (PRTLP_TIMER_QUEUE) TimerQueueHandle;
  1362. if (LdrpShutdownInProgress) {
  1363. return STATUS_UNSUCCESSFUL;
  1364. }
  1365. if (!TimerQueueHandle) {
  1366. return STATUS_INVALID_PARAMETER_1;
  1367. }
  1368. if (!Timer) {
  1369. return STATUS_INVALID_PARAMETER_2;
  1370. }
  1371. // check if timer queue already deleted
  1372. if (IS_DEL_SIGNATURE_SET(Queue)) {
  1373. return STATUS_INVALID_HANDLE;
  1374. }
  1375. CHECK_DEL_SIGNATURE(ActualTimer) ;
  1376. TmpTimer = (PRTLP_TIMER) RtlpAllocateTPHeap (
  1377. sizeof (RTLP_TIMER),
  1378. 0
  1379. ) ;
  1380. if (TmpTimer == NULL) {
  1381. return STATUS_NO_MEMORY ;
  1382. }
  1383. TmpTimer->DeltaFiringTime = DueTime;
  1384. //todo:remove below
  1385. if (Period==-1) Period = 0;
  1386. TmpTimer->Period = Period ;
  1387. #if DBG1
  1388. ActualTimer->ThreadId2 =
  1389. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread) ;
  1390. #endif
  1391. #if DBG
  1392. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1393. RTLP_THREADPOOL_TRACE_MASK,
  1394. "<%d:%d:%d> Timer: updated by Thread:<%x:%x>\n",
  1395. ((PRTLP_TIMER)Timer)->Queue->DbgId,
  1396. ((PRTLP_TIMER)Timer)->DbgId, ((PRTLP_TIMER)Timer)->RefCount,
  1397. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  1398. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess)) ;
  1399. #endif
  1400. // queue APC to update timer
  1401. Status = NtQueueApcThread (
  1402. TimerThreadHandle,
  1403. (PPS_APC_ROUTINE)RtlpUpdateTimer,
  1404. (PVOID)Timer, //Actual timer
  1405. (PVOID)TmpTimer,
  1406. NULL
  1407. );
  1408. if (!NT_SUCCESS (Status)) {
  1409. RtlpFreeTPHeap(TmpTimer);
  1410. }
  1411. return Status ;
  1412. }
  1413. VOID
  1414. RtlpCancelTimer (
  1415. PRTLP_TIMER Timer
  1416. )
  1417. /*++
  1418. Routine Description:
  1419. This routine executes in an APC and cancels the specified timer if it exists
  1420. Arguments:
  1421. Timer - Specifies pointer to a timer structure that contains Queue and Timer information
  1422. Return Value:
  1423. --*/
  1424. {
  1425. RtlpCancelTimerEx( Timer, FALSE ) ; // queue not being deleted
  1426. }
  1427. NTSTATUS
  1428. RtlDeleteTimer (
  1429. IN HANDLE TimerQueueHandle,
  1430. IN HANDLE TimerToCancel,
  1431. IN HANDLE Event
  1432. )
  1433. /*++
  1434. Routine Description:
  1435. This routine cancels the timer
  1436. Arguments:
  1437. TimerQueueHandle - Handle identifying the queue from which to delete timer
  1438. TimerToCancel - Handle identifying the timer to cancel
  1439. Event - Event to be signalled when the timer is deleted
  1440. (HANDLE)-1: The function creates an event and waits on it.
  1441. Event : The caller passes an event. The function marks the timer for deletion,
  1442. but does not wait for all callbacks to complete. The event is
  1443. signalled after all callbacks have completed.
  1444. NULL : The function is non-blocking. The function marks the timer for deletion,
  1445. but does not wait for all callbacks to complete.
  1446. Return Value:
  1447. NTSTATUS - Result code from call. The following are returned
  1448. STATUS_SUCCESS - Timer cancelled. No pending callbacks.
  1449. STATUS_PENDING - Timer cancelled. Some callbacks still not completed.
  1450. --*/
  1451. {
  1452. NTSTATUS Status;
  1453. PRTLP_EVENT CompletionEvent = NULL ;
  1454. PRTLP_TIMER Timer = (PRTLP_TIMER) TimerToCancel ;
  1455. ULONG TimerRefCount ;
  1456. #if DBG
  1457. ULONG QueueDbgId ;
  1458. #endif
  1459. if (LdrpShutdownInProgress) {
  1460. return STATUS_SUCCESS;
  1461. }
  1462. if (!TimerQueueHandle) {
  1463. return STATUS_INVALID_PARAMETER_1 ;
  1464. }
  1465. if (!TimerToCancel) {
  1466. return STATUS_INVALID_PARAMETER_2 ;
  1467. }
  1468. #if DBG
  1469. QueueDbgId = Timer->Queue->DbgId ;
  1470. #endif
  1471. CHECK_DEL_SIGNATURE( Timer ) ;
  1472. SET_DEL_SIGNATURE( Timer ) ;
  1473. CHECK_DEL_SIGNATURE( (PRTLP_TIMER_QUEUE)TimerQueueHandle ) ;
  1474. if (Event == (HANDLE)-1 ) {
  1475. // Get an event from the event cache
  1476. CompletionEvent = RtlpGetWaitEvent () ;
  1477. if (!CompletionEvent) {
  1478. return STATUS_NO_MEMORY ;
  1479. }
  1480. }
  1481. #if DBG1
  1482. Timer->ThreadId2 = HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread) ;
  1483. #endif
  1484. #if DBG
  1485. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1486. RTLP_THREADPOOL_TRACE_MASK,
  1487. "<%d:%d:%d> Timer: Cancel:(Timer:%x, Event:%x)\n",
  1488. Timer->Queue->DbgId, Timer->DbgId, Timer->RefCount,
  1489. (ULONG_PTR)Timer, (ULONG_PTR)Event) ;
  1490. #endif
  1491. Timer->CompletionEvent = CompletionEvent
  1492. ? CompletionEvent->Handle
  1493. : Event ;
  1494. ACQUIRE_GLOBAL_TIMER_LOCK();
  1495. RtlInterlockedSetBitsDiscardReturn(&Timer->State,
  1496. STATE_DONTFIRE);
  1497. TimerRefCount = Timer->RefCount ;
  1498. RELEASE_GLOBAL_TIMER_LOCK();
  1499. Status = NtQueueApcThread(
  1500. TimerThreadHandle,
  1501. (PPS_APC_ROUTINE)RtlpCancelTimer,
  1502. (PVOID)TimerToCancel,
  1503. NULL,
  1504. NULL
  1505. );
  1506. if (! NT_SUCCESS(Status)) {
  1507. if ( CompletionEvent ) {
  1508. RtlpFreeWaitEvent( CompletionEvent ) ;
  1509. }
  1510. return Status ;
  1511. }
  1512. if ( CompletionEvent ) {
  1513. // wait for the event to be signalled
  1514. #if DBG
  1515. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1516. RTLP_THREADPOOL_TRACE_MASK,
  1517. "<%d> Timer: %x: Cancel waiting Thread<%d:%d>\n",
  1518. QueueDbgId, (ULONG_PTR)Timer,
  1519. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  1520. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess)) ;
  1521. #endif
  1522. Status = RtlpWaitForEvent( CompletionEvent->Handle, TimerThreadHandle ) ;
  1523. #if DBG
  1524. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1525. RTLP_THREADPOOL_TRACE_MASK,
  1526. "<%d> Timer: %x: Cancel waiting done\n", QueueDbgId,
  1527. (ULONG_PTR)Timer) ;
  1528. #endif
  1529. RtlpFreeWaitEvent( CompletionEvent ) ;
  1530. return NT_SUCCESS(Status) ? STATUS_SUCCESS : Status ;
  1531. } else {
  1532. return (TimerRefCount > 1) ? STATUS_PENDING : STATUS_SUCCESS;
  1533. }
  1534. }
  1535. VOID
  1536. RtlpDeleteTimer (
  1537. PRTLP_TIMER Timer
  1538. )
  1539. /*++
  1540. Routine Description:
  1541. This routine executes in worker or timer thread and deletes the timer
  1542. whose RefCount == 0. The function can be called outside timer thread,
  1543. so no structure outside Timer can be touched (no list etc).
  1544. Arguments:
  1545. Timer - Specifies pointer to a timer structure that contains Queue and Timer information
  1546. Return Value:
  1547. --*/
  1548. {
  1549. PRTLP_TIMER_QUEUE Queue = Timer->Queue ;
  1550. HANDLE Event;
  1551. CHECK_SIGNATURE( Timer ) ;
  1552. CLEAR_SIGNATURE( Timer ) ;
  1553. #if DBG
  1554. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1555. RTLP_THREADPOOL_TRACE_MASK,
  1556. "<%d> Timer: %x: deleted\n", Timer->Queue->DbgId,
  1557. (ULONG_PTR)Timer) ;
  1558. #endif
  1559. // safe to call this. Either the timer is in the TimersToFireList and
  1560. // the function is being called in time context or else it is not in the
  1561. // list
  1562. RemoveEntryList( &Timer->TimersToFireList ) ;
  1563. Event = Timer->CompletionEvent;
  1564. // decrement the total number of timers in the queue
  1565. if ( InterlockedDecrement( &Queue->RefCount ) == 0 )
  1566. RtlpDeleteTimerQueueComplete( Queue ) ;
  1567. RtlpTimerReleaseWorker(Timer->Flags);
  1568. if (Timer->ActivationContext != INVALID_ACTIVATION_CONTEXT)
  1569. RtlReleaseActivationContext(Timer->ActivationContext);
  1570. if (Timer->ImpersonationToken) {
  1571. NtClose(Timer->ImpersonationToken);
  1572. }
  1573. RtlpFreeTPHeap( Timer ) ;
  1574. if ( Event ) {
  1575. NtSetEvent( Event, NULL ) ;
  1576. }
  1577. }
  1578. ULONG
  1579. RtlpGetTimeRemaining (
  1580. HANDLE TimerHandle
  1581. )
  1582. /*++
  1583. Routine Description:
  1584. Gets the time remaining on the specified NT timer
  1585. Arguments:
  1586. TimerHandle - Handle to the NT timer
  1587. Return Value:
  1588. Time remaining on the timer
  1589. --*/
  1590. {
  1591. ULONG InfoLen ;
  1592. TIMER_BASIC_INFORMATION Info ;
  1593. NTSTATUS Status ;
  1594. LARGE_INTEGER RemainingTime;
  1595. Status = NtQueryTimer (TimerHandle, TimerBasicInformation, &Info, sizeof(Info), &InfoLen) ;
  1596. if (! NT_SUCCESS(Status)) {
  1597. ASSERTMSG ("NtQueryTimer failed", Status == STATUS_SUCCESS) ;
  1598. return 0;
  1599. }
  1600. #if DBG
  1601. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1602. RTLP_THREADPOOL_TRACE_MASK,
  1603. "RtlpGetTimeRemaining: Read SignalState %d, time %p'%p in thread:<%x:%x>\n",
  1604. Info.TimerState,
  1605. Info.RemainingTime.HighPart,
  1606. Info.RemainingTime.LowPart,
  1607. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread),
  1608. HandleToUlong(NtCurrentTeb()->ClientId.UniqueProcess));
  1609. #endif
  1610. // Due to an executive bug, Info.TimerState and Info.RemainingTime
  1611. // may be out of sync -- it's possible for us to be told that the
  1612. // timer has not fired, but that it will fire very far into the
  1613. // future (because we use ULONGLONGs), when in fact it's *just* fired.
  1614. //
  1615. // So: if the time remaining on the timer is negative, we'll
  1616. // assume that it just fired, and invert it. We'll use this as
  1617. // our signal state, too, instead of trusting the one from the
  1618. // executive.
  1619. if (Info.RemainingTime.QuadPart < 0) {
  1620. // The timer has fired.
  1621. return 0;
  1622. } else {
  1623. // Capture the remaining time.
  1624. RemainingTime = Info.RemainingTime;
  1625. // Translate the remaining time from 100ns units to ms,
  1626. // clamping at PSEUDO_INFINITE_TIME.
  1627. RemainingTime.QuadPart /= (10 * 1000); /* 100ns per ms */
  1628. if (RemainingTime.QuadPart > PSEUDO_INFINITE_TIME) {
  1629. RemainingTime.QuadPart = PSEUDO_INFINITE_TIME;
  1630. }
  1631. ASSERT(RemainingTime.HighPart == 0);
  1632. #if DBG
  1633. if ((RtlpDueTimeMax != 0)
  1634. && ((ULONG) RemainingTime.LowPart > RtlpDueTimeMax)) {
  1635. DbgPrint("\n*** Discovered timer due time %d is greater than max allowed (%d)\n",
  1636. RemainingTime.LowPart,
  1637. RtlpDueTimeMax);
  1638. DbgBreakPoint();
  1639. }
  1640. #endif
  1641. return RemainingTime.LowPart;
  1642. }
  1643. }
  1644. BOOLEAN
  1645. RtlpInsertInDeltaList (
  1646. PLIST_ENTRY DeltaList,
  1647. PRTLP_GENERIC_TIMER NewTimer,
  1648. ULONG TimeRemaining,
  1649. ULONG *NewFiringTime
  1650. )
  1651. /*++
  1652. Routine Description:
  1653. Inserts the timer element in the appropriate place in the delta list.
  1654. Arguments:
  1655. DeltaList - Delta list to insert into
  1656. NewTimer - Timer element to insert into list
  1657. TimeRemaining - This time must be added to the head of the list to get "real"
  1658. relative time.
  1659. NewFiringTime - If the new element was inserted at the head of the list - this
  1660. will contain the new firing time in milliseconds. The caller
  1661. can use this time to re-program the NT timer. This MUST NOT be
  1662. changed if the function returns FALSE.
  1663. Return Value:
  1664. TRUE - If the timer was inserted at head of delta list
  1665. FALSE - otherwise
  1666. --*/
  1667. {
  1668. PLIST_ENTRY Node ;
  1669. PRTLP_GENERIC_TIMER Temp ;
  1670. PRTLP_GENERIC_TIMER Head ;
  1671. if (IsListEmpty (DeltaList)) {
  1672. InsertHeadList (DeltaList, &NewTimer->List) ;
  1673. *NewFiringTime = NewTimer->DeltaFiringTime ;
  1674. NewTimer->DeltaFiringTime = 0 ;
  1675. return TRUE ;
  1676. }
  1677. // Adjust the head of the list to reflect the time remaining on the NT timer
  1678. Head = CONTAINING_RECORD (DeltaList->Flink, RTLP_GENERIC_TIMER, List) ;
  1679. Head->DeltaFiringTime += TimeRemaining ;
  1680. // Find the appropriate location to insert this element in
  1681. for (Node = DeltaList->Flink ; Node != DeltaList ; Node = Node->Flink) {
  1682. Temp = CONTAINING_RECORD (Node, RTLP_GENERIC_TIMER, List) ;
  1683. if (Temp->DeltaFiringTime <= NewTimer->DeltaFiringTime) {
  1684. NewTimer->DeltaFiringTime -= Temp->DeltaFiringTime ;
  1685. } else {
  1686. // found appropriate place to insert this timer
  1687. break ;
  1688. }
  1689. }
  1690. // Either we have found the appopriate node to insert before in terms of deltas.
  1691. // OR we have come to the end of the list. Insert this timer here.
  1692. InsertHeadList (Node->Blink, &NewTimer->List) ;
  1693. // If this isnt the last element in the list - adjust the delta of the
  1694. // next element
  1695. if (Node != DeltaList) {
  1696. Temp->DeltaFiringTime -= NewTimer->DeltaFiringTime ;
  1697. }
  1698. // Check if element was inserted at head of list
  1699. if (DeltaList->Flink == &NewTimer->List) {
  1700. // Set NewFiringTime to the time in milliseconds when the new head of list
  1701. // should be serviced.
  1702. *NewFiringTime = NewTimer->DeltaFiringTime ;
  1703. // This means the timer must be programmed to service this request
  1704. NewTimer->DeltaFiringTime = 0 ;
  1705. return TRUE ;
  1706. } else {
  1707. // No change to the head of the list, set the delta time back
  1708. Head->DeltaFiringTime -= TimeRemaining ;
  1709. return FALSE ;
  1710. }
  1711. }
  1712. BOOLEAN
  1713. RtlpRemoveFromDeltaList (
  1714. PLIST_ENTRY DeltaList,
  1715. PRTLP_GENERIC_TIMER Timer,
  1716. ULONG TimeRemaining,
  1717. ULONG* NewFiringTime
  1718. )
  1719. /*++
  1720. Routine Description:
  1721. Removes the specified timer from the delta list
  1722. Arguments:
  1723. DeltaList - Delta list to insert into
  1724. Timer - Timer element to insert into list
  1725. TimerHandle - Handle of the NT Timer object
  1726. TimeRemaining - This time must be added to the head of the list to get "real"
  1727. relative time.
  1728. Return Value:
  1729. TRUE if the timer was removed from head of timer list
  1730. FALSE otherwise
  1731. --*/
  1732. {
  1733. PLIST_ENTRY Next ;
  1734. PRTLP_GENERIC_TIMER Temp ;
  1735. Next = Timer->List.Flink ;
  1736. RemoveEntryList (&Timer->List) ;
  1737. if (IsListEmpty (DeltaList)) {
  1738. *NewFiringTime = INFINITE_TIME ;
  1739. return TRUE ;
  1740. }
  1741. if (Next == DeltaList) {
  1742. // If we removed the last element in the list nothing to do either
  1743. return FALSE ;
  1744. } else {
  1745. Temp = CONTAINING_RECORD ( Next, RTLP_GENERIC_TIMER, List) ;
  1746. Temp->DeltaFiringTime += Timer->DeltaFiringTime ;
  1747. // Check if element was removed from head of list
  1748. if (DeltaList->Flink == Next) {
  1749. *NewFiringTime = Temp->DeltaFiringTime + TimeRemaining ;
  1750. Temp->DeltaFiringTime = 0 ;
  1751. return TRUE ;
  1752. } else {
  1753. return FALSE ;
  1754. }
  1755. }
  1756. }
  1757. BOOLEAN
  1758. RtlpReOrderDeltaList (
  1759. PLIST_ENTRY DeltaList,
  1760. PRTLP_GENERIC_TIMER Timer,
  1761. ULONG TimeRemaining,
  1762. ULONG *NewFiringTime,
  1763. ULONG ChangedFiringTime
  1764. )
  1765. /*++
  1766. Routine Description:
  1767. Called when a timer in the delta list needs to be re-inserted because the firing time
  1768. has changed.
  1769. Arguments:
  1770. DeltaList - List in which to re-insert
  1771. Timer - Timer for which the firing time has changed
  1772. TimeRemaining - Time before the head of the delta list is fired
  1773. NewFiringTime - If the new element was inserted at the head of the list - this
  1774. will contain the new firing time in milliseconds. The caller
  1775. can use this time to re-program the NT timer.
  1776. ChangedFiringTime - Changed Time for the specified timer.
  1777. Return Value:
  1778. TRUE if the timer was removed from head of timer list
  1779. FALSE otherwise
  1780. --*/
  1781. {
  1782. ULONG NewTimeRemaining ;
  1783. PRTLP_GENERIC_TIMER Temp ;
  1784. // Remove the timer from the list
  1785. if (RtlpRemoveFromDeltaList (DeltaList, Timer, TimeRemaining, NewFiringTime)) {
  1786. // If element was removed from the head of the list we should record that
  1787. NewTimeRemaining = *NewFiringTime ;
  1788. } else {
  1789. // Element was not removed from head of delta list, the current TimeRemaining is valid
  1790. NewTimeRemaining = TimeRemaining ;
  1791. }
  1792. // Before inserting Timer, set its delta time to the ChangedFiringTime
  1793. Timer->DeltaFiringTime = ChangedFiringTime ;
  1794. // Reinsert this element back in the list
  1795. if (!RtlpInsertInDeltaList (DeltaList, Timer, NewTimeRemaining, NewFiringTime)) {
  1796. // If we did not add at the head of the list, then we should return TRUE if
  1797. // RtlpRemoveFromDeltaList() had returned TRUE. We also update the NewFiringTime to
  1798. // the reflect the new firing time returned by RtlpRemoveFromDeltaList()
  1799. *NewFiringTime = NewTimeRemaining ;
  1800. return (NewTimeRemaining != TimeRemaining) ;
  1801. } else {
  1802. // NewFiringTime contains the time the NT timer must be programmed for
  1803. return TRUE ;
  1804. }
  1805. }
  1806. VOID
  1807. RtlpAddTimerQueue (
  1808. PVOID Queue
  1809. )
  1810. /*++
  1811. Routine Description:
  1812. This routine runs as an APC into the Timer thread. It does whatever necessary to
  1813. create a new timer queue
  1814. Arguments:
  1815. Queue - Pointer to the queue to add
  1816. Return Value:
  1817. --*/
  1818. {
  1819. // We do nothing here. The newly created queue is free floating until a timer is
  1820. // queued onto it.
  1821. }
  1822. VOID
  1823. RtlpProcessTimeouts (
  1824. PRTLP_WAIT_THREAD_CONTROL_BLOCK ThreadCB
  1825. )
  1826. /*++
  1827. Routine Description:
  1828. This routine processes timeouts for the wait thread
  1829. Arguments:
  1830. ThreadCB - The wait thread to add the wait to
  1831. Return Value:
  1832. --*/
  1833. {
  1834. ULONG NewFiringTime, TimeRemaining ;
  1835. LIST_ENTRY TimersToFireList ;
  1836. //
  1837. // check if incorrect timer fired
  1838. //
  1839. if (ThreadCB->Firing64BitTickCount >
  1840. RtlpGet64BitTickCount(&ThreadCB->Current64BitTickCount) + 200 )
  1841. {
  1842. RtlpResetTimer (ThreadCB->TimerHandle,
  1843. RtlpGetTimeRemaining (ThreadCB->TimerHandle),
  1844. ThreadCB) ;
  1845. return ;
  1846. }
  1847. InitializeListHead( &TimersToFireList ) ;
  1848. // Walk thru the timer list and fire all waits with DeltaFiringTime == 0
  1849. RtlpFireTimersAndReorder (&ThreadCB->TimerQueue, &NewFiringTime, &TimersToFireList) ;
  1850. // Reset the NT timer
  1851. RtlpResetTimer (ThreadCB->TimerHandle, NewFiringTime, ThreadCB) ;
  1852. RtlpFireTimers( &TimersToFireList ) ;
  1853. }
  1854. NTSTATUS
  1855. RtlpTimerCleanup(
  1856. VOID
  1857. )
  1858. {
  1859. BOOLEAN Cleanup;
  1860. IS_COMPONENT_INITIALIZED(StartedTimerInitialization,
  1861. CompletedTimerInitialization,
  1862. Cleanup ) ;
  1863. if ( Cleanup ) {
  1864. ACQUIRE_GLOBAL_TIMER_LOCK() ;
  1865. if (NumTimerQueues != 0 ) {
  1866. RELEASE_GLOBAL_TIMER_LOCK() ;
  1867. return STATUS_UNSUCCESSFUL ;
  1868. }
  1869. NtQueueApcThread(
  1870. TimerThreadHandle,
  1871. (PPS_APC_ROUTINE)RtlpThreadCleanup,
  1872. NULL,
  1873. NULL,
  1874. NULL
  1875. );
  1876. NtClose( TimerThreadHandle ) ;
  1877. TimerThreadHandle = NULL ;
  1878. RELEASE_GLOBAL_TIMER_LOCK() ;
  1879. }
  1880. return STATUS_SUCCESS;
  1881. }
  1882. #if DBG
  1883. VOID
  1884. PrintTimerQueue(PLIST_ENTRY QNode, ULONG Delta, ULONG Count
  1885. )
  1886. {
  1887. PLIST_ENTRY Tnode ;
  1888. PRTLP_TIMER Timer ;
  1889. PRTLP_TIMER_QUEUE Queue ;
  1890. Queue = CONTAINING_RECORD (QNode, RTLP_TIMER_QUEUE, List) ;
  1891. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1892. RTLP_THREADPOOL_VERBOSE_MASK,
  1893. "<%1d> Queue: %x FiringTime:%d\n", Count, (ULONG_PTR)Queue,
  1894. Queue->DeltaFiringTime);
  1895. for (Tnode=Queue->TimerList.Flink; Tnode!=&Queue->TimerList;
  1896. Tnode=Tnode->Flink)
  1897. {
  1898. Timer = CONTAINING_RECORD (Tnode, RTLP_TIMER, List) ;
  1899. Delta += Timer->DeltaFiringTime ;
  1900. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1901. RTLP_THREADPOOL_VERBOSE_MASK,
  1902. " Timer: %x Delta:%d Period:%d\n",(ULONG_PTR)Timer,
  1903. Delta, Timer->Period);
  1904. }
  1905. }
  1906. #endif
  1907. VOID
  1908. RtlDebugPrintTimes (
  1909. )
  1910. {
  1911. #if DBG
  1912. PLIST_ENTRY QNode ;
  1913. ULONG Count = 0 ;
  1914. ULONG Delta = RtlpGetTimeRemaining (TimerHandle) ;
  1915. ULONG CurrentThreadId =
  1916. HandleToUlong(NtCurrentTeb()->ClientId.UniqueThread) ;
  1917. RtlpResync64BitTickCount();
  1918. if (CompletedTimerInitialization != 1) {
  1919. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1920. RTLP_THREADPOOL_ERROR_MASK,
  1921. "RtlTimerThread not yet initialized\n");
  1922. return ;
  1923. }
  1924. if (CurrentThreadId == TimerThreadId)
  1925. {
  1926. PRTLP_TIMER_QUEUE Queue ;
  1927. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1928. RTLP_THREADPOOL_VERBOSE_MASK,
  1929. "================Printing timerqueues====================\n");
  1930. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1931. RTLP_THREADPOOL_VERBOSE_MASK,
  1932. "TimeRemaining: %d\n", Delta);
  1933. for (QNode = TimerQueues.Flink; QNode != &TimerQueues;
  1934. QNode = QNode->Flink)
  1935. {
  1936. Queue = CONTAINING_RECORD (QNode, RTLP_TIMER_QUEUE, List) ;
  1937. Delta += Queue->DeltaFiringTime ;
  1938. PrintTimerQueue(QNode, Delta, ++Count);
  1939. }
  1940. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1941. RTLP_THREADPOOL_VERBOSE_MASK,
  1942. "================Printed ================================\n");
  1943. }
  1944. else
  1945. {
  1946. NtQueueApcThread(
  1947. TimerThreadHandle,
  1948. (PPS_APC_ROUTINE)RtlDebugPrintTimes,
  1949. NULL,
  1950. NULL,
  1951. NULL
  1952. );
  1953. }
  1954. #endif
  1955. return;
  1956. }
  1957. /*DO NOT USE THIS FUNCTION: REPLACED BY RTLCREATETIMER*/
  1958. NTSTATUS
  1959. RtlSetTimer(
  1960. IN HANDLE TimerQueueHandle,
  1961. OUT HANDLE *Handle,
  1962. IN WAITORTIMERCALLBACKFUNC Function,
  1963. IN PVOID Context,
  1964. IN ULONG DueTime,
  1965. IN ULONG Period,
  1966. IN ULONG Flags
  1967. )
  1968. {
  1969. #if DBG
  1970. static ULONG Count = 0;
  1971. if (Count++ ==0 ) {
  1972. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1973. RTLP_THREADPOOL_ERROR_MASK,
  1974. "Using obsolete function call: RtlSetTimer\n");
  1975. DbgBreakPoint();
  1976. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  1977. RTLP_THREADPOOL_ERROR_MASK,
  1978. "Using obsolete function call: RtlSetTimer\n");
  1979. }
  1980. #endif
  1981. return RtlCreateTimer(TimerQueueHandle,
  1982. Handle,
  1983. Function,
  1984. Context,
  1985. DueTime,
  1986. Period,
  1987. Flags
  1988. ) ;
  1989. }
  1990. /*DO NOT USE THIS FUNCTION: REPLACED BY RTLDeleteTimer*/
  1991. NTSTATUS
  1992. RtlCancelTimer(
  1993. IN HANDLE TimerQueueHandle,
  1994. IN HANDLE TimerToCancel
  1995. )
  1996. /*++
  1997. Routine Description:
  1998. This routine cancels the timer. This call is non-blocking. The timer Callback
  1999. will not be executed after this call returns.
  2000. Arguments:
  2001. TimerQueueHandle - Handle identifying the queue from which to delete timer
  2002. TimerToCancel - Handle identifying the timer to cancel
  2003. Return Value:
  2004. NTSTATUS - Result code from call. The following are returned
  2005. STATUS_SUCCESS - Timer cancelled. All callbacks completed.
  2006. STATUS_PENDING - Timer cancelled. Some callbacks still not completed.
  2007. --*/
  2008. {
  2009. #if DBG
  2010. static ULONG Count = 0;
  2011. if (Count++ ==0) {
  2012. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  2013. RTLP_THREADPOOL_ERROR_MASK,
  2014. "Using obsolete function call: RtlCancelTimer\n");
  2015. DbgBreakPoint();
  2016. DbgPrintEx(DPFLTR_RTLTHREADPOOL_ID,
  2017. RTLP_THREADPOOL_ERROR_MASK,
  2018. "Using obsolete function call: RtlCancelTimer\n");
  2019. }
  2020. #endif
  2021. return RtlDeleteTimer( TimerQueueHandle, TimerToCancel, NULL ) ;
  2022. }