hw/xwin: Improve WM_ENDSESSION handling using separate messaging window thread
authorJon TURNEY <jon.turney@dronecode.org.uk>
Wed, 31 Aug 2011 20:35:14 +0000 (21:35 +0100)
committerJon TURNEY <jon.turney@dronecode.org.uk>
Tue, 23 Jul 2013 22:58:50 +0000 (23:58 +0100)
Currently, WM_ENDSESSION just calls GiveUp() to set the DE_TERMINATE flag. But
for the X server to exit cleanly, we also need the X server dispatch loop to be
unblocked so it can notice that DE_TERMINATE has been set and exit, removing
it's lock file and any unix domain socket.

It appears that the system will terminate the process when the last UI thread in
that process returns from processing WM_ENDSESSION for the last top-level
window.

Since WM_ENDSESSION appears to sent by the system via SendMessage()
(synchronously) and the wndproc is called to process it in the message thread
for that window (the X server thread), we can't easily terminate the X server
dispatch loop from inside the WM_ENDSESSION message processing.

So, create a messaging window, a hidden, top-level window, with a separate
thread to catch this message, and process it by calling GiveUp() and then
blocking on a mutex until the X server dispatch loop exits.

Also, notice when this is a shutdown cancel WM_ENDSESSION message and take no
action.

Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
Reviewed-by: Colin Harrison <colin.harrison@virgin.net>
hw/xwin/InitOutput.c
hw/xwin/Makefile.am
hw/xwin/win.h
hw/xwin/winglobals.c
hw/xwin/winglobals.h
hw/xwin/winmsgwindow.c [new file with mode: 0644]
hw/xwin/winwindow.h
hw/xwin/winwndproc.c

index 04c17b7..d6b5e4f 100644 (file)
@@ -186,6 +186,25 @@ ddxBeforeReset(void)
 }
 #endif
 
+int
+main(int argc, char *argv[], char *envp[])
+{
+    int iReturn;
+
+    /* Create & acquire the termination mutex */
+    iReturn = pthread_mutex_init(&g_pmTerminating, NULL);
+    if (iReturn != 0) {
+        ErrorF("ddxMain - pthread_mutex_init () failed: %d\n", iReturn);
+    }
+
+    iReturn = pthread_mutex_lock(&g_pmTerminating);
+    if (iReturn != 0) {
+        ErrorF("ddxMain - pthread_mutex_lock () failed: %d\n", iReturn);
+    }
+
+    return dix_main(argc, argv, envp);
+}
+
 /* See Porting Layer Definition - p. 57 */
 void
 ddxGiveUp(enum ExitCode error)
@@ -243,6 +262,19 @@ ddxGiveUp(enum ExitCode error)
 
     /* Tell Windows that we want to end the app */
     PostQuitMessage(0);
+
+    {
+        winDebug("ddxGiveUp - Releasing termination mutex\n");
+
+        int iReturn = pthread_mutex_unlock(&g_pmTerminating);
+
+        if (iReturn != 0) {
+            ErrorF("winMsgWindowProc - pthread_mutex_unlock () failed: %d\n",
+                   iReturn);
+        }
+    }
+
+    winDebug("ddxGiveUp - End\n");
 }
 
 /* See Porting Layer Definition - p. 57 */
@@ -962,6 +994,10 @@ InitOutput(ScreenInfo * pScreenInfo, int argc, char *argv[])
     /* Store the instance handle */
     g_hInstance = GetModuleHandle(NULL);
 
