winmm: Use fd's instead of events to synchronize, v2

Maarten Lankhorst m.b.lankhorst at gmail.com
Thu May 17 07:19:00 CDT 2007


Uses nonblocking calls now, have to keep cast for timeout, but timeout
shouldn't be bigger then 65536 so it's harmless.
-------------- next part --------------
>From 55cb9bea356634e2f8fa6261e005b327835058e4 Mon Sep 17 00:00:00 2001
From: Maarten Lankhorst <m.b.lankhorst at gmail.com>
Date: Mon, 14 May 2007 21:58:59 +0200
Subject: [PATCH] winmm: Use unix fd's to sync

---
 dlls/winmm/time.c |   90 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 60 insertions(+), 30 deletions(-)

diff --git a/dlls/winmm/time.c b/dlls/winmm/time.c
index 27a4e46..42aff27 100644
--- a/dlls/winmm/time.c
+++ b/dlls/winmm/time.c
@@ -4,6 +4,7 @@
  * MMSYSTEM time functions
  *
  * Copyright 1993 Martin Ayotte
+ * Copyright 2007 Maarten Lankhorst
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+/* Wine <= 0.9.37 uses win32 handles instead of unix pipes to sync, for those who are interested */
+
 #include "config.h"
 #include "wine/port.h"
 
@@ -32,6 +35,14 @@
 # include <unistd.h>
 #endif
 
+#ifdef HAVE_POLL_H
+#include <poll.h>
+#endif
+#ifdef HAVE_SYS_POLL_H
+#include <sys/poll.h>
+#endif
+#include <errno.h>
+
 #include "windef.h"
 #include "winbase.h"
 #include "mmsystem.h"
@@ -44,9 +55,9 @@ WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
 
 static    HANDLE                TIME_hMMTimer;
 static    LPWINE_TIMERENTRY 	TIME_TimersList;
-static    HANDLE                TIME_hWakeEvent;
 static    CRITICAL_SECTION      TIME_cbcrst;
 static    BOOL                  TIME_TimeToDie = TRUE;
+static    int                   TIME_fdWake[2] = { -1, -1 };
 
 /*
  * Some observations on the behavior of winmm on Windows.
@@ -77,7 +88,6 @@ static    BOOL                  TIME_TimeToDie = TRUE;
 #define MMSYSTIME_MININTERVAL (1)
 #define MMSYSTIME_MAXINTERVAL (65535)
 
-
 static	void	TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
 {
     TRACE("%04x:CallBack => lpFunc=%p wTimerID=%04X dwUser=%08X dwTriggerTime %d(delta %d)\n",
@@ -226,8 +236,13 @@ static    LPWINE_TIMERENTRY		lpTimers;
 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
 {
     LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
-    DWORD sleep_time;
-    DWORD rc;
+    int sleep_time;
+    char readme[16];
+    int ret;
+
+    struct pollfd pfd;
+    pfd.fd = TIME_fdWake[0];
+    pfd.events = POLLIN;
 
     TRACE("Starting main winmm thread\n");
 
@@ -239,18 +254,28 @@ static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
 
     while (! TIME_TimeToDie) 
     {
-	sleep_time = TIME_MMSysTimeCallback(iData);
+        /* Sleep time should always be < 65536 anyway, safe to convert */
+        sleep_time = (int)TIME_MMSysTimeCallback(iData);
 
         if (sleep_time == 0)
             continue;
 
-        rc = WaitForSingleObject(TIME_hWakeEvent, sleep_time);
-        if (rc != WAIT_TIMEOUT && rc != WAIT_OBJECT_0)
-        {   
-            FIXME("Unexpected error %d(%d) in timer thread\n", rc, GetLastError());
-            break;
+        while ((ret = poll(&pfd, 1, sleep_time)) < 0)
+        {
+            if (errno != EINTR || errno != EAGAIN)
+            {
+                ERR("Unexpected error in poll: %s(%d)\n", strerror(errno), errno);
+                goto end;
+            }
         }
+
+        if (ret > 0) 
+            do {
+                ret = read(TIME_fdWake[0], readme, sizeof(*readme));
+            } while (ret > 0);
     }
