1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * MMSYSTEM time functions
6 * Copyright 1993 Martin Ayotte
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mmtime
);
45 typedef struct tagWINE_TIMERENTRY
{
48 LPTIMECALLBACK lpFunc
; /* can be lots of things */
53 struct tagWINE_TIMERENTRY
* lpNext
;
54 } WINE_TIMERENTRY
, *LPWINE_TIMERENTRY
;
56 static HANDLE TIME_hMMTimer
;
57 static LPWINE_TIMERENTRY TIME_TimersList
;
58 static HANDLE TIME_hKillEvent
;
59 static HANDLE TIME_hWakeEvent
;
60 static BOOL TIME_TimeToDie
= TRUE
;
63 * Some observations on the behavior of winmm on Windows.
64 * First, the call to timeBeginPeriod(xx) can never be used
65 * to raise the timer resolution, only lower it.
67 * Second, a brief survey of a variety of Win 2k and Win X
68 * machines showed that a 'standard' (aka default) timer
69 * resolution was 1 ms (Win9x is documented as being 1). However, one
70 * machine had a standard timer resolution of 10 ms.
72 * Further, if we set our default resolution to 1,
73 * the implementation of timeGetTime becomes GetTickCount(),
74 * and we can optimize the code to reduce overhead.
76 * Additionally, a survey of Event behaviors shows that
77 * if we request a Periodic event every 50 ms, then Windows
78 * makes sure to trigger that event 20 times in the next
79 * second. If delays prevent that from happening on exact
80 * schedule, Windows will trigger the events as close
81 * to the original schedule as is possible, and will eventually
82 * bring the event triggers back onto a schedule that is
83 * consistent with what would have happened if there were
86 * Jeremy White, October 2004
88 #define MMSYSTIME_MININTERVAL (1)
89 #define MMSYSTIME_MAXINTERVAL (65535)
92 static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer
)
94 TRACE("%04lx:CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX dwTriggerTime %ld(delta %ld)\n",
95 GetCurrentThreadId(), lpTimer
->lpFunc
, lpTimer
->wTimerID
, lpTimer
->dwUser
,
96 lpTimer
->dwTriggerTime
, GetTickCount() - lpTimer
->dwTriggerTime
);
98 /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
99 * during interrupt time, is allowed to execute very limited number of API calls (like
100 * PostMessage), and must reside in DLL (therefore uses stack of active application). So I
101 * guess current implementation via SetTimer has to be improved upon.
103 switch (lpTimer
->wFlags
& 0x30) {
104 case TIME_CALLBACK_FUNCTION
:
105 (lpTimer
->lpFunc
)(lpTimer
->wTimerID
, 0, lpTimer
->dwUser
, 0, 0);
107 case TIME_CALLBACK_EVENT_SET
:
108 SetEvent((HANDLE
)lpTimer
->lpFunc
);
110 case TIME_CALLBACK_EVENT_PULSE
:
111 PulseEvent((HANDLE
)lpTimer
->lpFunc
);
114 FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
115 lpTimer
->wFlags
, lpTimer
->lpFunc
);
120 /**************************************************************************
121 * TIME_MMSysTimeCallback
123 static DWORD CALLBACK
TIME_MMSysTimeCallback()
125 static int nSizeLpTimers
;
126 static LPWINE_TIMERENTRY lpTimers
;
128 LPWINE_TIMERENTRY timer
, *ptimer
, *next_ptimer
;
132 DWORD ret_time
= INFINITE
;
136 /* optimize for the most frequent case - no events */
137 if (! TIME_TimersList
)
140 /* since timeSetEvent() and timeKillEvent() can be called
141 * from 16 bit code, there are cases where win16 lock is
142 * locked upon entering timeSetEvent(), and then the mm timer
143 * critical section is locked. This function cannot call the
144 * timer callback with the crit sect locked (because callback
145 * may need to acquire Win16 lock, thus providing a deadlock
147 * To cope with that, we just copy the WINE_TIMERENTRY struct
148 * that need to trigger the callback, and call it without the
149 * mm timer crit sect locked.
150 * the hKillTimeEvent is used to mark the section where we
151 * handle the callbacks so we can do synchronous kills.
152 * EPP 99/07/13, updated 04/01/10
155 cur_time
= GetTickCount();
157 EnterCriticalSection(&WINMM_cs
);
158 for (ptimer
= &TIME_TimersList
; *ptimer
!= NULL
; ) {
160 next_ptimer
= &timer
->lpNext
;
161 if (cur_time
>= timer
->dwTriggerTime
)
164 if (idx
== nSizeLpTimers
) {
166 lpTimers
= (LPWINE_TIMERENTRY
)
167 HeapReAlloc(GetProcessHeap(), 0, lpTimers
,
168 ++nSizeLpTimers
* sizeof(WINE_TIMERENTRY
));
170 lpTimers
= (LPWINE_TIMERENTRY
)
171 HeapAlloc(GetProcessHeap(), 0,
172 ++nSizeLpTimers
* sizeof(WINE_TIMERENTRY
));
174 lpTimers
[idx
++] = *timer
;
178 /* Update the time after we make the copy to preserve
179 the original trigger time */
180 timer
->dwTriggerTime
+= timer
->wDelay
;
182 /* TIME_ONESHOT is defined as 0 */
183 if (!(timer
->wFlags
& TIME_PERIODIC
))
185 /* unlink timer from timers list */
186 *ptimer
= *next_ptimer
;
187 HeapFree(GetProcessHeap(), 0, timer
);
189 /* We don't need to trigger oneshots again */
190 delta_time
= INFINITE
;
194 /* Compute when this event needs this function
195 to be called again */
196 if (timer
->dwTriggerTime
<= cur_time
)
199 delta_time
= timer
->dwTriggerTime
- cur_time
;
203 delta_time
= timer
->dwTriggerTime
- cur_time
;
205 /* Determine when we need to return to this function */
206 ret_time
= min(ret_time
, delta_time
);
208 ptimer
= next_ptimer
;
210 if (TIME_hKillEvent
) ResetEvent(TIME_hKillEvent
);
211 LeaveCriticalSection(&WINMM_cs
);
213 while (idx
> 0) TIME_TriggerCallBack(&lpTimers
[--idx
]);
214 if (TIME_hKillEvent
) SetEvent(TIME_hKillEvent
);
216 /* Finally, adjust the recommended wait time downward
217 by the amount of time the processing routines
219 adjust_time
= GetTickCount() - cur_time
;
220 if (adjust_time
> ret_time
)
223 ret_time
-= adjust_time
;
225 /* We return the amount of time our caller should sleep
226 before needing to check in on us again */
230 /**************************************************************************
231 * TIME_MMSysTimeThread
233 static DWORD CALLBACK
TIME_MMSysTimeThread(LPVOID arg
)
238 TRACE("Starting main winmm thread\n");
240 /* FIXME: As an optimization, we could have
241 this thread die when there are no more requests
242 pending, and then get recreated on the first
243 new event; it's not clear if that would be worth
246 while (! TIME_TimeToDie
)
248 sleep_time
= TIME_MMSysTimeCallback();
253 rc
= WaitForSingleObject(TIME_hWakeEvent
, sleep_time
);
254 if (rc
!= WAIT_TIMEOUT
&& rc
!= WAIT_OBJECT_0
)
256 FIXME("Unexpected error %ld(%ld) in timer thread\n", rc
, GetLastError());
260 TRACE("Exiting main winmm thread\n");
264 /**************************************************************************
267 void TIME_MMTimeStart(void)
269 if (!TIME_hMMTimer
) {
270 TIME_TimersList
= NULL
;
271 TIME_hWakeEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
272 TIME_TimeToDie
= FALSE
;
273 TIME_hMMTimer
= CreateThread(NULL
, 0, TIME_MMSysTimeThread
, NULL
, 0, NULL
);
274 SetThreadPriority(TIME_hMMTimer
, THREAD_PRIORITY_TIME_CRITICAL
);
278 /**************************************************************************
281 void TIME_MMTimeStop(void)
285 TIME_TimeToDie
= TRUE
;
286 SetEvent(TIME_hWakeEvent
);
288 /* FIXME: in the worst case, we're going to wait 65 seconds here :-( */
289 WaitForSingleObject(TIME_hMMTimer
, INFINITE
);
291 CloseHandle(TIME_hMMTimer
);
292 CloseHandle(TIME_hWakeEvent
);
294 TIME_TimersList
= NULL
;
298 /**************************************************************************
299 * timeGetSystemTime [WINMM.@]
301 MMRESULT WINAPI
timeGetSystemTime(LPMMTIME lpTime
, UINT wSize
)
304 if (wSize
>= sizeof(*lpTime
)) {
305 lpTime
->wType
= TIME_MS
;
306 lpTime
->u
.ms
= GetTickCount();
313 /**************************************************************************
314 * TIME_SetEventInternal [internal]
316 WORD
TIME_SetEventInternal(UINT wDelay
, UINT wResol
,
317 LPTIMECALLBACK lpFunc
, DWORD dwUser
, UINT wFlags
)
320 LPWINE_TIMERENTRY lpNewTimer
;
321 LPWINE_TIMERENTRY lpTimer
;
323 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay
, wResol
, lpFunc
, dwUser
, wFlags
);
325 if (wDelay
< MMSYSTIME_MININTERVAL
|| wDelay
> MMSYSTIME_MAXINTERVAL
)
328 lpNewTimer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY
));
329 if (lpNewTimer
== NULL
)
334 lpNewTimer
->wDelay
= wDelay
;
335 lpNewTimer
->dwTriggerTime
= GetTickCount() + wDelay
;
337 /* FIXME - wResol is not respected, although it is not clear
338 that we could change our precision meaningfully */
339 lpNewTimer
->wResol
= wResol
;
340 lpNewTimer
->lpFunc
= lpFunc
;
341 lpNewTimer
->dwUser
= dwUser
;
342 lpNewTimer
->wFlags
= wFlags
;
344 EnterCriticalSection(&WINMM_cs
);
346 if ((wFlags
& TIME_KILL_SYNCHRONOUS
) && !TIME_hKillEvent
)
347 TIME_hKillEvent
= CreateEventW(NULL
, TRUE
, TRUE
, NULL
);
349 for (lpTimer
= TIME_TimersList
; lpTimer
!= NULL
; lpTimer
= lpTimer
->lpNext
) {
350 wNewID
= max(wNewID
, lpTimer
->wTimerID
);
353 lpNewTimer
->lpNext
= TIME_TimersList
;
354 TIME_TimersList
= lpNewTimer
;
355 lpNewTimer
->wTimerID
= wNewID
+ 1;
357 LeaveCriticalSection(&WINMM_cs
);
359 /* Wake the service thread in case there is work to be done */
360 SetEvent(TIME_hWakeEvent
);
362 TRACE("=> %u\n", wNewID
+ 1);
367 /**************************************************************************
368 * timeSetEvent [WINMM.@]
370 MMRESULT WINAPI
timeSetEvent(UINT wDelay
, UINT wResol
, LPTIMECALLBACK lpFunc
,
371 DWORD_PTR dwUser
, UINT wFlags
)
373 return TIME_SetEventInternal(wDelay
, wResol
, lpFunc
,
377 /**************************************************************************
378 * timeKillEvent [WINMM.@]
380 MMRESULT WINAPI
timeKillEvent(UINT wID
)
382 LPWINE_TIMERENTRY lpSelf
= NULL
, *lpTimer
;
384 TRACE("(%u)\n", wID
);
385 EnterCriticalSection(&WINMM_cs
);
386 /* remove WINE_TIMERENTRY from list */
387 for (lpTimer
= &TIME_TimersList
; *lpTimer
; lpTimer
= &(*lpTimer
)->lpNext
) {
388 if (wID
== (*lpTimer
)->wTimerID
) {
390 /* unlink timer of id 'wID' */
391 *lpTimer
= (*lpTimer
)->lpNext
;
395 LeaveCriticalSection(&WINMM_cs
);
399 WARN("wID=%u is not a valid timer ID\n", wID
);
400 return MMSYSERR_INVALPARAM
;
402 if (lpSelf
->wFlags
& TIME_KILL_SYNCHRONOUS
)
403 WaitForSingleObject(TIME_hKillEvent
, INFINITE
);
404 HeapFree(GetProcessHeap(), 0, lpSelf
);
405 return TIMERR_NOERROR
;
408 /**************************************************************************
409 * timeGetDevCaps [WINMM.@]
411 MMRESULT WINAPI
timeGetDevCaps(LPTIMECAPS lpCaps
, UINT wSize
)
413 TRACE("(%p, %u)\n", lpCaps
, wSize
);
416 WARN("invalid lpCaps\n");
417 return TIMERR_NOCANDO
;
420 if (wSize
< sizeof(TIMECAPS
)) {
421 WARN("invalid wSize\n");
422 return TIMERR_NOCANDO
;
425 lpCaps
->wPeriodMin
= MMSYSTIME_MININTERVAL
;
426 lpCaps
->wPeriodMax
= MMSYSTIME_MAXINTERVAL
;
427 return TIMERR_NOERROR
;
430 /**************************************************************************
431 * timeBeginPeriod [WINMM.@]
433 MMRESULT WINAPI
timeBeginPeriod(UINT wPeriod
)
435 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
436 return TIMERR_NOCANDO
;
438 if (wPeriod
> MMSYSTIME_MININTERVAL
)
440 WARN("Stub; we set our timer resolution at minimum\n");
446 /**************************************************************************
447 * timeEndPeriod [WINMM.@]
449 MMRESULT WINAPI
timeEndPeriod(UINT wPeriod
)
451 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
452 return TIMERR_NOCANDO
;
454 if (wPeriod
> MMSYSTIME_MININTERVAL
)
456 WARN("Stub; we set our timer resolution at minimum\n");
461 /**************************************************************************
462 * timeGetTime [MMSYSTEM.607]
463 * timeGetTime [WINMM.@]
465 DWORD WINAPI
timeGetTime(void)
467 #if defined(COMMENTOUTPRIORTODELETING)
470 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
471 * that lets mciavi.drv run correctly
473 if (pFnReleaseThunkLock
) pFnReleaseThunkLock(&count
);
474 if (pFnRestoreThunkLock
) pFnRestoreThunkLock(count
);
477 return GetTickCount();