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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/list.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mmtime
);
38 typedef struct tagWINE_TIMERENTRY
{
42 LPTIMECALLBACK lpFunc
; /* can be lots of things */
47 } WINE_TIMERENTRY
, *LPWINE_TIMERENTRY
;
49 static struct list timer_list
= LIST_INIT(timer_list
);
51 static CRITICAL_SECTION TIME_cbcrst
;
52 static CRITICAL_SECTION_DEBUG critsect_debug
=
55 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
56 0, 0, { (DWORD_PTR
)(__FILE__
": TIME_cbcrst") }
58 static CRITICAL_SECTION TIME_cbcrst
= { &critsect_debug
, -1, 0, 0, 0, 0 };
60 static HANDLE TIME_hMMTimer
;
61 static BOOL TIME_TimeToDie
= TRUE
;
62 static CONDITION_VARIABLE TIME_cv
;
64 /* link timer at the appropriate spot in the list */
65 static inline void link_timer( WINE_TIMERENTRY
*timer
)
67 WINE_TIMERENTRY
*next
;
69 LIST_FOR_EACH_ENTRY( next
, &timer_list
, WINE_TIMERENTRY
, entry
)
70 if ((int)(next
->dwTriggerTime
- timer
->dwTriggerTime
) >= 0) break;
72 list_add_before( &next
->entry
, &timer
->entry
);
76 * Some observations on the behavior of winmm on Windows.
77 * First, the call to timeBeginPeriod(xx) can never be used
78 * to raise the timer resolution, only lower it.
80 * Second, a brief survey of a variety of Win 2k and Win X
81 * machines showed that a 'standard' (aka default) timer
82 * resolution was 1 ms (Win9x is documented as being 1). However, one
83 * machine had a standard timer resolution of 10 ms.
85 * Further, if we set our default resolution to 1,
86 * the implementation of timeGetTime becomes GetTickCount(),
87 * and we can optimize the code to reduce overhead.
89 * Additionally, a survey of Event behaviors shows that
90 * if we request a Periodic event every 50 ms, then Windows
91 * makes sure to trigger that event 20 times in the next
92 * second. If delays prevent that from happening on exact
93 * schedule, Windows will trigger the events as close
94 * to the original schedule as is possible, and will eventually
95 * bring the event triggers back onto a schedule that is
96 * consistent with what would have happened if there were
99 * Jeremy White, October 2004
101 #define MMSYSTIME_MININTERVAL (1)
102 #define MMSYSTIME_MAXINTERVAL (65535)
104 /**************************************************************************
105 * TIME_MMSysTimeCallback
107 static int TIME_MMSysTimeCallback(void)
109 WINE_TIMERENTRY
*timer
, *to_free
;
112 /* since timeSetEvent() and timeKillEvent() can be called
113 * from 16 bit code, there are cases where win16 lock is
114 * locked upon entering timeSetEvent(), and then the mm timer
115 * critical section is locked. This function cannot call the
116 * timer callback with the crit sect locked (because callback
117 * may need to acquire Win16 lock, thus providing a deadlock
119 * To cope with that, we just copy the WINE_TIMERENTRY struct
120 * that need to trigger the callback, and call it without the
121 * mm timer crit sect locked.
126 struct list
*ptr
= list_head( &timer_list
);
133 timer
= LIST_ENTRY( ptr
, WINE_TIMERENTRY
, entry
);
134 delta_time
= timer
->dwTriggerTime
- GetTickCount();
135 if (delta_time
> 0) break;
137 list_remove( &timer
->entry
);
138 if (timer
->wFlags
& TIME_PERIODIC
)
140 timer
->dwTriggerTime
+= timer
->wDelay
;
141 link_timer( timer
); /* restart it */
144 else to_free
= timer
;
146 switch(timer
->wFlags
& (TIME_CALLBACK_EVENT_SET
|TIME_CALLBACK_EVENT_PULSE
))
148 case TIME_CALLBACK_EVENT_SET
:
149 SetEvent(timer
->lpFunc
);
151 case TIME_CALLBACK_EVENT_PULSE
:
152 PulseEvent(timer
->lpFunc
);
154 case TIME_CALLBACK_FUNCTION
:
156 DWORD_PTR user
= timer
->dwUser
;
157 UINT16 id
= timer
->wTimerID
;
158 UINT16 flags
= timer
->wFlags
;
159 LPTIMECALLBACK func
= timer
->lpFunc
;
161 if (flags
& TIME_KILL_SYNCHRONOUS
) EnterCriticalSection(&TIME_cbcrst
);
162 LeaveCriticalSection(&WINMM_cs
);
164 func(id
, 0, user
, 0, 0);
166 EnterCriticalSection(&WINMM_cs
);
167 if (flags
& TIME_KILL_SYNCHRONOUS
) LeaveCriticalSection(&TIME_cbcrst
);
171 HeapFree( GetProcessHeap(), 0, to_free
);
176 /**************************************************************************
177 * TIME_MMSysTimeThread
179 static DWORD CALLBACK
TIME_MMSysTimeThread(LPVOID arg
)
184 TRACE("Starting main winmm thread\n");
186 EnterCriticalSection(&WINMM_cs
);
187 while (! TIME_TimeToDie
)
189 sleep_time
= TIME_MMSysTimeCallback();
196 ret
= SleepConditionVariableCS(&TIME_cv
, &WINMM_cs
, sleep_time
);
197 if (!ret
&& GetLastError() != ERROR_TIMEOUT
)
199 ERR("Unexpected error in poll: %s(%d)\n", strerror(errno
), errno
);
203 CloseHandle(TIME_hMMTimer
);
204 TIME_hMMTimer
= NULL
;
205 LeaveCriticalSection(&WINMM_cs
);
206 TRACE("Exiting main winmm thread\n");
207 FreeLibraryAndExitThread(arg
, 0);
211 /**************************************************************************
214 static void TIME_MMTimeStart(void)
218 if (TIME_hMMTimer
) return;
220 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)TIME_MMSysTimeThread
, &mod
);
221 TIME_hMMTimer
= CreateThread(NULL
, 0, TIME_MMSysTimeThread
, mod
, 0, NULL
);
222 SetThreadPriority(TIME_hMMTimer
, THREAD_PRIORITY_TIME_CRITICAL
);
225 /**************************************************************************
228 void TIME_MMTimeStop(void)
231 EnterCriticalSection(&WINMM_cs
);
233 ERR("Timer still active?!\n");
234 CloseHandle(TIME_hMMTimer
);
236 DeleteCriticalSection(&TIME_cbcrst
);
240 /**************************************************************************
241 * timeGetSystemTime [WINMM.@]
243 MMRESULT WINAPI
timeGetSystemTime(LPMMTIME lpTime
, UINT wSize
)
246 if (wSize
>= sizeof(*lpTime
)) {
247 lpTime
->wType
= TIME_MS
;
248 lpTime
->u
.ms
= GetTickCount();
255 /**************************************************************************
256 * timeSetEvent [WINMM.@]
258 MMRESULT WINAPI
timeSetEvent(UINT wDelay
, UINT wResol
, LPTIMECALLBACK lpFunc
,
259 DWORD_PTR dwUser
, UINT wFlags
)
262 LPWINE_TIMERENTRY lpNewTimer
;
263 LPWINE_TIMERENTRY lpTimer
;
265 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay
, wResol
, lpFunc
, dwUser
, wFlags
);
267 if (wDelay
< MMSYSTIME_MININTERVAL
|| wDelay
> MMSYSTIME_MAXINTERVAL
)
270 lpNewTimer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY
));
271 if (lpNewTimer
== NULL
)
274 lpNewTimer
->wDelay
= wDelay
;
275 lpNewTimer
->dwTriggerTime
= GetTickCount() + wDelay
;
277 /* FIXME - wResol is not respected, although it is not clear
278 that we could change our precision meaningfully */
279 lpNewTimer
->wResol
= wResol
;
280 lpNewTimer
->lpFunc
= lpFunc
;
281 lpNewTimer
->dwUser
= dwUser
;
282 lpNewTimer
->wFlags
= wFlags
;
284 EnterCriticalSection(&WINMM_cs
);
286 LIST_FOR_EACH_ENTRY( lpTimer
, &timer_list
, WINE_TIMERENTRY
, entry
)
287 wNewID
= max(wNewID
, lpTimer
->wTimerID
);
289 link_timer( lpNewTimer
);
290 lpNewTimer
->wTimerID
= wNewID
+ 1;
294 LeaveCriticalSection(&WINMM_cs
);
296 /* Wake the service thread in case there is work to be done */
297 WakeConditionVariable(&TIME_cv
);
299 TRACE("=> %u\n", wNewID
+ 1);
304 /**************************************************************************
305 * timeKillEvent [WINMM.@]
307 MMRESULT WINAPI
timeKillEvent(UINT wID
)
309 WINE_TIMERENTRY
*lpSelf
= NULL
, *lpTimer
;
312 TRACE("(%u)\n", wID
);
313 EnterCriticalSection(&WINMM_cs
);
314 /* remove WINE_TIMERENTRY from list */
315 LIST_FOR_EACH_ENTRY( lpTimer
, &timer_list
, WINE_TIMERENTRY
, entry
)
317 if (wID
== lpTimer
->wTimerID
) {
319 list_remove( &lpTimer
->entry
);
323 if (list_empty(&timer_list
)) {
325 WakeConditionVariable(&TIME_cv
);
327 LeaveCriticalSection(&WINMM_cs
);
331 WARN("wID=%u is not a valid timer ID\n", wID
);
332 return MMSYSERR_INVALPARAM
;
334 wFlags
= lpSelf
->wFlags
;
335 if (wFlags
& TIME_KILL_SYNCHRONOUS
)
336 EnterCriticalSection(&TIME_cbcrst
);
337 HeapFree(GetProcessHeap(), 0, lpSelf
);
338 if (wFlags
& TIME_KILL_SYNCHRONOUS
)
339 LeaveCriticalSection(&TIME_cbcrst
);
340 return TIMERR_NOERROR
;
343 /**************************************************************************
344 * timeGetDevCaps [WINMM.@]
346 MMRESULT WINAPI
timeGetDevCaps(LPTIMECAPS lpCaps
, UINT wSize
)
348 TRACE("(%p, %u)\n", lpCaps
, wSize
);
351 WARN("invalid lpCaps\n");
352 return TIMERR_NOCANDO
;
355 if (wSize
< sizeof(TIMECAPS
)) {
356 WARN("invalid wSize\n");
357 return TIMERR_NOCANDO
;
360 lpCaps
->wPeriodMin
= MMSYSTIME_MININTERVAL
;
361 lpCaps
->wPeriodMax
= MMSYSTIME_MAXINTERVAL
;
362 return TIMERR_NOERROR
;
365 /**************************************************************************
366 * timeBeginPeriod [WINMM.@]
368 MMRESULT WINAPI
timeBeginPeriod(UINT wPeriod
)
370 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
371 return TIMERR_NOCANDO
;
373 if (wPeriod
> MMSYSTIME_MININTERVAL
)
375 WARN("Stub; we set our timer resolution at minimum\n");
381 /**************************************************************************
382 * timeEndPeriod [WINMM.@]
384 MMRESULT WINAPI
timeEndPeriod(UINT wPeriod
)
386 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
387 return TIMERR_NOCANDO
;
389 if (wPeriod
> MMSYSTIME_MININTERVAL
)
391 WARN("Stub; we set our timer resolution at minimum\n");