lib: posix: internal: use a more generic INIT mask and inlines
Previously `PTHREAD_MUTEX_MASK_INIT` was used to mark a
`pthread_mutex_t` as initialized.
The same needs to be done for `pthread_cond_t` and likely others.
Rather than copy-pasting that and a number of inlines that
duplicate the same functionality, simply make it more generic.
Signed-off-by: Chris Friedt <cfriedt@meta.com>
diff --git a/lib/posix/posix_internal.h b/lib/posix/posix_internal.h
index c0bfba7..01055b7 100644
--- a/lib/posix/posix_internal.h
+++ b/lib/posix/posix_internal.h
@@ -8,10 +8,10 @@
#define ZEPHYR_LIB_POSIX_POSIX_INTERNAL_H_
/*
- * Bit used to mark a pthread_mutex_t as initialized. Initialization status is
+ * Bit used to mark a pthread object as initialized. Initialization status is
* verified (against internal status) in lock / unlock / destroy functions.
*/
-#define PTHREAD_MUTEX_MASK_INIT 0x80000000
+#define PTHREAD_OBJ_MASK_INIT 0x80000000
struct posix_mutex {
k_tid_t owner;
@@ -59,19 +59,19 @@
/* get a previously initialized posix_mutex */
struct posix_mutex *get_posix_mutex(pthread_mutex_t mut);
-static inline bool is_pthread_mutex_initialized(pthread_mutex_t mut)
+static inline bool is_pthread_obj_initialized(uint32_t obj)
{
- return (mut & PTHREAD_MUTEX_MASK_INIT) != 0;
+ return (obj & PTHREAD_OBJ_MASK_INIT) != 0;
}
-static inline pthread_mutex_t mark_pthread_mutex_initialized(pthread_mutex_t mut)
+static inline uint32_t mark_pthread_obj_initialized(uint32_t obj)
{
- return mut | PTHREAD_MUTEX_MASK_INIT;
+ return obj | PTHREAD_OBJ_MASK_INIT;
}
-static inline pthread_mutex_t mark_pthread_mutex_uninitialized(pthread_mutex_t mut)
+static inline uint32_t mark_pthread_obj_uninitialized(uint32_t obj)
{
- return mut & ~PTHREAD_MUTEX_MASK_INIT;
+ return obj & ~PTHREAD_OBJ_MASK_INIT;
}
#endif
diff --git a/lib/posix/pthread_mutex.c b/lib/posix/pthread_mutex.c
index 29a878f..17abffe 100644
--- a/lib/posix/pthread_mutex.c
+++ b/lib/posix/pthread_mutex.c
@@ -33,7 +33,7 @@
* perspective of the application). With a linear space, this means that
* the theoretical pthread_mutex_t range is [0,2147483647].
*/
-BUILD_ASSERT(CONFIG_MAX_PTHREAD_MUTEX_COUNT < PTHREAD_MUTEX_MASK_INIT,
+BUILD_ASSERT(CONFIG_MAX_PTHREAD_MUTEX_COUNT < PTHREAD_OBJ_MASK_INIT,
"CONFIG_MAX_PTHREAD_MUTEX_COUNT is too high");
static inline size_t posix_mutex_to_offset(struct posix_mutex *m)
@@ -43,7 +43,7 @@
static inline size_t to_posix_mutex_idx(pthread_mutex_t mut)
{
- return mark_pthread_mutex_uninitialized(mut);
+ return mark_pthread_obj_uninitialized(mut);
}
struct posix_mutex *get_posix_mutex(pthread_mutex_t mu)
@@ -52,7 +52,7 @@
size_t bit = to_posix_mutex_idx(mu);
/* if the provided mutex does not claim to be initialized, its invalid */
- if (!is_pthread_mutex_initialized(mu)) {
+ if (!is_pthread_obj_initialized(mu)) {
return NULL;
}
@@ -85,7 +85,7 @@
}
/* Record the associated posix_mutex in mu and mark as initialized */
- *mu = mark_pthread_mutex_initialized(bit);
+ *mu = mark_pthread_obj_initialized(bit);
/* Initialize the posix_mutex */
m = &posix_mutex_pool[bit];