From 2dab009e761edf8a2e56c2a2138f21e3600b22f1 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Wed, 25 Nov 2020 10:49:39 +0300 Subject: [PATCH] mdbx-windows: handling `EXCEPTION_POSSIBLE_DEADLOCK`. Change-Id: If42c7833e9c4e02fef25634e69c0bd2e926686c1 --- .github/actions/spelling/expect.txt | 1 + ChangeLog.md | 1 + src/lck-windows.c | 10 +++++++++- src/ntdll.def | 2 ++ src/osal.c | 10 +++++++++- src/osal.h | 1 + 6 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index 435f969b..75cdff13 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -447,6 +447,7 @@ EWOULDBLOCK Exa exactkey exactp +excpt exe exename exherbo diff --git a/ChangeLog.md b/ChangeLog.md index e7cf7953..90cd7f04 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -58,6 +58,7 @@ Fixes: - Fixed large pages checking (for compatibility and to avoid false-positive errors from `mdbx_chk`). - Added workaround for Wine (https://github.com/miranda-ng/miranda-ng/issues/1209). - Fixed `ERROR_NOT_SUPPORTED` while opening DB by UNC pathnames (https://github.com/miranda-ng/miranda-ng/issues/2627). + - Added handling `EXCEPTION_POSSIBLE_DEADLOCK` condition for Windows. ## v0.9.1 2020-09-30 diff --git a/src/lck-windows.c b/src/lck-windows.c index 0ddd8c7d..3bcf347d 100644 --- a/src/lck-windows.c +++ b/src/lck-windows.c @@ -144,7 +144,15 @@ int mdbx_txn_lock(MDBX_env *env, bool dontwait) { if (!TryEnterCriticalSection(&env->me_windowsbug_lock)) return MDBX_BUSY; } else { - EnterCriticalSection(&env->me_windowsbug_lock); + __try { + EnterCriticalSection(&env->me_windowsbug_lock); + } + __except ((GetExceptionCode() == + 0xC0000194 /* STATUS_POSSIBLE_DEADLOCK / EXCEPTION_POSSIBLE_DEADLOCK */) + ? EXCEPTION_EXECUTE_HANDLER + : EXCEPTION_CONTINUE_SEARCH) { + return ERROR_POSSIBLE_DEADLOCK; + } } if ((env->me_flags & MDBX_EXCLUSIVE) || diff --git a/src/ntdll.def b/src/ntdll.def index e3a6e33c..f974ea63 100644 --- a/src/ntdll.def +++ b/src/ntdll.def @@ -1242,3 +1242,5 @@ wcsstr wcstol wcstombs wcstoul +__C_specific_handler +_except_handler4_common diff --git a/src/osal.c b/src/osal.c index f263346b..da99e618 100644 --- a/src/osal.c +++ b/src/osal.c @@ -492,7 +492,15 @@ MDBX_INTERNAL_FUNC int mdbx_fastmutex_destroy(mdbx_fastmutex_t *fastmutex) { MDBX_INTERNAL_FUNC int mdbx_fastmutex_acquire(mdbx_fastmutex_t *fastmutex) { #if defined(_WIN32) || defined(_WIN64) - EnterCriticalSection(fastmutex); + __try { + EnterCriticalSection(fastmutex); + } __except ( + (GetExceptionCode() == + 0xC0000194 /* STATUS_POSSIBLE_DEADLOCK / EXCEPTION_POSSIBLE_DEADLOCK */) + ? EXCEPTION_EXECUTE_HANDLER + : EXCEPTION_CONTINUE_SEARCH) { + return ERROR_POSSIBLE_DEADLOCK; + } return MDBX_SUCCESS; #else return pthread_mutex_lock(fastmutex); diff --git a/src/osal.h b/src/osal.h index 3212976a..a1c8c1f6 100644 --- a/src/osal.h +++ b/src/osal.h @@ -146,6 +146,7 @@ __extern_C key_t ftok(const char *, int); #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif +#include #include #include #include