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 	/* Dynamic stack */
34 	k_thread_stack_t *dynamic_stack;
35 
36 	/* Exit status */
37 	void *retval;
38 
39 	/* Pthread cancellation */
40 	uint8_t cancel_state;
41 	bool cancel_pending;
42 
43 	/* Detach state */
44 	uint8_t detachstate;
45 
46 	/* Queue ID (internal-only) */
47 	uint8_t qid;
48 };
49 
50 typedef struct pthread_key_obj {
51 	/* List of pthread_key_data objects that contain thread
52 	 * specific data for the key
53 	 */
54 	sys_slist_t key_data_l;
55 
56 	/* Optional destructor that is passed to pthread_key_create() */
57 	void (*destructor)(void *value);
58 } pthread_key_obj;
59 
60 typedef struct pthread_thread_data {
61 	sys_snode_t node;
62 
63 	/* Key and thread specific data passed to pthread_setspecific() */
64 	pthread_key_obj *key;
65 	void *spec_data;
66 } pthread_thread_data;
67 
is_pthread_obj_initialized(uint32_t obj)68 static inline bool is_pthread_obj_initialized(uint32_t obj)
69 {
70 	return (obj & PTHREAD_OBJ_MASK_INIT) != 0;
71 }
72 
mark_pthread_obj_initialized(uint32_t obj)73 static inline uint32_t mark_pthread_obj_initialized(uint32_t obj)
74 {
75 	return obj | PTHREAD_OBJ_MASK_INIT;
76 }
77 
mark_pthread_obj_uninitialized(uint32_t obj)78 static inline uint32_t mark_pthread_obj_uninitialized(uint32_t obj)
79 {
80 	return obj & ~PTHREAD_OBJ_MASK_INIT;
81 }
82 
83 struct posix_thread *to_posix_thread(pthread_t pth);
84 
85 /* get and possibly initialize a posix_mutex */
86 struct k_mutex *to_posix_mutex(pthread_mutex_t *mu);
87 
88 #endif
89