From 5e7f670c91a7b24dc01dedece337acbb18c746d2 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Wed, 24 Sep 2025 08:52:58 +0000 Subject: Add a panic to MutexDestroy if the mutex is still owned --- unix/tclUnixThrd.c | 11 ++++++++++- win/tclWinThrd.c | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/unix/tclUnixThrd.c b/unix/tclUnixThrd.c index da88cdb..ae6d96a 100644 --- a/unix/tclUnixThrd.c +++ b/unix/tclUnixThrd.c @@ -95,8 +95,17 @@ static void PMutexDestroy( PMutex *pmutexPtr) { -#if defined(HAVE_PTHREAD_SPIN_LOCK) && !defined(HAVE_STDATOMIC_H) +#ifdef HAVE_STDATOMIC_H + if (__atomic_load_n(&pmutexPtr->thread, __ATOMIC_SEQ_CST) != 0) { + Tcl_Panic("mutex still owned"); + } +#else + if (mutexPtr->thread != 0) { + Tcl_Panic("mutex still owned"); + } +# if defined(HAVE_PTHREAD_SPIN_LOCK) pthread_spin_destroy(&pmutexPtr->lock); +# endif #endif pthread_mutex_destroy(&pmutexPtr->mutex); } diff --git a/win/tclWinThrd.c b/win/tclWinThrd.c index ab4a3c9..4afa334 100644 --- a/win/tclWinThrd.c +++ b/win/tclWinThrd.c @@ -554,6 +554,9 @@ static void WMutexDestroy( WMutex *wmutexPtr) { + if (InterlockedOr(&wmPtr->thread, 0) != 0) { + Tcl_Panic("mutex still owned"); + } DeleteCriticalSection(&wmutexPtr->crit); } -- cgit v0.12