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 CRITICAL_SECTION TIME_cbcrst
;
67 static CRITICAL_SECTION_DEBUG critsect_debug
=
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
114 * Jeremy White, October 2004
116 #define MMSYSTIME_MININTERVAL (1)
117 #define MMSYSTIME_MAXINTERVAL (65535)
121 /**************************************************************************
122 * TIME_MMSysTimeCallback
124 static int TIME_MMSysTimeCallback(void)
126 WINE_TIMERENTRY
*timer
, *to_free
;
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
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.
143 struct list
*ptr
= list_head( &timer_list
);
150 timer
= LIST_ENTRY( ptr
, WINE_TIMERENTRY
, entry
);
151 delta_time
= timer
->dwTriggerTime
- GetTickCount();
152 if (delta_time
> 0) break;
154 list_remove( &timer
->entry
);
155 if (timer
->wFlags
& TIME_PERIODIC
)
157 timer
->dwTriggerTime
+= timer
->wDelay
;
158 link_timer( timer
); /* restart it */
161 else to_free
= timer
;
163 switch(timer
->wFlags
& (TIME_CALLBACK_EVENT_SET
|TIME_CALLBACK_EVENT_PULSE
))
165 case TIME_CALLBACK_EVENT_SET
:
166 SetEvent(timer
->lpFunc
);
168 case TIME_CALLBACK_EVENT_PULSE
:
169 PulseEvent(timer
->lpFunc
);
171 case TIME_CALLBACK_FUNCTION
:
173 DWORD_PTR user
= timer
->dwUser
;
174 UINT16 id
= timer
->wTimerID
;
175 UINT16 flags
= timer
->wFlags
;
176 LPTIMECALLBACK func
= timer
->lpFunc
;
178 if (flags
& TIME_KILL_SYNCHRONOUS
) EnterCriticalSection(&TIME_cbcrst
);
179 LeaveCriticalSection(&WINMM_cs
);
181 func(id
, 0, user
, 0, 0);
183 EnterCriticalSection(&WINMM_cs
);
184 if (flags
& TIME_KILL_SYNCHRONOUS
) LeaveCriticalSection(&TIME_cbcrst
);
188 HeapFree( GetProcessHeap(), 0, to_free
);
193 /**************************************************************************
194 * TIME_MMSysTimeThread
196 static DWORD CALLBACK
TIME_MMSysTimeThread(LPVOID arg
)
202 pfd
.fd
= TIME_fdWake
[0];
205 TRACE("Starting main winmm thread\n");
207 EnterCriticalSection(&WINMM_cs
);
208 while (! TIME_TimeToDie
)
210 sleep_time
= TIME_MMSysTimeCallback();
217 LeaveCriticalSection(&WINMM_cs
);
218 ret
= poll(&pfd
, 1, sleep_time
);
219 EnterCriticalSection(&WINMM_cs
);
223 if (errno
!= EINTR
&& errno
!= EAGAIN
)
225 ERR("Unexpected error in poll: %s(%d)\n", strerror(errno
), errno
);
230 while (ret
> 0) ret
= read(TIME_fdWake
[0], readme
, sizeof(readme
));
232 CloseHandle(TIME_hMMTimer
);
233 TIME_hMMTimer
= NULL
;
234 LeaveCriticalSection(&WINMM_cs
);
235 TRACE("Exiting main winmm thread\n");
236 FreeLibraryAndExitThread(arg
, 0);
240 /**************************************************************************
243 static void TIME_MMTimeStart(void)
247 if (TIME_fdWake
[0] < 0) {
248 if (pipe(TIME_fdWake
) < 0) {
249 TIME_fdWake
[0] = TIME_fdWake
[1] = -1;
250 ERR("Cannot create pipe: %s\n", strerror(errno
));
252 fcntl(TIME_fdWake
[0], F_SETFL
, O_NONBLOCK
);
253 fcntl(TIME_fdWake
[1], F_SETFL
, O_NONBLOCK
);
257 if (!TIME_hMMTimer
) {
259 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)TIME_MMSysTimeThread
, &mod
);
260 TIME_hMMTimer
= CreateThread(NULL
, 0, TIME_MMSysTimeThread
, mod
, 0, NULL
);
261 SetThreadPriority(TIME_hMMTimer
, THREAD_PRIORITY_TIME_CRITICAL
);
265 #else /* HAVE_POLL */
267 static void TIME_MMTimeStart(void)
269 FIXME( "not starting system thread\n" );
272 #endif /* HAVE_POLL */
274 /**************************************************************************
277 void TIME_MMTimeStop(void)
280 EnterCriticalSection(&WINMM_cs
);
282 ERR("Timer still active?!\n");
283 CloseHandle(TIME_hMMTimer
);
285 close(TIME_fdWake
[0]);
286 close(TIME_fdWake
[1]);
287 DeleteCriticalSection(&TIME_cbcrst
);
291 /**************************************************************************
292 * timeGetSystemTime [WINMM.@]
294 MMRESULT WINAPI
timeGetSystemTime(LPMMTIME lpTime
, UINT wSize
)
297 if (wSize
>= sizeof(*lpTime
)) {
298 lpTime
->wType
= TIME_MS
;
299 lpTime
->u
.ms
= GetTickCount();
306 /**************************************************************************
307 * timeSetEvent [WINMM.@]
309 MMRESULT WINAPI
timeSetEvent(UINT wDelay
, UINT wResol
, LPTIMECALLBACK lpFunc
,
310 DWORD_PTR dwUser
, UINT wFlags
)
313 LPWINE_TIMERENTRY lpNewTimer
;
314 LPWINE_TIMERENTRY lpTimer
;
317 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay
, wResol
, lpFunc
, dwUser
, wFlags
);
319 if (wDelay
< MMSYSTIME_MININTERVAL
|| wDelay
> MMSYSTIME_MAXINTERVAL
)
322 lpNewTimer
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY
));
323 if (lpNewTimer
== NULL
)
326 lpNewTimer
->wDelay
= wDelay
;
327 lpNewTimer
->dwTriggerTime
= GetTickCount() + wDelay
;
329 /* FIXME - wResol is not respected, although it is not clear
330 that we could change our precision meaningfully */
331 lpNewTimer
->wResol
= wResol
;
332 lpNewTimer
->lpFunc
= lpFunc
;
333 lpNewTimer
->dwUser
= dwUser
;
334 lpNewTimer
->wFlags
= wFlags
;
336 EnterCriticalSection(&WINMM_cs
);
338 LIST_FOR_EACH_ENTRY( lpTimer
, &timer_list
, WINE_TIMERENTRY
, entry
)
339 wNewID
= max(wNewID
, lpTimer
->wTimerID
);
341 link_timer( lpNewTimer
);
342 lpNewTimer
->wTimerID
= wNewID
+ 1;
346 LeaveCriticalSection(&WINMM_cs
);
348 /* Wake the service thread in case there is work to be done */
349 write(TIME_fdWake
[1], &c
, sizeof(c
));
351 TRACE("=> %u\n", wNewID
+ 1);
356 /**************************************************************************
357 * timeKillEvent [WINMM.@]
359 MMRESULT WINAPI
timeKillEvent(UINT wID
)
361 WINE_TIMERENTRY
*lpSelf
= NULL
, *lpTimer
;
364 TRACE("(%u)\n", wID
);
365 EnterCriticalSection(&WINMM_cs
);
366 /* remove WINE_TIMERENTRY from list */
367 LIST_FOR_EACH_ENTRY( lpTimer
, &timer_list
, WINE_TIMERENTRY
, entry
)
369 if (wID
== lpTimer
->wTimerID
) {
371 list_remove( &lpTimer
->entry
);
375 if (list_empty(&timer_list
)) {
378 write(TIME_fdWake
[1], &c
, sizeof(c
));
380 LeaveCriticalSection(&WINMM_cs
);
384 WARN("wID=%u is not a valid timer ID\n", wID
);
385 return MMSYSERR_INVALPARAM
;
387 wFlags
= lpSelf
->wFlags
;
388 if (wFlags
& TIME_KILL_SYNCHRONOUS
)
389 EnterCriticalSection(&TIME_cbcrst
);
390 HeapFree(GetProcessHeap(), 0, lpSelf
);
391 if (wFlags
& TIME_KILL_SYNCHRONOUS
)
392 LeaveCriticalSection(&TIME_cbcrst
);
393 return TIMERR_NOERROR
;
396 /**************************************************************************
397 * timeGetDevCaps [WINMM.@]
399 MMRESULT WINAPI
timeGetDevCaps(LPTIMECAPS lpCaps
, UINT wSize
)
401 TRACE("(%p, %u)\n", lpCaps
, wSize
);
404 WARN("invalid lpCaps\n");
405 return TIMERR_NOCANDO
;
408 if (wSize
< sizeof(TIMECAPS
)) {
409 WARN("invalid wSize\n");
410 return TIMERR_NOCANDO
;
413 lpCaps
->wPeriodMin
= MMSYSTIME_MININTERVAL
;
414 lpCaps
->wPeriodMax
= MMSYSTIME_MAXINTERVAL
;
415 return TIMERR_NOERROR
;
418 /**************************************************************************
419 * timeBeginPeriod [WINMM.@]
421 MMRESULT WINAPI
timeBeginPeriod(UINT wPeriod
)
423 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
424 return TIMERR_NOCANDO
;
426 if (wPeriod
> MMSYSTIME_MININTERVAL
)
428 WARN("Stub; we set our timer resolution at minimum\n");
434 /**************************************************************************
435 * timeEndPeriod [WINMM.@]
437 MMRESULT WINAPI
timeEndPeriod(UINT wPeriod
)
439 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
440 return TIMERR_NOCANDO
;
442 if (wPeriod
> MMSYSTIME_MININTERVAL
)
444 WARN("Stub; we set our timer resolution at minimum\n");