Index | Thread | Search

From:
Christian Schulte <cs@schulte.it>
Subject:
libstdthreads: mtx_destroy/cnd_destroy
To:
ports@openbsd.org
Date:
Sun, 11 Oct 2026 01:15:26 +0200

Download raw body.

Thread
  • Christian Schulte:

    libstdthreads: mtx_destroy/cnd_destroy

Hi @ports,

I am a bit puzzled by this and just wanted to ask about your opinion
before taking a closer look at cnd_destroy. See the comment in the
patch. WDYT?

$ cat /usr/ports/devel/libstdthreads/patches/patch-mtx_c
Index: mtx.c
--- mtx.c.orig
+++ mtx.c
@@ -31,14 +31,46 @@
 
 #include <errno.h>
 #include <pthread.h>
+#include <stdlib.h>
 
 #include "threads.h"
 
 void
 mtx_destroy(mtx_t *mtx)
 {
-
-	(void)pthread_mutex_destroy(mtx);
+	/*
+	 * ISO/IEC 9899:202y (en) 7.30.4.2 The mtx_destroy function
+	 *
+	 *   The mtx_destroy function releases any resources used by the
+	 *   mutex pointed to by mtx. Attempting to destroy a locked mutex,
+	 *   or a mutex that is being used in a cnd_timedwait(), or cnd_wait()
+	 *   call by another thread, results in undefined behavior.
+	 *   The mtx_destroy function returns no value.
+	 *
+	 * IEEE Std 1003.1-2024 pthread_mutex_destroy
+	 *
+	 *   Attempting to destroy a locked mutex, or a mutex that another
+	 *   thread is attempting to lock, or a mutex that is being used in a
+	 *   pthread_cond_clockwait(), pthread_cond_timedwait(), or
+	 *   pthread_cond_wait() call by another thread, results in undefined
+	 *   behavior. If successful, the pthread_mutex_destroy() function shall
+	 *   return zero; otherwise, an error number shall be returned to indicate
+	 *   the error.
+	 *
+	 * OpenBSD
+	 *
+	 *   The pthread_mutex_destroy() function returns EINVAL, if mtx == NULL
+	 *   and EBUSY, if the mutex is locked. It will already write a message to
+	 *   stderr before returning EBUSY reading:
+	 *    "pthread_mutex_destroy on mutex with waiters!"
+	 *
+	 *   Instead of silently letting the application run in such undefined state,
+	 *   any error returned by pthread_mutex_destroy() will abort the process as
+	 *   that is the only defined state to guarantee when pthread_mutex_destroy()
+	 *   fails.
+	 */
+	if (pthread_mutex_destroy(mtx) != 0)
+	    abort();
 }
 
 int