+
+    end:
     TRACE("Exiting main winmm thread\n");
     return 0;
 }
@@ -261,10 +286,21 @@ static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
 void	TIME_MMTimeStart(void)
 {
     if (!TIME_hMMTimer) {
-	TIME_TimersList = NULL;
-        TIME_hWakeEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
+        TIME_TimersList = NULL;
+
+        if (pipe(TIME_fdWake) < 0)
+        {
+            TIME_fdWake[0] = TIME_fdWake[1] = -1;
+            ERR("Cannot create pipe: %s\n", strerror(errno));
+        }
+        else
+        {
+            fcntl(TIME_fdWake[0], F_SETFL, O_NONBLOCK);
+            fcntl(TIME_fdWake[1], F_SETFL, O_NONBLOCK);
+        }
+
         TIME_TimeToDie = FALSE;
-	TIME_hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, &WINMM_IData, 0, NULL);
+        TIME_hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, &WINMM_IData, 0, NULL);
         SetThreadPriority(TIME_hMMTimer, THREAD_PRIORITY_TIME_CRITICAL);
         InitializeCriticalSection(&TIME_cbcrst);
         TIME_cbcrst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM.TIME_cbcrst");
@@ -277,16 +313,18 @@ void	TIME_MMTimeStart(void)
 void	TIME_MMTimeStop(void)
 {
     if (TIME_hMMTimer) {
-
+        const char a='a';
         TIME_TimeToDie = TRUE;
-        SetEvent(TIME_hWakeEvent);
 
+        write(TIME_fdWake[1], &a, sizeof(a));
         /* FIXME: in the worst case, we're going to wait 65 seconds here :-( */
-	WaitForSingleObject(TIME_hMMTimer, INFINITE);
+        WaitForSingleObject(TIME_hMMTimer, INFINITE);
 
-	CloseHandle(TIME_hMMTimer);
-	CloseHandle(TIME_hWakeEvent);
-	TIME_hMMTimer = 0;
+        CloseHandle(TIME_hMMTimer);
+        close(TIME_fdWake[0]);
+        close(TIME_fdWake[1]);
+        TIME_fdWake[0] = TIME_fdWake[1] = -1;
+        TIME_hMMTimer = 0;
         TIME_cbcrst.DebugInfo->Spare[0] = 0;
         DeleteCriticalSection(&TIME_cbcrst);
         TIME_TimersList = NULL;
@@ -317,6 +355,7 @@ WORD	TIME_SetEventInternal(UINT wDelay, UINT wResol,
     WORD 		wNewID = 0;
     LPWINE_TIMERENTRY	lpNewTimer;
     LPWINE_TIMERENTRY	lpTimer;
+    const char c = 'c';
 
     TRACE("(%u, %u, %p, %08X, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
 
@@ -352,7 +391,7 @@ WORD	TIME_SetEventInternal(UINT wDelay, UINT wResol,
     LeaveCriticalSection(&WINMM_IData.cs);
 
     /* Wake the service thread in case there is work to be done */
-    SetEvent(TIME_hWakeEvent);
+    write(TIME_fdWake[1], &c, sizeof(c));
 
     TRACE("=> %u\n", wNewID + 1);
 
@@ -464,15 +503,6 @@ MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
  */
 DWORD WINAPI timeGetTime(void)
 {
-#if defined(COMMENTOUTPRIORTODELETING)
-    DWORD       count;
-
-    /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
-     * that lets mciavi32.dll run correctly
-     */
-    if (pFnReleaseThunkLock) pFnReleaseThunkLock(&count);
-    if (pFnRestoreThunkLock) pFnRestoreThunkLock(count);
-#endif
-
+    TRACE("\n");
     return GetTickCount();
 }
-- 
1.4.4.2



More information about the wine-patches mailing list