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>
This commit is contained in:
parent
84c09433b5
commit
1777a33558
2 changed files with 12 additions and 12 deletions
|
@ -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 @@ struct posix_mutex *to_posix_mutex(pthread_mutex_t *mu);
|
|||
/* 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
|
||||
|
|
|
@ -33,7 +33,7 @@ SYS_BITARRAY_DEFINE_STATIC(posix_mutex_bitarray, CONFIG_MAX_PTHREAD_MUTEX_COUNT)
|
|||
* 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 posix_mutex_to_offset(struct posix_mutex *m)
|
|||
|
||||
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 @@ struct posix_mutex *get_posix_mutex(pthread_mutex_t mu)
|
|||
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 @@ struct posix_mutex *to_posix_mutex(pthread_mutex_t *mu)
|
|||
}
|
||||
|
||||
/* 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];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue