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
24 #include "wine/port.h"
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
38 #ifdef HAVE_SYS_POLL_H
48 #include "wine/list.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
53 typedef struct tagWINE_TIMERENTRY {
57 LPTIMECALLBACK lpFunc; /* can be lots of things */
62 } WINE_TIMERENTRY, *LPWINE_TIMERENTRY;
64 static struct list timer_list = LIST_INIT(timer_list);
66 static HANDLE TIME_hMMTimer;
67 static CRITICAL_SECTION TIME_cbcrst;
68 static BOOL TIME_TimeToDie = TRUE;
69 static int TIME_fdWake[2] = { -1, -1 };
71 /* link timer at the appropriate spot in the list */
72 static inline void link_timer( WINE_TIMERENTRY *timer )
74 WINE_TIMERENTRY *next;
76 LIST_FOR_EACH_ENTRY( next, &timer_list, WINE_TIMERENTRY, entry )
77 if ((int)(next->dwTriggerTime - timer->dwTriggerTime) >= 0) break;
79 list_add_before( &next->entry, &timer->entry );
83 * Some observations on the behavior of winmm on Windows.
84 * First, the call to timeBeginPeriod(xx) can never be used
85 * to raise the timer resolution, only lower it.
87 * Second, a brief survey of a variety of Win 2k and Win X
88 * machines showed that a 'standard' (aka default) timer
89 * resolution was 1 ms (Win9x is documented as being 1). However, one
90 * machine had a standard timer resolution of 10 ms.
92 * Further, if we set our default resolution to 1,
93 * the implementation of timeGetTime becomes GetTickCount(),
94 * and we can optimize the code to reduce overhead.
96 * Additionally, a survey of Event behaviors shows that
97 * if we request a Periodic event every 50 ms, then Windows
98 * makes sure to trigger that event 20 times in the next
99 * second. If delays prevent that from happening on exact
100 * schedule, Windows will trigger the events as close
101 * to the original schedule as is possible, and will eventually
102 * bring the event triggers back onto a schedule that is
103 * consistent with what would have happened if there were
106 * Jeremy White, October 2004
108 #define MMSYSTIME_MININTERVAL (1)
109 #define MMSYSTIME_MAXINTERVAL (65535)
112 /**************************************************************************
113 * TIME_MMSysTimeCallback
115 static int TIME_MMSysTimeCallback(void)
117 WINE_TIMERENTRY *timer, *to_free;
120 /* since timeSetEvent() and timeKillEvent() can be called
121 * from 16 bit code, there are cases where win16 lock is
122 * locked upon entering timeSetEvent(), and then the mm timer
123 * critical section is locked. This function cannot call the
124 * timer callback with the crit sect locked (because callback
125 * may need to acquire Win16 lock, thus providing a deadlock
127 * To cope with that, we just copy the WINE_TIMERENTRY struct
128 * that need to trigger the callback, and call it without the
129 * mm timer crit sect locked.
132 EnterCriticalSection(&WINMM_cs);
135 struct list *ptr = list_head( &timer_list );
142 timer = LIST_ENTRY( ptr, WINE_TIMERENTRY, entry );
143 delta_time = timer->dwTriggerTime - GetTickCount();
144 if (delta_time > 0) break;
146 list_remove( &timer->entry );
147 if (timer->wFlags & TIME_PERIODIC)
149 timer->dwTriggerTime += timer->wDelay;
150 link_timer( timer ); /* restart it */
153 else to_free = timer;
155 switch(timer->wFlags & (TIME_CALLBACK_EVENT_SET|TIME_CALLBACK_EVENT_PULSE))
157 case TIME_CALLBACK_EVENT_SET:
158 SetEvent((HANDLE)timer->lpFunc);
160 case TIME_CALLBACK_EVENT_PULSE:
161 PulseEvent((HANDLE)timer->lpFunc);
163 case TIME_CALLBACK_FUNCTION:
165 DWORD user = timer->dwUser;
166 UINT16 id = timer->wTimerID;
167 UINT16 flags = timer->wFlags;
168 LPTIMECALLBACK func = timer->lpFunc;
170 if (flags & TIME_KILL_SYNCHRONOUS) EnterCriticalSection(&TIME_cbcrst);
171 LeaveCriticalSection(&WINMM_cs);
173 if (flags & WINE_TIMER_IS32) func(id, 0, user, 0, 0);
174 else if (pFnCallMMDrvFunc16) pFnCallMMDrvFunc16((DWORD)func, id, 0, user, 0, 0);
176 EnterCriticalSection(&WINMM_cs);
177 if (flags & TIME_KILL_SYNCHRONOUS) LeaveCriticalSection(&TIME_cbcrst);
181 HeapFree( GetProcessHeap(), 0, to_free );
183 LeaveCriticalSection(&WINMM_cs);
187 /**************************************************************************
188 * TIME_MMSysTimeThread
190 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
196 pfd.fd = TIME_fdWake[0];
199 TRACE("Starting main winmm thread\n");
201 /* FIXME: As an optimization, we could have
202 this thread die when there are no more requests
203 pending, and then get recreated on the first
204 new event; it's not clear if that would be worth
207 while (! TIME_TimeToDie)
209 sleep_time = TIME_MMSysTimeCallback();
214 if ((ret = poll(&pfd, 1, sleep_time)) < 0)
216 if (errno != EINTR && errno != EAGAIN)
218 ERR("Unexpected error in poll: %s(%d)\n", strerror(errno), errno);
223 while (ret > 0) ret = read(TIME_fdWake[0], readme, sizeof(readme));
225 TRACE("Exiting main winmm thread\n");
229 /**************************************************************************
232 static void TIME_MMTimeStart(void)
234 if (!TIME_hMMTimer) {
235 if (pipe(TIME_fdWake) < 0)
237 TIME_fdWake[0] = TIME_fdWake[1] = -1;
238 ERR("Cannot create pipe: %s\n", strerror(errno));
240 fcntl(TIME_fdWake[0], F_SETFL, O_NONBLOCK);
241 fcntl(TIME_fdWake[1], F_SETFL, O_NONBLOCK);
243 TIME_TimeToDie = FALSE;
244 TIME_hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, NULL, 0, NULL);
245 SetThreadPriority(TIME_hMMTimer, THREAD_PRIORITY_TIME_CRITICAL);
246 InitializeCriticalSection(&TIME_cbcrst);
247 TIME_cbcrst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM.TIME_cbcrst");
251 /**************************************************************************
254 void TIME_MMTimeStop(void)
259 TIME_TimeToDie = TRUE;
260 write(TIME_fdWake[1], &a, sizeof(a));
262 WaitForSingleObject(TIME_hMMTimer, INFINITE);
263 close(TIME_fdWake[0]);
264 close(TIME_fdWake[1]);
265 TIME_fdWake[0] = TIME_fdWake[1] = -1;
266 CloseHandle(TIME_hMMTimer);
268 TIME_cbcrst.DebugInfo->Spare[0] = 0;
269 DeleteCriticalSection(&TIME_cbcrst);
273 /**************************************************************************
274 * timeGetSystemTime [WINMM.@]
276 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
279 if (wSize >= sizeof(*lpTime)) {
280 lpTime->wType = TIME_MS;
281 lpTime->u.ms = GetTickCount();
288 /**************************************************************************
289 * TIME_SetEventInternal [internal]
291 WORD TIME_SetEventInternal(UINT wDelay, UINT wResol,
292 LPTIMECALLBACK lpFunc, DWORD dwUser, UINT wFlags)
295 LPWINE_TIMERENTRY lpNewTimer;
296 LPWINE_TIMERENTRY lpTimer;
299 TRACE("(%u, %u, %p, %08X, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
301 if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
304 lpNewTimer = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
305 if (lpNewTimer == NULL)
308 lpNewTimer->wDelay = wDelay;
309 lpNewTimer->dwTriggerTime = GetTickCount() + wDelay;
311 /* FIXME - wResol is not respected, although it is not clear
312 that we could change our precision meaningfully */
313 lpNewTimer->wResol = wResol;
314 lpNewTimer->lpFunc = lpFunc;
315 lpNewTimer->dwUser = dwUser;
316 lpNewTimer->wFlags = wFlags;
318 EnterCriticalSection(&WINMM_cs);
320 LIST_FOR_EACH_ENTRY( lpTimer, &timer_list, WINE_TIMERENTRY, entry )
321 wNewID = max(wNewID, lpTimer->wTimerID);
323 link_timer( lpNewTimer );
324 lpNewTimer->wTimerID = wNewID + 1;
328 LeaveCriticalSection(&WINMM_cs);
330 /* Wake the service thread in case there is work to be done */
331 write(TIME_fdWake[1], &c, sizeof(c));
333 TRACE("=> %u\n", wNewID + 1);
338 /**************************************************************************
339 * timeSetEvent [WINMM.@]
341 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
342 DWORD_PTR dwUser, UINT wFlags)
344 if (wFlags & WINE_TIMER_IS32)
345 WARN("Unknown windows flag... wine internally used.. ooch\n");
347 return TIME_SetEventInternal(wDelay, wResol, lpFunc,
348 dwUser, wFlags|WINE_TIMER_IS32);
351 /**************************************************************************
352 * timeKillEvent [WINMM.@]
354 MMRESULT WINAPI timeKillEvent(UINT wID)
356 WINE_TIMERENTRY *lpSelf = NULL, *lpTimer;
358 TRACE("(%u)\n", wID);
359 EnterCriticalSection(&WINMM_cs);
360 /* remove WINE_TIMERENTRY from list */
361 LIST_FOR_EACH_ENTRY( lpTimer, &timer_list, WINE_TIMERENTRY, entry )
363 if (wID == lpTimer->wTimerID) {
365 list_remove( &lpTimer->entry );
369 LeaveCriticalSection(&WINMM_cs);
373 WARN("wID=%u is not a valid timer ID\n", wID);
374 return MMSYSERR_INVALPARAM;
376 if (lpSelf->wFlags & TIME_KILL_SYNCHRONOUS)
377 EnterCriticalSection(&TIME_cbcrst);
378 HeapFree(GetProcessHeap(), 0, lpSelf);
379 if (lpSelf->wFlags & TIME_KILL_SYNCHRONOUS)
380 LeaveCriticalSection(&TIME_cbcrst);
381 return TIMERR_NOERROR;
384 /**************************************************************************
385 * timeGetDevCaps [WINMM.@]
387 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
389 TRACE("(%p, %u)\n", lpCaps, wSize);
392 WARN("invalid lpCaps\n");
393 return TIMERR_NOCANDO;
396 if (wSize < sizeof(TIMECAPS)) {
397 WARN("invalid wSize\n");
398 return TIMERR_NOCANDO;
401 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
402 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
403 return TIMERR_NOERROR;
406 /**************************************************************************
407 * timeBeginPeriod [WINMM.@]
409 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
411 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
412 return TIMERR_NOCANDO;
414 if (wPeriod > MMSYSTIME_MININTERVAL)
416 WARN("Stub; we set our timer resolution at minimum\n");
422 /**************************************************************************
423 * timeEndPeriod [WINMM.@]
425 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
427 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
428 return TIMERR_NOCANDO;
430 if (wPeriod > MMSYSTIME_MININTERVAL)
432 WARN("Stub; we set our timer resolution at minimum\n");
437 /**************************************************************************
438 * timeGetTime [MMSYSTEM.607]
439 * timeGetTime [WINMM.@]
441 DWORD WINAPI timeGetTime(void)
443 #if defined(COMMENTOUTPRIORTODELETING)
446 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
447 * that lets mciavi32.dll run correctly
449 if (pFnReleaseThunkLock) pFnReleaseThunkLock(&count);
450 if (pFnRestoreThunkLock) pFnRestoreThunkLock(count);
453 return GetTickCount();