1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /**   ThreadX Component                                                   */
16 /**                                                                       */
17 /**   POSIX Compliancy Wrapper (POSIX)                                    */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /**************************************************************************/
23 /*                                                                        */
24 /*  EKP DEFINITIONS                                        RELEASE        */
25 /*                                                                        */
26 /*    px_int.h                                            PORTABLE C      */
27 /*                                                           6.2.0        */
28 /*  AUTHOR                                                                */
29 /*                                                                        */
30 /*    William E. Lamie, Microsoft Corporation                             */
31 /*                                                                        */
32 /*  DESCRIPTION                                                           */
33 /*                                                                        */
34 /*    This file defines the constants, structures, etc.needed to          */
35 /*    implement the Evacuation Kit for POSIX Users (POSIX)                */
36 /*                                                                        */
37 /*                                                                        */
38 /*  RELEASE HISTORY                                                       */
39 /*                                                                        */
40 /*    DATE              NAME                      DESCRIPTION             */
41 /*                                                                        */
42 /*  06-02-2021      William E. Lamie        Initial Version 6.1.7         */
43 /*  10-31-2022      Scott Larson            Remove unneeded values,       */
44 /*                                            resulting in version 6.2.0  */
45 /*                                                                        */
46 /**************************************************************************/
47 
48 #ifndef _TX_PX_INT_H
49 #define _TX_PX_INT_H
50 
51 #include  "string.h"
52 
53 /* Define several ThreadX equates for use inside of POSIX. */
54 #define  TX_INITIALIZE_IN_PROGRESS      0xF0F0F0F0UL
55 #define  TX_INITIALIZE_ALMOST_DONE      0xF0F0F0F1UL
56 
57 /* Threadx min and max priority */
58 #define TX_HIGHEST_PRIORITY                 1
59 #define TX_LOWEST_PRIORITY                  31
60 
61 #define PX_HIGHEST_PRIORITY                 31
62 #define PX_LOWEST_PRIORITY                  1
63 
64 /************ Extern Variables **************/
65 extern TX_THREAD *     _tx_thread_current_ptr;
66 extern TX_THREAD *     _tx_thread_execute_ptr;
67 
68 
69 
70 /* declares posix objects in the px_pth_init.c file */
71 #ifdef  PX_OBJECT_INIT
72 #define PX_OBJECT_DECLARE
73 #else
74 #define PX_OBJECT_DECLARE extern
75 #endif
76 /**************************************************************************/
77 /*                             Global Definitions                         */
78 /**************************************************************************/
79 
80 /* The ThreadX work queue for the POSIX System Manager.    */
81 PX_OBJECT_DECLARE TX_QUEUE                     posix_work_queue;
82 
83 /* Define the thread for the System Manager functionality.  */
84 PX_OBJECT_DECLARE TX_THREAD                    posix_system_manager;
85 
86 
87 /**************************************************************************/
88 /*                       Local definitions                                 */
89 /**************************************************************************/
90 
91 /* Define a byte pool control block for the heap memory used
92    by POSIX.  */
93 
94 PX_OBJECT_DECLARE TX_BYTE_POOL          posix_heap_byte_pool;
95 
96 /* Define a static pool of pthread Control Blocks (TCB). If more pthreades are
97    required the constant PTHREAD_THREADS_MAX (in tx_posix.h) may be modified.   */
98 PX_OBJECT_DECLARE POSIX_TCB             ptcb_pool[PTHREAD_THREADS_MAX];
99 
100 #if POSIX_MAX_QUEUES!= 0
101 /* Define a static pool of queues.If more pthread message queues are required
102    the constant POSIX_MAX_QUEUES (in tx_posix.h) may be modified.               */
103 PX_OBJECT_DECLARE POSIX_MSG_QUEUE       posix_queue_pool[POSIX_MAX_QUEUES];
104 /* Define a queue attribute.            */
105 PX_OBJECT_DECLARE struct mq_attr        posix_qattr_default;
106 #endif
107 
108 #if  SEM_NSEMS_MAX != 0
109 /* Define a static pool of semaphores. If more pthread semaphores required the
110    constant SEM_NSEMS_MAX (in tx_posix.h) may be modified.                      */
111 PX_OBJECT_DECLARE sem_t                 posix_sem_pool[SEM_NSEMS_MAX];
112 
113 #endif
114 
115 /* Define a default pthread attribute.  */
116 PX_OBJECT_DECLARE pthread_attr_t        posix_default_pthread_attr;
117 
118 /* Define a default mutex attribute.    */
119 PX_OBJECT_DECLARE pthread_mutexattr_t   posix_default_mutex_attr;
120 
121 
122 
123 /* Define a temporary posix errno.  */
124 
125 PX_OBJECT_DECLARE unsigned int posix_errno;
126 
127 
128 /**************************************************************************/
129 /*                      Local prototypes                                  */
130 /**************************************************************************/
131 /* Define Evacuation Kit for POSIX function prototypes.  */
132 
133 INT                   posix_get_pthread_errno(pthread_t ptid);
134 
135 POSIX_MSG_QUEUE      *posix_mq_create ( const CHAR * mq_name,
136                                                 struct mq_attr * msgq_attr);
137 
138 POSIX_MSG_QUEUE      *posix_find_queue(const CHAR * mq_name);
139 
140 POSIX_MSG_QUEUE      *posix_get_new_queue(ULONG maxnum);
141 
142 ULONG                 posix_arrange_msg( TX_QUEUE * Queue, ULONG * pMsgPrio );
143 
144 ULONG                 posix_priority_search(mqd_t msgQId ,ULONG priority);
145 
146 VOID                  posix_queue_init(VOID);
147 
148 VOID                  posix_qattr_init(VOID);
149 
150 VOID                  posix_pthread_init(VOID);
151 
152 struct mq_des        *posix_get_queue_des(POSIX_MSG_QUEUE * q_ptr);
153 
154 VOID                  posix_reset_queue(POSIX_MSG_QUEUE * q_ptr);
155 
156 VOID                  posix_memory_release(VOID * memory_ptr);
157 
158 VOID                  posix_internal_error(ULONG error_code);
159 
160 VOID                  posix_error_handler(ULONG error_code);
161 
162 INT                   posix_memory_allocate(ULONG size, VOID **memory_ptr);
163 
164 INT                   posix_queue_delete(POSIX_MSG_QUEUE  * q_ptr);
165 
166 VOID                  posix_putback_queue(TX_QUEUE * qid);
167 
168 sem_t                *posix_find_sem(const CHAR * name);
169 
170 VOID                  posix_set_sem_name(sem_t * sem, CHAR *name);
171 
172 TX_SEMAPHORE         *posix_get_new_sem(VOID);
173 
174 VOID                  posix_sem_reset(sem_t *sem);
175 
176 ULONG                 posix_in_thread_context(VOID);
177 
178 VOID                  posix_reset_pthread_t(POSIX_TCB *ptcb);
179 
180 TX_THREAD            *posix_tid2thread(pthread_t ptid);
181 
182 POSIX_TCB            *posix_tid2tcb(pthread_t ptid);
183 
184 pthread_t             posix_thread2tid(TX_THREAD *thread_ptr);
185 
186 POSIX_TCB            *posix_thread2tcb(TX_THREAD *thread_ptr);
187 
188 TX_THREAD            *posix_tcb2thread (POSIX_TCB *pthread_ptr);
189 
190 VOID                  posix_thread_wrapper(ULONG pthr_ptr);
191 
192 VOID                  set_default_pthread_attr(pthread_attr_t *attr);
193 
194 VOID                  set_default_mutexattr(pthread_mutexattr_t *attr);
195 
196 INT                   posix_allocate_pthread_t(POSIX_TCB **ptcb_ptr);
197 
198 VOID                  posix_copy_pthread_attr(POSIX_TCB *pthread_ptr,pthread_attr_t *attr);
199 
200 VOID                  posix_destroy_pthread(POSIX_TCB *pthread_ptr, VOID *value_ptr);
201 
202 VOID                  posix_do_pthread_delete(POSIX_TCB *pthread_ptr, VOID *value_ptr);
203 
204 VOID                  posix_system_manager_entry(ULONG input);
205 
206 INT                   posix_set_pthread_errno(ULONG errno_set);
207 
208 ULONG                 posix_abs_time_to_rel_ticks(struct timespec *abs_timeout);
209 
210 #endif
211