1 /*
2  * Copyright (c) 2022 Meta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_LIB_POSIX_POSIX_INTERNAL_H_
8 #define ZEPHYR_LIB_POSIX_POSIX_INTERNAL_H_
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/posix/pthread.h>
15 #include <zephyr/sys/dlist.h>
16 #include <zephyr/sys/slist.h>
17 
18 /*
19  * Bit used to mark a pthread object as initialized. Initialization status is
20  * verified (against internal status) in lock / unlock / destroy functions.
21  */
22 #define PTHREAD_OBJ_MASK_INIT 0x80000000
23 
24 struct posix_thread {
25 	struct k_thread thread;
26 
27 	/* List node for ready_q, run_q, or done_q */
28 	sys_dnode_t q_node;
29 
30 	/* List of keys that thread has called pthread_setspecific() on */
31 	sys_slist_t key_list;
32 
33 	/* Exit status */
34 	void *retval;
35 
36 	/* Pthread cancellation */
37 	uint8_t cancel_state;
38 	bool cancel_pending;
39 
40 	/* Detach state */
41 	uint8_t detachstate;
42 
43 	/* Queue ID (internal-only) */
44 	uint8_t qid;
45 };
46 
47 typedef struct pthread_key_obj {
48 	/* List of pthread_key_data objects that contain thread
49 	 * specific data for the key
50 	 */
51 	sys_slist_t key_data_l;
52 
53 	/* Optional destructor that is passed to pthread_key_create() */
54 	void (*destructor)(void *value);
55 } pthread_key_obj;
56 
57 typedef struct pthread_thread_data {
58 	sys_snode_t node;
59 
60 	/* Key and thread specific data passed to pthread_setspecific() */
61 	pthread_key_obj *key;
62 	void *spec_data;
63 } pthread_thread_data;
64 
is_pthread_obj_initialized(uint32_t obj)65 static inline bool is_pthread_obj_initialized(uint32_t obj)
66 {
67 	return (obj & PTHREAD_OBJ_MASK_INIT) != 0;
68 }
69 
mark_pthread_obj_initialized(uint32_t obj)70 static inline uint32_t mark_pthread_obj_initialized(uint32_t obj)
71 {
72 	return obj | PTHREAD_OBJ_MASK_INIT;
73 }
74 
mark_pthread_obj_uninitialized(uint32_t obj)75 static inline uint32_t mark_pthread_obj_uninitialized(uint32_t obj)
76 {
77 	return obj & ~PTHREAD_OBJ_MASK_INIT;
78 }
79 
80 struct posix_thread *to_posix_thread(pthread_t pth);
81 
82 /* get and possibly initialize a posix_mutex */
83 struct k_mutex *to_posix_mutex(pthread_mutex_t *mu);
84 
85 #endif
86