[PATCH v2 03/20] lib/cobalt: Introduce mutex.h

Florian Bezdeka <[email protected]>
Newsgroups dev.linux.lists.xenomai
Message-ID <20260302-wip-flo-fix-time64-32bit-native-v2-3-7caefce29bcd@siemens.com>
Some code will be re-used later by the time64_t compile unit. Move that
code into mutex.h, so code duplication is avoided.

No modifications to the code itself.

Signed-off-by: Florian Bezdeka <[email protected]>
---
 lib/cobalt/Makefile.am |  1 +
 lib/cobalt/mutex.c     | 82 ++----------------------------------------
 lib/cobalt/mutex.h     | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 101 insertions(+), 79 deletions(-)

diff --git a/lib/cobalt/Makefile.am b/lib/cobalt/Makefile.am
index aa4ba2b92020e661d4906844ddee62aecc30de07..576fb980f72b342e118bfacb061b5634bda420a2 100644
--- a/lib/cobalt/Makefile.am
+++ b/lib/cobalt/Makefile.am
@@ -4,6 +4,7 @@ noinst_HEADERS =	\
 	cond.h		\
 	current.h	\
 	mq.h		\
+	mutex.h		\
 	umm.h		\
 	internal.h
 
diff --git a/lib/cobalt/mutex.c b/lib/cobalt/mutex.c
index 62cdb462d7b92b177cc44777e07f69459fb6d195..390305cddc94985ebed354c895783e24652c1025 100644
--- a/lib/cobalt/mutex.c
+++ b/lib/cobalt/mutex.c
@@ -20,8 +20,10 @@
 #include <string.h>
 #include <pthread.h>
 #include <asm/xenomai/syscall.h>
+
 #include "current.h"
 #include "internal.h"
