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