Index: /branches/libc-0.6/src/emx/src/lib/process/fmutex.c
===================================================================
--- /branches/libc-0.6/src/emx/src/lib/process/fmutex.c	(revision 3848)
+++ /branches/libc-0.6/src/emx/src/lib/process/fmutex.c	(revision 3849)
@@ -148,5 +148,8 @@
         for (;;)
         {
-            rc = DosWaitEventSem(sem->hev, sem->flags & _FMC_SHARED ? SEM_INDEFINITE_WAIT : 3000);
+            ULONG cMsWait = sem->flags & _FMC_SHARED ? SEM_INDEFINITE_WAIT : 3000;
+            if (fibIsInExit())
+                cMsWait = 250;
+            rc = DosWaitEventSem(sem->hev, cMsWait);
             if (rc == ERROR_INTERRUPT)
             {
@@ -165,4 +168,5 @@
             if (rc != ERROR_TIMEOUT)
                 break;
+
             /*
              * Deadlock detection - check if owner is around.
@@ -179,4 +183,5 @@
             }
         }
+
         FS_RESTORE();
         if (rc != 0)
@@ -284,6 +289,9 @@
         for (;;)
         {
-            DosExitMustComplete(&ulNesting);
-            rc = DosWaitEventSem(sem->hev, sem->flags & _FMC_SHARED ? SEM_INDEFINITE_WAIT : 3000);
+            ULONG cMsWait = sem->flags & _FMC_SHARED ? SEM_INDEFINITE_WAIT : 3000;
+            if (fibIsInExit())
+                cMsWait = 250;
+            DosExitMustComplete(&ulNesting);
+            rc = DosWaitEventSem(sem->hev, cMsWait);
             DosEnterMustComplete(&ulNesting);
             if (rc == ERROR_INTERRUPT)
@@ -303,4 +311,5 @@
             if (rc != ERROR_TIMEOUT)
                 break;
+
             /*
              * Deadlock detection - check if owner is around.
@@ -319,4 +328,5 @@
             }
         }
+
         FS_RESTORE();
         if (rc != 0)
@@ -331,11 +341,12 @@
 static void __fmutex_deadlock(_fmutex *pSem, const char *pszMsg)
 {
-    __libc_Back_panic(0, NULL,
-                      "fmutex deadlock: %s\n"
-                      "%x: Owner=%x Self=%x fs=%x flags=%x hev=%x\n"
-                      "            Desc=\"%s\"\n",
-                      pszMsg,
-                      pSem, pSem->Owner, fibGetTidPid(), pSem->fs, pSem->flags, pSem->hev,
-                      pSem->pszDesc);
+    if (!fibIsInExit())
+        __libc_Back_panic(0, NULL,
+                          "fmutex deadlock: %s\n"
+                          "%x: Owner=%x Self=%x fs=%x flags=%x hev=%x\n"
+                          "            Desc=\"%s\"\n",
+                          pszMsg,
+                          pSem, pSem->Owner, fibGetTidPid(), pSem->fs, pSem->flags, pSem->hev,
+                          pSem->pszDesc);
 }
 
@@ -377,5 +388,5 @@
     {
         FS_RESTORE();
-        LIBCLOG_RETURN_UINT(rc);
+        LIBCLOG_ERROR_RETURN_UINT(rc);
     }
 
Index: /trunk/libc/src/libc/process/os2/fmutex-os2.c
===================================================================
--- /trunk/libc/src/libc/process/os2/fmutex-os2.c	(revision 3848)
+++ /trunk/libc/src/libc/process/os2/fmutex-os2.c	(revision 3849)
@@ -17,4 +17,5 @@
 #define __LIBC_LOG_GROUP __LIBC_LOG_GRP_MUTEX
 #include <InnoTekLIBC/logstrict.h>
+#include <InnoTekLIBC/thread.h>
 #include <InnoTekLIBC/backend.h>
 
@@ -147,5 +148,8 @@
         for (;;)
         {
-            rc = DosWaitEventSem(sem->hev, sem->flags & _FMC_SHARED ? SEM_INDEFINITE_WAIT : 3000);
+            ULONG cMsWait = sem->flags & _FMC_SHARED ? SEM_INDEFINITE_WAIT : 3000;
+            if (fibIsInExit())
+                cMsWait = 250;
+            rc = DosWaitEventSem(sem->hev, cMsWait);
             if (rc == ERROR_INTERRUPT)
             {
@@ -164,4 +168,5 @@
             if (rc != ERROR_TIMEOUT)
                 break;
+
             /*
              * Deadlock detection - check if owner is around.
@@ -178,4 +183,5 @@
             }
         }
+
         FS_RESTORE();
         if (rc != 0)
@@ -200,4 +206,16 @@
     FS_SAVE_LOAD();
     DosEnterMustComplete(&ulNesting);
+#if 0 /** @todo lost signal / kernel boundrary */
+    if (ulNesting == 1) /* This is a hack to catch lost poke signals. */
+    {
+        __LIBC_PTHREAD pThrd = __libc_threadCurrentNoAuto();
+        if (pThrd && pThrd->fSigBeingPoked)
+        {
+            DosExitMustComplete(&ulNesting);
+            __libc_Back_signalLostPoke();
+            DosEnterMustComplete(&ulNesting);
+        }
+    }
+#endif
     fs = __cxchg(&sem->fs, _FMS_OWNED_SIMPLE);
     if (fs == _FMS_AVAILABLE)
@@ -214,4 +232,5 @@
         if (rc)
         {
+            DosExitMustComplete(&ulNesting);
             LIBC_ASSERTM_FAILED("Failed to create event semaphore for fmutex '%s', rc=%d. flags=%#x\n", sem->pszDesc, rc, sem->flags);
             FS_RESTORE();
@@ -272,6 +291,9 @@
         for (;;)
         {
-            DosExitMustComplete(&ulNesting);
-            rc = DosWaitEventSem(sem->hev, sem->flags & _FMC_SHARED ? SEM_INDEFINITE_WAIT : 3000);
+            ULONG cMsWait = sem->flags & _FMC_SHARED ? SEM_INDEFINITE_WAIT : 3000;
+            if (fibIsInExit())
+                cMsWait = 250;
+            DosExitMustComplete(&ulNesting);
+            rc = DosWaitEventSem(sem->hev, cMsWait);
             DosEnterMustComplete(&ulNesting);
             if (rc == ERROR_INTERRUPT)
@@ -291,4 +313,5 @@
             if (rc != ERROR_TIMEOUT)
                 break;
+
             /*
              * Deadlock detection - check if owner is around.
@@ -307,4 +330,5 @@
             }
         }
+
         FS_RESTORE();
         if (rc != 0)
@@ -319,11 +343,12 @@
 static void __fmutex_deadlock(_fmutex *pSem, const char *pszMsg)
 {
-    __libc_Back_panic(0, NULL,
-                      "fmutex deadlock: %s\n"
-                      "%x: Owner=%x Self=%x fs=%x flags=%x hev=%x\n"
-                      "            Desc=\"%s\"\n",
-                      pszMsg,
-                      pSem, pSem->Owner, fibGetTidPid(), pSem->fs, pSem->flags, pSem->hev,
-                      pSem->pszDesc);
+    if (!fibIsInExit())
+        __libc_Back_panic(0, NULL,
+                          "fmutex deadlock: %s\n"
+                          "%x: Owner=%x Self=%x fs=%x flags=%x hev=%x\n"
+                          "            Desc=\"%s\"\n",
+                          pszMsg,
+                          pSem, pSem->Owner, fibGetTidPid(), pSem->fs, pSem->flags, pSem->hev,
+                          pSem->pszDesc);
 }
 
@@ -332,21 +357,25 @@
     LIBCLOG_ENTER("sem=%p{.pszDesc=%s}\n", (void *)sem, sem->pszDesc);
     ULONG ulNesting;
+    int rc = 0;
     __atomic_xchg(&sem->Owner, 0);
     signed char fs = __cxchg(&sem->fs, _FMS_AVAILABLE);
-    if (fs != _FMS_OWNED_HARD)
-    {
-        FS_VAR_SAVE_LOAD();
-        DosExitMustComplete(&ulNesting);
-        FS_RESTORE();
-        LIBCLOG_RETURN_UINT(0);
-    }
-    else
-    {
-        int rc = __fmutex_release_internal (sem);
-        FS_VAR_SAVE_LOAD();
-        DosExitMustComplete(&ulNesting);
-        FS_RESTORE();
-        LIBCLOG_RETURN_UINT(rc);
-    }
+    if (fs == _FMS_OWNED_HARD)
+        rc = __fmutex_release_internal (sem);
+
+    FS_VAR_SAVE_LOAD();
+    DosExitMustComplete(&ulNesting);
+    FS_RESTORE();
+
+    /* This is a hack to catch lost poke signals. */
+#if 0 /** @todo lost signal / kernel boundrary */
+    if (!ulNesting)
+    {
+        __LIBC_PTHREAD pThrd = __libc_threadCurrentNoAuto();
+        if (pThrd && pThrd->fSigBeingPoked)
+            __libc_Back_signalLostPoke();
+    }
+#endif
+
+    LIBCLOG_RETURN_UINT(rc);
 }
 