+    /* Create the messaging window */
+    if (serverGeneration == 1)
+        winCreateMsgWindowThread();
+
     /* Initialize each screen */
     for (i = 0; i < g_iNumScreens; ++i) {
         /* Initialize the screen */
index 90812ab..74a4243 100644 (file)
@@ -92,6 +92,7 @@ SRCS =        InitInput.c \
        winmonitors.c \
        winmouse.c \
        winmsg.c \
+       winmsgwindow.c \
        winmultiwindowclass.c \
        winmultiwindowicons.c \
        winprefs.c \
index fa774bc..dd6ed02 100644 (file)
@@ -1403,6 +1403,12 @@ winDoRandRScreenSetSize(ScreenPtr pScreen,
                         CARD16 height, CARD32 mmWidth, CARD32 mmHeight);
 
 /*
+ * winmsgwindow.c
+ */
+Bool
+winCreateMsgWindowThread(void);
+
+/*
  * END DDX and DIX Function Prototypes
  */
 
index b34d1a7..b3ca295 100644 (file)
@@ -78,6 +78,7 @@ Bool g_fNoHelpMessageBox = FALSE;
 Bool g_fSoftwareCursor = FALSE;
 Bool g_fSilentDupError = FALSE;
 Bool g_fNativeGl = TRUE;
+pthread_mutex_t g_pmTerminating = PTHREAD_MUTEX_INITIALIZER;
 
 #ifdef XWIN_CLIPBOARD
 /*
index d2e2ba2..e7e2f7e 100644 (file)
@@ -90,4 +90,6 @@ extern Bool g_fButton[3];
 extern Bool g_fNoConfigureWindow;
 #endif
 
+extern pthread_mutex_t g_pmTerminating;
+
 #endif                          /* WINGLOBALS_H */
diff --git a/hw/xwin/winmsgwindow.c b/hw/xwin/winmsgwindow.c
new file mode 100644 (file)
index 0000000..8067c69
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) Jon TURNEY 2011
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include "win.h"
+
+/*
+ * This is the messaging window, a hidden top-level window. We never do anything
+ * with it, but other programs may send messages to it.
+ */
+
+/*
+ * winMsgWindowProc - Window procedure for msg window
+ */
+
+static
+LRESULT CALLBACK
+winMsgWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
+{
+#if CYGDEBUG
+    winDebugWin32Message("winMsgWindowProc", hwnd, message, wParam, lParam);
+#endif
+
+    switch (message) {
+    case WM_ENDSESSION:
+        if (!wParam)
+            return 0;           /* shutdown is being cancelled */
+
+        /*
+           Send a WM_GIVEUP message to the X server thread so it wakes up if
+           blocked in select(), performs GiveUp(), and then notices that GiveUp()
+           has set the DE_TERMINATE flag so exits the msg dispatch loop.
+         */
+        {
+            ScreenPtr pScreen = screenInfo.screens[0];
+
+            winScreenPriv(pScreen);
+            PostMessage(pScreenPriv->hwndScreen, WM_GIVEUP, 0, 0);
+        }
+
+        /*
+           This process will be terminated by the system almost immediately
+           after the last thread with a message queue returns from processing
+           WM_ENDSESSION, so we cannot rely on any code executing after this
+           message is processed and need to wait here until ddxGiveUp() is called
+           and releases the termination mutex to guarantee that the lock file and
+           unix domain sockets have been removed
+
+           ofc, Microsoft doesn't document this under WM_ENDSESSION, you are supposed
+           to read the source of CRSS to find out how it works :-)
+
+           http://blogs.msdn.com/b/michen/archive/2008/04/04/application-termination-when-user-logs-off.aspx
+         */
+        {
+            int iReturn = pthread_mutex_lock(&g_pmTerminating);
+
+            if (iReturn != 0) {
+                ErrorF("winMsgWindowProc - pthread_mutex_lock () failed: %d\n",
+                       iReturn);
+            }
+            winDebug
+                ("winMsgWindowProc - WM_ENDSESSION termination lock acquired\n");
+        }
+
+        return 0;
+    }
+
+    return DefWindowProc(hwnd, message, wParam, lParam);
+}
+
+static HWND
+winCreateMsgWindow(void)
+{
+    HWND hwndMsg;
+    wATOM winClass;
+
+    // register window class
+    {
+        WNDCLASSEX wcx;
+
+        wcx.cbSize = sizeof(WNDCLASSEX);
+        wcx.style = CS_HREDRAW | CS_VREDRAW;
+        wcx.lpfnWndProc = winMsgWindowProc;
+        wcx.cbClsExtra = 0;
+        wcx.cbWndExtra = 0;
+        wcx.hInstance = g_hInstance;
+        wcx.hIcon = NULL;
+        wcx.hCursor = 0;
+        wcx.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
+        wcx.lpszMenuName = NULL;
+        wcx.lpszClassName = WINDOW_CLASS_X_MSG;
+        wcx.hIconSm = NULL;
+        winClass = RegisterClassEx(&wcx);
+    }
+
+    // Create the msg window.
+    hwndMsg = CreateWindowEx(0, // no extended styles
+                             WINDOW_CLASS_X_MSG,        // class name
+                             "XWin Msg Window", // window name
+                             WS_OVERLAPPEDWINDOW,       // overlapped window
+                             CW_USEDEFAULT,     // default horizontal position
+                             CW_USEDEFAULT,     // default vertical position
+                             CW_USEDEFAULT,     // default width
+                             CW_USEDEFAULT,     // default height
+                             (HWND) NULL,       // no parent or owner window
+                             (HMENU) NULL,      // class menu used
+                             GetModuleHandle(NULL),     // instance handle
+                             NULL);     // no window creation data
+
+    if (!hwndMsg) {
+        ErrorF("winCreateMsgWindow - Create msg window failed\n");
+        return NULL;
+    }
+
+    winDebug("winCreateMsgWindow - Created msg window hwnd 0x%x\n", hwndMsg);
+
+    return hwndMsg;
+}
+
+static void *
+winMsgWindowThreadProc(void *arg)
+{
+    HWND hwndMsg;
+
+    winDebug("winMsgWindowThreadProc - Hello\n");
+
+    hwndMsg = winCreateMsgWindow();
+    if (hwndMsg) {
+        MSG msg;
+
+        /* Pump the msg window message queue */
+        while (GetMessage(&msg, hwndMsg, 0, 0) > 0) {
+#if CYGDEBUG
+            winDebugWin32Message("winMsgWindowThread", msg.hwnd, msg.message,
+                                 msg.wParam, msg.lParam);
+#endif
+            DispatchMessage(&msg);
+        }
+    }
+
+    winDebug("winMsgWindowThreadProc - Exit\n");
+
+    return NULL;
+}
+
+Bool
+winCreateMsgWindowThread(void)
+{
+    pthread_t ptMsgWindowThreadProc;
+
+    /* Spawn a thread for the msg window  */
+    if (pthread_create(&ptMsgWindowThreadProc,
+                       NULL, winMsgWindowThreadProc, NULL)) {
+        /* Bail if thread creation failed */
+        ErrorF("winCreateMsgWindow - pthread_create failed.\n");
+        return FALSE;
+    }
+
+    return TRUE;
+}
index 25826ec..7e6bd56 100644 (file)
@@ -49,6 +49,7 @@
 #define WINDOW_TITLE_XDMCP     "%s:%s.%d"
 #define WIN_SCR_PROP           "cyg_screen_prop rl"
 #define WINDOW_CLASS_X         "cygwin/x X rl"
+#define WINDOW_CLASS_X_MSG      "cygwin/x X msg"
 #define WINDOW_TITLE_X         PROJECT_NAME " X"
 #define WIN_WINDOW_PROP                "cyg_window_prop_rl"
 #ifdef HAS_DEVWINDOWS
index c7509ea..f93072c 100644 (file)
@@ -1221,7 +1221,6 @@ winWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
         }
         break;
 
-    case WM_ENDSESSION:
     case WM_GIVEUP:
         /* Tell X that we are giving up */
 #ifdef XWIN_MULTIWINDOW