1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** POSIX wrapper for THREADX                                             */
16 /**                                                                       */
17 /**                                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /* Include necessary system files.  */
23 
24 #include "tx_api.h"    /* Threadx API */
25 #include "pthread.h"  /* Posix API */
26 #include "px_int.h"    /* Posix helper functions */
27 
28 /**************************************************************************/
29 /**************************************************************************/
30 /**                                                                       */
31 /** POSIX wrapper for THREADX                                             */
32 /**                                                                       */
33 /**                                                                       */
34 /**                                                                       */
35 /**************************************************************************/
36 /**************************************************************************/
37 
38 /* Include necessary system files.  */
39 
40 #include "tx_api.h"    /* Threadx API */
41 #include "pthread.h"  /* Posix API */
42 #include "px_int.h"    /* Posix helper functions */
43 
44 
45 
46 
47 /**************************************************************************/
48 /*                                                                        */
49 /*  FUNCTION                                               RELEASE        */
50 /*                                                                        */
51 /*    posix_memory_init                                   PORTABLE C      */
52 /*                                                           6.1.7        */
53 /*  AUTHOR                                                                */
54 /*                                                                        */
55 /*    William E. Lamie, Microsoft Corporation                             */
56 /*                                                                        */
57 /*  DESCRIPTION                                                           */
58 /*                                                                        */
59 /*    This function attempts to create a ThreadX byte pool that will      */
60 /*    act as a "heap" for the POSIX's dynamic, "behind-the-scenes"        */
61 /*    memory needs.                                                       */
62 /*                                                                        */
63 /*  INPUT                                                                 */
64 /*                                                                        */
65 /*    reqion0_ptr                           Pointer to region0 memory     */
66 /*                                                                        */
67 /*  OUTPUT                                                                */
68 /*                                                                        */
69 /*    None                                                                */
70 /*                                                                        */
71 /*  CALLS                                                                 */
72 /*                                                                        */
73 /*    tx_byte_pool_create                   Create region0 byte pool      */
74 /*    posix_internal_error                  Generic posix error           */
75 /*                                                                        */
76 /*  CALLED BY                                                             */
77 /*                                                                        */
78 /*    POSIX internal code                                                 */
79 /*                                                                        */
80 /*  RELEASE HISTORY                                                       */
81 /*                                                                        */
82 /*    DATE              NAME                      DESCRIPTION             */
83 /*                                                                        */
84 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
85 /*                                                                        */
86 /**************************************************************************/
posix_memory_init(VOID * posix_heap_ptr)87 static VOID posix_memory_init(VOID * posix_heap_ptr)
88 {
89 
90 INT    retval;
91 
92     /* Create a ThreadX byte pool that will provide memory
93        needed by the POSIX.  */
94     retval = tx_byte_pool_create((TX_BYTE_POOL *)&posix_heap_byte_pool,
95                                  "POSIX HEAP",
96                                  posix_heap_ptr,
97                                  POSIX_HEAP_SIZE_IN_BYTES);
98 
99     /* Make sure the byte pool was created successfully.  */
100     if (retval)
101     {
102         /* Error creating byte pool.  */
103         posix_internal_error(9999);
104     }
105 }
106 
107 
108 
109 /**************************************************************************/
110 /*                                                                        */
111 /*  FUNCTION                                               RELEASE        */
112 /*                                                                        */
113 /*    posix_semaphore_init                                PORTABLE C      */
114 /*                                                           6.1.7        */
115 /*  AUTHOR                                                                */
116 /*                                                                        */
117 /*    William E. Lamie, Microsoft Corporation                             */
118 /*                                                                        */
119 /*  DESCRIPTION                                                           */
120 /*                                                                        */
121 /*    This function initializes the POSIX's internal semaphore pool.      */
122 /*                                                                        */
123 /*  INPUT                                                                 */
124 /*                                                                        */
125 /*    None                                                                */
126 /*                                                                        */
127 /*  OUTPUT                                                                */
128 /*                                                                        */
129 /*    None                                                                */
130 /*                                                                        */
131 /*  CALLS                                                                 */
132 /*                                                                        */
133 /*    None                                                                */
134 /*                                                                        */
135 /*  CALLED BY                                                             */
136 /*                                                                        */
137 /*    POSIX internal Code                                                 */
138 /*                                                                        */
139 /*  RELEASE HISTORY                                                       */
140 /*                                                                        */
141 /*    DATE              NAME                      DESCRIPTION             */
142 /*                                                                        */
143 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
144 /*                                                                        */
145 /**************************************************************************/
posix_semaphore_init(VOID)146 static VOID posix_semaphore_init(VOID)
147 {
148 
149 ULONG       i;
150 sem_t      *sem_ptr;
151 
152     /* Start at the front of the pool.  */
153     sem_ptr = &(posix_sem_pool[0]);
154 
155     for (i = 0;  i < SEM_NSEMS_MAX;  i++, sem_ptr++)
156     {
157         /* This semaphore is not currently in use.  */
158         sem_ptr->in_use = TX_FALSE;
159         sem_ptr->count = 0;
160         sem_ptr->sem_name = "";
161         sem_ptr->refCnt = 0;
162         sem_ptr->psemId = 0;
163         sem_ptr->unlink_flag = TX_FALSE;
164     }
165 }
166 
167 
168 
169 /**************************************************************************/
170 /*                                                                        */
171 /*  FUNCTION                                               RELEASE        */
172 /*                                                                        */
173 /*    posix_initialize                                    PORTABLE C      */
174 /*                                                           6.1.7        */
175 /*  AUTHOR                                                                */
176 /*                                                                        */
177 /*    William E. Lamie, Microsoft Corporation                             */
178 /*                                                                        */
179 /*  DESCRIPTION                                                           */
180 /*                                                                        */
181 /*    This function sets up / configures / initializes all the            */
182 /*    "behind-the-scenes" data structures, tables, memory regions, etc.   */
183 /*    used by the POSIX at run-time.                                      */
184 /*                                                                        */
185 /*  INPUT                                                                 */
186 /*                                                                        */
187 /*    posix_memory                           POSIX memory pointer         */
188 /*                                                                        */
189 /*  OUTPUT                                                                */
190 /*                                                                        */
191 /*    pointer                                Next free POSIX memory       */
192 /*                                                                        */
193 /*  CALLS                                                                 */
194 /*                                                                        */
195 /*    posix_memory_init                     Initialize posix memory       */
196 /*    tx_thread_create                      Create System Manager Thread  */
197 /*    tx_queue_create                       Create system manager queue   */
198 /*    posix_queue_init                      Initialize posix queues       */
199 /*    posix_qattr_init                      Initialize mqattr structure   */
200 /*    posix_semaphore_init                  Initialize posix semaphores   */
201 /*    set_default_mutexattr                 Set up default mutexattr obj  */
202 /*    posix_pthread_init                    Initialize a static pool of   */
203 /*                                          pthreads Control blocks       */
204 /*    set_default_pthread_attr              Set defualt pthread_attr obj  */
205 /*                                                                        */
206 /*  CALLED BY                                                             */
207 /*                                                                        */
208 /*    Start-up code                                                       */
209 /*                                                                        */
210 /*  RELEASE HISTORY                                                       */
211 /*                                                                        */
212 /*    DATE              NAME                      DESCRIPTION             */
213 /*                                                                        */
214 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
215 /*                                                                        */
216 /**************************************************************************/
posix_initialize(VOID * posix_memory)217 VOID *posix_initialize(VOID * posix_memory)
218 {
219 
220 UCHAR   *pointer;
221 
222     /*  Setup temporary memory pointer, so we can start allocating
223         space for the posix data structures.  The important thing to
224         remember here is that the system thread's stack, the region0
225         memory, and the queue are allocated sequentially from the
226         address specified by posix_memory.                          */
227 
228     pointer =  (UCHAR *)posix_memory;
229 
230     /* Start up the System Manager thread.  */
231     tx_thread_create(&posix_system_manager, "POSIX System Manager", posix_system_manager_entry,
232                        0, pointer, POSIX_SYSTEM_STACK_SIZE,
233                        SYSMGR_PRIORITY, SYSMGR_THRESHOLD,
234                        TX_NO_TIME_SLICE, TX_AUTO_START);
235 
236     pointer =  pointer + POSIX_SYSTEM_STACK_SIZE;
237 
238     /* Set up a memory "heap" used internally by the POSIX.  */
239     posix_memory_init(pointer);
240 
241     pointer =  pointer + POSIX_HEAP_SIZE_IN_BYTES;
242 
243     /* Create the work item message queue.  */
244     tx_queue_create(&posix_work_queue, "POSIX work queue", WORK_REQ_SIZE,
245                        pointer, WORK_QUEUE_DEPTH*WORK_REQ_SIZE);
246 
247     pointer = pointer + (WORK_QUEUE_DEPTH * WORK_REQ_SIZE);
248 
249     /* Initialize static pool of pthreads Control blocks.  */
250     posix_pthread_init();
251 
252     /* Create a default pthread_attr */
253     set_default_pthread_attr(&posix_default_pthread_attr);
254 
255 #if POSIX_MAX_QUEUES != 0
256     /* Set up a pool of message queues used internally by the POSIX.  */
257     posix_queue_init();
258 
259     /* Set up a pool of q_attr used internally by the POSIX.  */
260     posix_qattr_init();
261 #endif
262 
263 #if SEM_NSEMS_MAX != 0
264     /* Set up a pool of semaphores used internally by the POSIX.  */
265     posix_semaphore_init();
266 #endif
267 
268     /* Create a default mutex_attr */
269     set_default_mutexattr(&posix_default_mutex_attr);
270 
271     pointer = (VOID *) pointer;
272 
273     /* All done.  */
274     return(pointer);
275 }
276