Commit 90d94efe authored by Alexandre Julliard's avatar Alexandre Julliard

winmm: Fix computation of sleep time until next timeout.

parent 622f62d3
...@@ -113,7 +113,7 @@ static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer) ...@@ -113,7 +113,7 @@ static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
/************************************************************************** /**************************************************************************
* TIME_MMSysTimeCallback * TIME_MMSysTimeCallback
*/ */
static DWORD CALLBACK TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData) static int TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData)
{ {
static int nSizeLpTimers; static int nSizeLpTimers;
static LPWINE_TIMERENTRY lpTimers; static LPWINE_TIMERENTRY lpTimers;
...@@ -121,14 +121,7 @@ static LPWINE_TIMERENTRY lpTimers; ...@@ -121,14 +121,7 @@ static LPWINE_TIMERENTRY lpTimers;
LPWINE_TIMERENTRY timer, *ptimer, *next_ptimer; LPWINE_TIMERENTRY timer, *ptimer, *next_ptimer;
int idx; int idx;
DWORD cur_time; DWORD cur_time;
DWORD delta_time; int delta_time, ret_time = -1;
DWORD ret_time = INFINITE;
DWORD adjust_time;
/* optimize for the most frequent case - no events */
if (! TIME_TimersList)
return(ret_time);
/* since timeSetEvent() and timeKillEvent() can be called /* since timeSetEvent() and timeKillEvent() can be called
* from 16 bit code, there are cases where win16 lock is * from 16 bit code, there are cases where win16 lock is
...@@ -151,7 +144,8 @@ static LPWINE_TIMERENTRY lpTimers; ...@@ -151,7 +144,8 @@ static LPWINE_TIMERENTRY lpTimers;
for (ptimer = &TIME_TimersList; *ptimer != NULL; ) { for (ptimer = &TIME_TimersList; *ptimer != NULL; ) {
timer = *ptimer; timer = *ptimer;
next_ptimer = &timer->lpNext; next_ptimer = &timer->lpNext;
if (cur_time >= timer->dwTriggerTime) delta_time = timer->dwTriggerTime - cur_time;
if (delta_time <= 0)
{ {
if (timer->lpFunc) { if (timer->lpFunc) {
if (idx == nSizeLpTimers) { if (idx == nSizeLpTimers) {
...@@ -180,23 +174,22 @@ static LPWINE_TIMERENTRY lpTimers; ...@@ -180,23 +174,22 @@ static LPWINE_TIMERENTRY lpTimers;
HeapFree(GetProcessHeap(), 0, timer); HeapFree(GetProcessHeap(), 0, timer);
/* We don't need to trigger oneshots again */ /* We don't need to trigger oneshots again */
delta_time = INFINITE; delta_time = -1;
} }
else else
{ {
/* Compute when this event needs this function /* Compute when this event needs this function
to be called again */ to be called again */
if (timer->dwTriggerTime <= cur_time)
delta_time = 0;
else
delta_time = timer->dwTriggerTime - cur_time; delta_time = timer->dwTriggerTime - cur_time;
if (delta_time < 0) delta_time = 0;
} }
} }
else
delta_time = timer->dwTriggerTime - cur_time;
/* Determine when we need to return to this function */ /* Determine when we need to return to this function */
ret_time = min(ret_time, delta_time); if (delta_time != -1)
{
if (ret_time == -1 || ret_time > delta_time) ret_time = delta_time;
}
ptimer = next_ptimer; ptimer = next_ptimer;
} }
...@@ -209,15 +202,15 @@ static LPWINE_TIMERENTRY lpTimers; ...@@ -209,15 +202,15 @@ static LPWINE_TIMERENTRY lpTimers;
/* Finally, adjust the recommended wait time downward /* Finally, adjust the recommended wait time downward
by the amount of time the processing routines by the amount of time the processing routines
actually took */ actually took */
adjust_time = GetTickCount() - cur_time; if (ret_time != -1)
if (adjust_time > ret_time) {
ret_time = 0; ret_time -= GetTickCount() - cur_time;
else if (ret_time < 0) ret_time = 0;
ret_time -= adjust_time; }
/* We return the amount of time our caller should sleep /* We return the amount of time our caller should sleep
before needing to check in on us again */ before needing to check in on us again */
return(ret_time); return ret_time;
} }
/************************************************************************** /**************************************************************************
...@@ -226,7 +219,6 @@ static LPWINE_TIMERENTRY lpTimers; ...@@ -226,7 +219,6 @@ static LPWINE_TIMERENTRY lpTimers;
static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg) static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
{ {
LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg; LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
DWORD sleep_time;
DWORD rc; DWORD rc;
TRACE("Starting main winmm thread\n"); TRACE("Starting main winmm thread\n");
...@@ -239,12 +231,12 @@ static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg) ...@@ -239,12 +231,12 @@ static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
while (! TIME_TimeToDie) while (! TIME_TimeToDie)
{ {
sleep_time = TIME_MMSysTimeCallback(iData); int sleep_time = TIME_MMSysTimeCallback(iData);
if (sleep_time == 0) if (sleep_time == 0)
continue; continue;
rc = WaitForSingleObject(TIME_hWakeEvent, sleep_time); rc = WaitForSingleObject(TIME_hWakeEvent, (sleep_time == -1) ? INFINITE : (DWORD)sleep_time);
if (rc != WAIT_TIMEOUT && rc != WAIT_OBJECT_0) if (rc != WAIT_TIMEOUT && rc != WAIT_OBJECT_0)
{ {
FIXME("Unexpected error %d(%d) in timer thread\n", rc, GetLastError()); FIXME("Unexpected error %d(%d) in timer thread\n", rc, GetLastError());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment