4 * Copyright 1993 Alexandre Julliard
13 DEFAULT_DEBUG_CHANNEL(timer)
16 typedef struct tagTIMER
20 UINT16 msg; /* WM_TIMER or WM_SYSTIMER */
23 struct tagTIMER *next;
24 DWORD expires; /* Next expiration, or 0 if already expired */
29 #define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
31 static TIMER TimersArray[NB_TIMERS];
33 static TIMER * pNextTimer = NULL; /* Next timer to expire */
35 static CRITICAL_SECTION csTimer;
37 /* Duration from 'time' until expiration of the timer */
38 #define EXPIRE_TIME(pTimer,time) \
39 (((pTimer)->expires <= (time)) ? 0 : (pTimer)->expires - (time))
42 /***********************************************************************
45 * Initialize critical section for the timer.
47 BOOL TIMER_Init( void )
49 InitializeCriticalSection( &csTimer );
50 MakeCriticalSectionGlobal( &csTimer );
56 /***********************************************************************
59 * Insert the timer at its place in the chain.
61 static void TIMER_InsertTimer( TIMER * pTimer )
63 EnterCriticalSection( &csTimer );
65 if (!pNextTimer || (pTimer->expires < pNextTimer->expires))
67 pTimer->next = pNextTimer;
72 TIMER * ptr = pNextTimer;
73 while (ptr->next && (pTimer->expires >= ptr->next->expires))
75 pTimer->next = ptr->next;
79 LeaveCriticalSection( &csTimer );
83 /***********************************************************************
86 * Remove the timer from the chain.
88 static void TIMER_RemoveTimer( TIMER * pTimer )
90 TIMER **ppTimer = &pNextTimer;
92 EnterCriticalSection( &csTimer );
94 while (*ppTimer && (*ppTimer != pTimer)) ppTimer = &(*ppTimer)->next;
95 if (*ppTimer) *ppTimer = pTimer->next;
98 LeaveCriticalSection( &csTimer );
100 if (!pTimer->expires) QUEUE_DecTimerCount( pTimer->hq );
104 /***********************************************************************
107 * Clear and remove a timer.
109 static void TIMER_ClearTimer( TIMER * pTimer )
111 TIMER_RemoveTimer( pTimer );
116 WINPROC_FreeProc( pTimer->proc, WIN_PROC_TIMER );
120 /***********************************************************************
123 void TIMER_SwitchQueue( HQUEUE16 old, HQUEUE16 new )
127 EnterCriticalSection( &csTimer );
132 if (pT->hq == old) pT->hq = new;
136 LeaveCriticalSection( &csTimer );
140 /***********************************************************************
141 * TIMER_RemoveWindowTimers
143 * Remove all timers for a given window.
145 void TIMER_RemoveWindowTimers( HWND hwnd )
150 EnterCriticalSection( &csTimer );
152 for (i = NB_TIMERS, pTimer = TimersArray; i > 0; i--, pTimer++)
153 if ((pTimer->hwnd == hwnd) && pTimer->timeout)
154 TIMER_ClearTimer( pTimer );
156 LeaveCriticalSection( &csTimer );
160 /***********************************************************************
161 * TIMER_RemoveQueueTimers
163 * Remove all timers for a given queue.
165 void TIMER_RemoveQueueTimers( HQUEUE16 hqueue )
170 EnterCriticalSection( &csTimer );
172 for (i = NB_TIMERS, pTimer = TimersArray; i > 0; i--, pTimer++)
173 if ((pTimer->hq == hqueue) && pTimer->timeout)
174 TIMER_ClearTimer( pTimer );
176 LeaveCriticalSection( &csTimer );
180 /***********************************************************************
181 * TIMER_RestartTimers
183 * Restart an expired timer.
185 static void TIMER_RestartTimer( TIMER * pTimer, DWORD curTime )
187 TIMER_RemoveTimer( pTimer );
188 pTimer->expires = curTime + pTimer->timeout;
189 TIMER_InsertTimer( pTimer );
193 /***********************************************************************
194 * TIMER_GetNextExpiration
196 * Return next timer expiration time, or -1 if none.
198 LONG TIMER_GetNextExpiration(void)
203 EnterCriticalSection( &csTimer );
207 while (pTimer && !pTimer->expires) /* Skip already expired timers */
208 pTimer = pTimer->next;
210 retValue = pTimer ? EXPIRE_TIME( pTimer, GetTickCount() ) : -1;
211 LeaveCriticalSection( &csTimer );
217 /***********************************************************************
220 * Mark expired timers and wake the appropriate queues.
222 void TIMER_ExpireTimers(void)
225 DWORD curTime = GetTickCount();
227 EnterCriticalSection( &csTimer );
231 while (pTimer && !pTimer->expires) /* Skip already expired timers */
232 pTimer = pTimer->next;
233 while (pTimer && (pTimer->expires <= curTime))
236 QUEUE_IncTimerCount( pTimer->hq );
237 pTimer = pTimer->next;
240 LeaveCriticalSection( &csTimer );
244 /***********************************************************************
247 * Build a message for an expired timer.
249 BOOL TIMER_GetTimerMsg( MSG *msg, HWND hwnd,
250 HQUEUE16 hQueue, BOOL remove )
253 DWORD curTime = GetTickCount();
255 EnterCriticalSection( &csTimer );
259 if (hwnd) /* Find first timer for this window */
260 while (pTimer && (pTimer->hwnd != hwnd)) pTimer = pTimer->next;
261 else /* Find first timer for this queue */
262 while (pTimer && (pTimer->hq != hQueue)) pTimer = pTimer->next;
264 if (!pTimer || (pTimer->expires > curTime))
266 LeaveCriticalSection( &csTimer );
267 return FALSE; /* No timer */
270 if (remove) TIMER_RestartTimer( pTimer, curTime ); /* Restart it */
272 TRACE(timer, "Timer expired: %04x, %04x, %04x, %08lx\n",
273 pTimer->hwnd, pTimer->msg, pTimer->id, (DWORD)pTimer->proc);
275 /* Build the message */
276 msg->hwnd = pTimer->hwnd;
277 msg->message = pTimer->msg;
278 msg->wParam = pTimer->id;
279 msg->lParam = (LONG)pTimer->proc;
282 LeaveCriticalSection( &csTimer );
288 /***********************************************************************
291 static UINT TIMER_SetTimer( HWND hwnd, UINT id, UINT timeout,
292 WNDPROC16 proc, WINDOWPROCTYPE type, BOOL sys )
297 if (!timeout) return 0;
299 EnterCriticalSection( &csTimer );
301 /* Check if there's already a timer with the same hwnd and id */
303 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
304 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
305 (pTimer->timeout != 0))
307 /* Got one: set new values and return */
308 TIMER_RemoveTimer( pTimer );
309 pTimer->timeout = timeout;
310 WINPROC_FreeProc( pTimer->proc, WIN_PROC_TIMER );
311 pTimer->proc = (HWINDOWPROC)0;
312 if (proc) WINPROC_SetProc( &pTimer->proc, proc,
313 type, WIN_PROC_TIMER );
314 pTimer->expires = GetTickCount() + timeout;
315 TIMER_InsertTimer( pTimer );
316 LeaveCriticalSection( &csTimer );
320 /* Find a free timer */
322 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
323 if (!pTimer->timeout) break;
325 if ( (i >= NB_TIMERS) ||
326 (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) )
328 LeaveCriticalSection( &csTimer );
332 if (!hwnd) id = i + 1;
337 pTimer->hq = (hwnd) ? GetThreadQueue16( GetWindowThreadProcessId( hwnd, NULL ) )
339 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
341 pTimer->timeout = timeout;
342 pTimer->expires = GetTickCount() + timeout;
343 pTimer->proc = (HWINDOWPROC)0;
344 if (proc) WINPROC_SetProc( &pTimer->proc, proc, type, WIN_PROC_TIMER );
345 TRACE(timer, "Timer added: %p, %04x, %04x, %04x, %08lx\n",
346 pTimer, pTimer->hwnd, pTimer->msg, pTimer->id,
347 (DWORD)pTimer->proc );
348 TIMER_InsertTimer( pTimer );
350 LeaveCriticalSection( &csTimer );
352 if (!id) return TRUE;
357 /***********************************************************************
360 static BOOL TIMER_KillTimer( HWND hwnd, UINT id, BOOL sys )
365 EnterCriticalSection( &csTimer );
369 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
370 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
371 (pTimer->timeout != 0)) break;
373 if ( (i >= NB_TIMERS) ||
374 (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) ||
375 (!sys && (pTimer->msg != WM_TIMER)) ||
376 (sys && (pTimer->msg != WM_SYSTIMER)) )
378 LeaveCriticalSection( &csTimer );
382 /* Delete the timer */
384 TIMER_ClearTimer( pTimer );
386 LeaveCriticalSection( &csTimer );
392 /***********************************************************************
393 * SetTimer16 (USER.10)
395 UINT16 WINAPI SetTimer16( HWND16 hwnd, UINT16 id, UINT16 timeout,
398 TRACE(timer, "%04x %d %d %08lx\n",
399 hwnd, id, timeout, (LONG)proc );
400 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
401 WIN_PROC_16, FALSE );
405 /***********************************************************************
406 * SetTimer32 (USER32.511)
408 UINT WINAPI SetTimer( HWND hwnd, UINT id, UINT timeout,
411 TRACE(timer, "%04x %d %d %08lx\n",
412 hwnd, id, timeout, (LONG)proc );
413 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
414 WIN_PROC_32A, FALSE );
418 /***********************************************************************
419 * SetSystemTimer16 (USER.11)
421 UINT16 WINAPI SetSystemTimer16( HWND16 hwnd, UINT16 id, UINT16 timeout,
424 TRACE(timer, "%04x %d %d %08lx\n",
425 hwnd, id, timeout, (LONG)proc );
426 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
431 /***********************************************************************
432 * SetSystemTimer32 (USER32.509)
434 UINT WINAPI SetSystemTimer( HWND hwnd, UINT id, UINT timeout,
437 TRACE(timer, "%04x %d %d %08lx\n",
438 hwnd, id, timeout, (LONG)proc );
439 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
440 WIN_PROC_32A, TRUE );
444 /***********************************************************************
445 * KillTimer16 (USER.12)
447 BOOL16 WINAPI KillTimer16( HWND16 hwnd, UINT16 id )
449 TRACE(timer, "%04x %d\n", hwnd, id );
450 return TIMER_KillTimer( hwnd, id, FALSE );
454 /***********************************************************************
455 * KillTimer32 (USER32.354)
457 BOOL WINAPI KillTimer( HWND hwnd, UINT id )
459 TRACE(timer, "%04x %d\n", hwnd, id );
460 return TIMER_KillTimer( hwnd, id, FALSE );
464 /***********************************************************************
465 * KillSystemTimer16 (USER.182)
467 BOOL16 WINAPI KillSystemTimer16( HWND16 hwnd, UINT16 id )
469 TRACE(timer, "%04x %d\n", hwnd, id );
470 return TIMER_KillTimer( hwnd, id, TRUE );
474 /***********************************************************************
475 * KillSystemTimer32 (USER32.353)
477 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT id )
479 TRACE(timer, "%04x %d\n", hwnd, id );
480 return TIMER_KillTimer( hwnd, id, TRUE );