dplayx: Code to forward player creation
[wine/gsoc_dplay.git] / dlls / winmm / time.c
blob8c64ac81125cfbfe079d290549408a046275738f
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <errno.h>
28 #include <time.h>
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SYS_POLL_H
39 #include <sys/poll.h>
40 #endif
42 #include "windef.h"
43 #include "winbase.h"
44 #include "mmsystem.h"
46 #include "winemm.h"
48 #include "wine/list.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
53 typedef struct tagWINE_TIMERENTRY {
54 struct list entry;
55 UINT wDelay;
56 UINT wResol;
57 LPTIMECALLBACK lpFunc; /* can be lots of things */
58 DWORD_PTR dwUser;
59 UINT16 wFlags;
60 UINT16 wTimerID;
61 DWORD dwTriggerTime;
62 } WINE_TIMERENTRY, *LPWINE_TIMERENTRY;
64 static struct list timer_list = LIST_INIT(timer_list);
66 static CRITICAL_SECTION TIME_cbcrst;
67 static CRITICAL_SECTION_DEBUG critsect_debug =
69 0, 0, &TIME_cbcrst,
70 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
71 0, 0, { (DWORD_PTR)(__FILE__ ": TIME_cbcrst") }
73 static CRITICAL_SECTION TIME_cbcrst = { &critsect_debug, -1, 0, 0, 0, 0 };
75 static HANDLE TIME_hMMTimer;
76 static BOOL TIME_TimeToDie = TRUE;
77 static int TIME_fdWake[2] = { -1, -1 };
79 /* link timer at the appropriate spot in the list */
80 static inline void link_timer( WINE_TIMERENTRY *timer )
82 WINE_TIMERENTRY *next;
84 LIST_FOR_EACH_ENTRY( next, &timer_list, WINE_TIMERENTRY, entry )
85 if ((int)(next->dwTriggerTime - timer->dwTriggerTime) >= 0) break;
87 list_add_before( &next->entry, &timer->entry );
91 * Some observations on the behavior of winmm on Windows.
92 * First, the call to timeBeginPeriod(xx) can never be used
93 * to raise the timer resolution, only lower it.
95 * Second, a brief survey of a variety of Win 2k and Win X
96 * machines showed that a 'standard' (aka default) timer
97 * resolution was 1 ms (Win9x is documented as being 1). However, one
98 * machine had a standard timer resolution of 10 ms.
100 * Further, if we set our default resolution to 1,
101 * the implementation of timeGetTime becomes GetTickCount(),
102 * and we can optimize the code to reduce overhead.
104 * Additionally, a survey of Event behaviors shows that
105 * if we request a Periodic event every 50 ms, then Windows
106 * makes sure to trigger that event 20 times in the next
107 * second. If delays prevent that from happening on exact
108 * schedule, Windows will trigger the events as close
109 * to the original schedule as is possible, and will eventually
110 * bring the event triggers back onto a schedule that is
111 * consistent with what would have happened if there were
112 * no delays.
114 * Jeremy White, October 2004
116 #define MMSYSTIME_MININTERVAL (1)
117 #define MMSYSTIME_MAXINTERVAL (65535)
119 #ifdef HAVE_POLL
121 /**************************************************************************
122 * TIME_MMSysTimeCallback
124 static int TIME_MMSysTimeCallback(void)
126 WINE_TIMERENTRY *timer, *to_free;
127 int delta_time;
129 /* since timeSetEvent() and timeKillEvent() can be called
130 * from 16 bit code, there are cases where win16 lock is
131 * locked upon entering timeSetEvent(), and then the mm timer
132 * critical section is locked. This function cannot call the
133 * timer callback with the crit sect locked (because callback
134 * may need to acquire Win16 lock, thus providing a deadlock
135 * situation).
136 * To cope with that, we just copy the WINE_TIMERENTRY struct
137 * that need to trigger the callback, and call it without the
138 * mm timer crit sect locked.
141 EnterCriticalSection(&WINMM_cs);
142 for (;;)
144 struct list *ptr = list_head( &timer_list );
145 if (!ptr)
147 delta_time = -1;
148 break;
151 timer = LIST_ENTRY( ptr, WINE_TIMERENTRY, entry );
152 delta_time = timer->dwTriggerTime - GetTickCount();
153 if (delta_time > 0) break;
155 list_remove( &timer->entry );
156 if (timer->wFlags & TIME_PERIODIC)
158 timer->dwTriggerTime += timer->wDelay;
159 link_timer( timer ); /* restart it */
160 to_free = NULL;
162 else to_free = timer;
164 switch(timer->wFlags & (TIME_CALLBACK_EVENT_SET|TIME_CALLBACK_EVENT_PULSE))
166 case TIME_CALLBACK_EVENT_SET:
167 SetEvent(timer->lpFunc);
168 break;
169 case TIME_CALLBACK_EVENT_PULSE:
170 PulseEvent(timer->lpFunc);
171 break;
172 case TIME_CALLBACK_FUNCTION:
174 DWORD_PTR user = timer->dwUser;
175 UINT16 id = timer->wTimerID;
176 UINT16 flags = timer->wFlags;
177 LPTIMECALLBACK func = timer->lpFunc;
179 if (flags & TIME_KILL_SYNCHRONOUS) EnterCriticalSection(&TIME_cbcrst);
180 LeaveCriticalSection(&WINMM_cs);
182 if (flags & WINE_TIMER_IS32) func(id, 0, user, 0, 0);
183 else if (pFnCallMMDrvFunc16) pFnCallMMDrvFunc16((DWORD_PTR)func, id, 0, user, 0, 0);
185 EnterCriticalSection(&WINMM_cs);
186 if (flags & TIME_KILL_SYNCHRONOUS) LeaveCriticalSection(&TIME_cbcrst);
188 break;
190 HeapFree( GetProcessHeap(), 0, to_free );
192 LeaveCriticalSection(&WINMM_cs);
193 return delta_time;
196 /**************************************************************************
197 * TIME_MMSysTimeThread
199 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
201 int sleep_time, ret;
202 char readme[16];
203 struct pollfd pfd;
205 pfd.fd = TIME_fdWake[0];
206 pfd.events = POLLIN;
208 TRACE("Starting main winmm thread\n");
210 /* FIXME: As an optimization, we could have
211 this thread die when there are no more requests
212 pending, and then get recreated on the first
213 new event; it's not clear if that would be worth
214 it or not. */
216 while (! TIME_TimeToDie)
218 sleep_time = TIME_MMSysTimeCallback();
220 if (sleep_time == 0)
221 continue;
223 if ((ret = poll(&pfd, 1, sleep_time)) < 0)
225 if (errno != EINTR && errno != EAGAIN)
227 ERR("Unexpected error in poll: %s(%d)\n", strerror(errno), errno);
228 break;
232 while (ret > 0) ret = read(TIME_fdWake[0], readme, sizeof(readme));
234 TRACE("Exiting main winmm thread\n");
235 return 0;
238 /**************************************************************************
239 * TIME_MMTimeStart
241 static void TIME_MMTimeStart(void)
243 if (!TIME_hMMTimer) {
244 if (pipe(TIME_fdWake) < 0)
246 TIME_fdWake[0] = TIME_fdWake[1] = -1;
247 ERR("Cannot create pipe: %s\n", strerror(errno));
248 } else {
249 fcntl(TIME_fdWake[0], F_SETFL, O_NONBLOCK);
250 fcntl(TIME_fdWake[1], F_SETFL, O_NONBLOCK);
252 TIME_TimeToDie = FALSE;
253 TIME_hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, NULL, 0, NULL);
254 SetThreadPriority(TIME_hMMTimer, THREAD_PRIORITY_TIME_CRITICAL);
258 #else /* HAVE_POLL */
260 static void TIME_MMTimeStart(void)
262 FIXME( "not starting system thread\n" );
265 #endif /* HAVE_POLL */
267 /**************************************************************************
268 * TIME_MMTimeStop
270 void TIME_MMTimeStop(void)
272 if (TIME_hMMTimer) {
273 const char a='a';
275 TIME_TimeToDie = TRUE;
276 write(TIME_fdWake[1], &a, sizeof(a));
278 WaitForSingleObject(TIME_hMMTimer, INFINITE);
279 close(TIME_fdWake[0]);
280 close(TIME_fdWake[1]);
281 TIME_fdWake[0] = TIME_fdWake[1] = -1;
282 CloseHandle(TIME_hMMTimer);
283 TIME_hMMTimer = 0;
284 DeleteCriticalSection(&TIME_cbcrst);
288 /**************************************************************************
289 * timeGetSystemTime [WINMM.@]
291 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
294 if (wSize >= sizeof(*lpTime)) {
295 lpTime->wType = TIME_MS;
296 lpTime->u.ms = GetTickCount();
300 return 0;
303 /**************************************************************************
304 * TIME_SetEventInternal [internal]
306 WORD TIME_SetEventInternal(UINT wDelay, UINT wResol,
307 LPTIMECALLBACK lpFunc, DWORD_PTR dwUser, UINT wFlags)
309 WORD wNewID = 0;
310 LPWINE_TIMERENTRY lpNewTimer;
311 LPWINE_TIMERENTRY lpTimer;
312 const char c = 'c';
314 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
316 if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
317 return 0;
319 lpNewTimer = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
320 if (lpNewTimer == NULL)
321 return 0;
323 lpNewTimer->wDelay = wDelay;
324 lpNewTimer->dwTriggerTime = GetTickCount() + wDelay;
326 /* FIXME - wResol is not respected, although it is not clear
327 that we could change our precision meaningfully */
328 lpNewTimer->wResol = wResol;
329 lpNewTimer->lpFunc = lpFunc;
330 lpNewTimer->dwUser = dwUser;
331 lpNewTimer->wFlags = wFlags;
333 EnterCriticalSection(&WINMM_cs);
335 LIST_FOR_EACH_ENTRY( lpTimer, &timer_list, WINE_TIMERENTRY, entry )
336 wNewID = max(wNewID, lpTimer->wTimerID);
338 link_timer( lpNewTimer );
339 lpNewTimer->wTimerID = wNewID + 1;
341 TIME_MMTimeStart();
343 LeaveCriticalSection(&WINMM_cs);
345 /* Wake the service thread in case there is work to be done */
346 write(TIME_fdWake[1], &c, sizeof(c));
348 TRACE("=> %u\n", wNewID + 1);
350 return wNewID + 1;
353 /**************************************************************************
354 * timeSetEvent [WINMM.@]
356 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
357 DWORD_PTR dwUser, UINT wFlags)
359 if (wFlags & WINE_TIMER_IS32)
360 WARN("Unknown windows flag... wine internally used.. ooch\n");
362 return TIME_SetEventInternal(wDelay, wResol, lpFunc,
363 dwUser, wFlags|WINE_TIMER_IS32);
366 /**************************************************************************
367 * timeKillEvent [WINMM.@]
369 MMRESULT WINAPI timeKillEvent(UINT wID)
371 WINE_TIMERENTRY *lpSelf = NULL, *lpTimer;
372 DWORD wFlags;
374 TRACE("(%u)\n", wID);
375 EnterCriticalSection(&WINMM_cs);
376 /* remove WINE_TIMERENTRY from list */
377 LIST_FOR_EACH_ENTRY( lpTimer, &timer_list, WINE_TIMERENTRY, entry )
379 if (wID == lpTimer->wTimerID) {
380 lpSelf = lpTimer;
381 list_remove( &lpTimer->entry );
382 break;
385 LeaveCriticalSection(&WINMM_cs);
387 if (!lpSelf)
389 WARN("wID=%u is not a valid timer ID\n", wID);
390 return MMSYSERR_INVALPARAM;
392 wFlags = lpSelf->wFlags;
393 if (wFlags & TIME_KILL_SYNCHRONOUS)
394 EnterCriticalSection(&TIME_cbcrst);
395 HeapFree(GetProcessHeap(), 0, lpSelf);
396 if (wFlags & TIME_KILL_SYNCHRONOUS)
397 LeaveCriticalSection(&TIME_cbcrst);
398 return TIMERR_NOERROR;
401 /**************************************************************************
402 * timeGetDevCaps [WINMM.@]
404 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
406 TRACE("(%p, %u)\n", lpCaps, wSize);
408 if (lpCaps == 0) {
409 WARN("invalid lpCaps\n");
410 return TIMERR_NOCANDO;
413 if (wSize < sizeof(TIMECAPS)) {
414 WARN("invalid wSize\n");
415 return TIMERR_NOCANDO;
418 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
419 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
420 return TIMERR_NOERROR;
423 /**************************************************************************
424 * timeBeginPeriod [WINMM.@]
426 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
428 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
429 return TIMERR_NOCANDO;
431 if (wPeriod > MMSYSTIME_MININTERVAL)
433 WARN("Stub; we set our timer resolution at minimum\n");
436 return 0;
439 /**************************************************************************
440 * timeEndPeriod [WINMM.@]
442 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
444 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
445 return TIMERR_NOCANDO;
447 if (wPeriod > MMSYSTIME_MININTERVAL)
449 WARN("Stub; we set our timer resolution at minimum\n");
451 return 0;
454 /**************************************************************************
455 * timeGetTime [MMSYSTEM.607]
456 * timeGetTime [WINMM.@]
458 DWORD WINAPI timeGetTime(void)
460 #if defined(COMMENTOUTPRIORTODELETING)
461 DWORD count;
463 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
464 * that lets mciavi32.dll run correctly
466 if (pFnReleaseThunkLock) pFnReleaseThunkLock(&count);
467 if (pFnRestoreThunkLock) pFnRestoreThunkLock(count);
468 #endif
470 return GetTickCount();