+#include "mutex.h"
 
 /**
  * @ingroup cobalt_api
@@ -61,7 +63,7 @@
 
 static pthread_mutexattr_t cobalt_default_mutexattr;
 static union cobalt_mutex_union cobalt_autoinit_mutex_union;
-static pthread_mutex_t *const cobalt_autoinit_mutex =
+pthread_mutex_t *const cobalt_autoinit_mutex =
 	&cobalt_autoinit_mutex_union.native_mutex;
 
 void cobalt_mutex_init(void)
@@ -167,84 +169,6 @@ COBALT_IMPL(int, pthread_mutex_init, (pthread_mutex_t *mutex,
 	return err;
 }
 
-/**
- * Test if a mutex structure contains a valid autoinitializer.
- *
- * @return the mutex type on success,
- * @return -1 if not in supported autoinitializer state
- */
-static int __attribute__((cold))
-	cobalt_mutex_autoinit_type(const pthread_mutex_t *mutex)
-{
-	static const pthread_mutex_t mutex_initializers[] = {
-#if HAVE_DECL_PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
-		PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP,
-#endif
-#if HAVE_DECL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
-		PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
-#endif
-		PTHREAD_MUTEX_INITIALIZER
-	};
-	static const int mutex_types[] = {
-#if HAVE_DECL_PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
-		PTHREAD_MUTEX_ERRORCHECK_NP,
-#endif
-#if HAVE_DECL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
-		PTHREAD_MUTEX_RECURSIVE_NP,
-#endif
-		PTHREAD_MUTEX_DEFAULT
-	};
-	int i;
-
-	for (i = ARRAY_SIZE(mutex_types); i > 0; --i) {
-		if (memcmp(mutex, &mutex_initializers[i - 1],
-				sizeof(mutex_initializers[0])) == 0)
-			return mutex_types[i - 1];
-	}
-	return -1;
-}
-
-static int __attribute__((cold))
-	cobalt_mutex_doautoinit(union cobalt_mutex_union *umutex)
-{
-	struct cobalt_mutex_shadow *_mutex = &umutex->shadow_mutex;
-	int err __attribute__((unused));
-	pthread_mutexattr_t mattr;
-	int ret = 0, type;
-
-	type = cobalt_mutex_autoinit_type(&umutex->native_mutex);
-	if (type < 0)
-		return EINVAL;
-
-	pthread_mutexattr_init(&mattr);
-	pthread_mutexattr_settype(&mattr, type);
-	err = __COBALT(pthread_mutex_lock(cobalt_autoinit_mutex));
-	if (err) {
-		ret = err;
-		goto out;
-	}
-	if (_mutex->magic != COBALT_MUTEX_MAGIC)
-		ret = __COBALT(pthread_mutex_init(&umutex->native_mutex,
-			&mattr));
-	err = __COBALT(pthread_mutex_unlock(cobalt_autoinit_mutex));
-	if (err) {
-		if (ret == 0)
-			ret = err;
-	}
-
-  out:
-	pthread_mutexattr_destroy(&mattr);
-
-	return ret;
-}
-
-static inline int cobalt_mutex_autoinit(union cobalt_mutex_union *umutex)
-{
-	if (umutex->shadow_mutex.magic != COBALT_MUTEX_MAGIC)
-		return cobalt_mutex_doautoinit(umutex);
-	return 0;
-}
-
 /**
  * Destroy a mutex.
  *
diff --git a/lib/cobalt/mutex.h b/lib/cobalt/mutex.h
new file mode 100644
index 0000000000000000000000000000000000000000..728088bf57cf665bcc0fb4aae44561fc131f6790
--- /dev/null
+++ b/lib/cobalt/mutex.h
@@ -0,0 +1,97 @@
+/* SPDX-License-Identifier: LGPL-2.0-or-later */
+
+/*
+ * Copyright (C) 2026 Florian Bezdeka <[email protected]>.
+ * Copyright (C) 2005 Philippe Gerum <[email protected]>.
+ */
+
+#ifndef _LIB_COBALT_MUTEX_H
+#define _LIB_COBALT_MUTEX_H
+
+#include <boilerplate/ancillaries.h>
+#include <cobalt/uapi/mutex.h>
+
+#include <pthread.h>
+
+extern pthread_mutex_t *const cobalt_autoinit_mutex;
+
+/**
+ * Test if a mutex structure contains a valid autoinitializer.
+ *
+ * @return the mutex type on success,
+ * @return -1 if not in supported autoinitializer state
+ */
+static int __attribute__((cold))
+cobalt_mutex_autoinit_type(const pthread_mutex_t *mutex)
+{
+	static const pthread_mutex_t mutex_initializers[] = {
+#if HAVE_DECL_PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
+		PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP,
+#endif
+#if HAVE_DECL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
+		PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
+#endif
+		PTHREAD_MUTEX_INITIALIZER
+	};
+	static const int mutex_types[] = {
+#if HAVE_DECL_PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
+		PTHREAD_MUTEX_ERRORCHECK_NP,
+#endif
+#if HAVE_DECL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
+		PTHREAD_MUTEX_RECURSIVE_NP,
+#endif
+		PTHREAD_MUTEX_DEFAULT
+	};
+	int i;
+
+	for (i = ARRAY_SIZE(mutex_types); i > 0; --i) {
+		if (memcmp(mutex, &mutex_initializers[i - 1],
+			   sizeof(mutex_initializers[0])) == 0)
+			return mutex_types[i - 1];
+	}
+	return -1;
+}
+
+static int __attribute__((cold))
+cobalt_mutex_doautoinit(union cobalt_mutex_union *umutex)
+{
+	struct cobalt_mutex_shadow *_mutex = &umutex->shadow_mutex;
+	int err __attribute__((unused));
+	pthread_mutexattr_t mattr;
+	int ret = 0, type;
+
+	type = cobalt_mutex_autoinit_type(&umutex->native_mutex);
+	if (type < 0)
+		return EINVAL;
+
+	pthread_mutexattr_init(&mattr);
+	pthread_mutexattr_settype(&mattr, type);
+	err = __COBALT(pthread_mutex_lock(cobalt_autoinit_mutex));
+	if (err) {
+		ret = err;
+		goto out;
+	}
+	if (_mutex->magic != COBALT_MUTEX_MAGIC)
+		ret = __COBALT(
+			pthread_mutex_init(&umutex->native_mutex, &mattr));
+	err = __COBALT(pthread_mutex_unlock(cobalt_autoinit_mutex));
+	if (err) {
+		if (ret == 0)
+			ret = err;
+	}
+
+out:
+	pthread_mutexattr_destroy(&mattr);
+
+	return ret;
+}
+
+static inline int cobalt_mutex_autoinit(union cobalt_mutex_union *umutex)
+{
+	if (umutex->shadow_mutex.magic != COBALT_MUTEX_MAGIC)
+		return cobalt_mutex_doautoinit(umutex);
+
+	return 0;
+}
+
+#endif //_LIB_COBALT_MUTEX_H

-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.