summaryrefslogtreecommitdiff
path: root/utility/rtos_compatibility_layers/posix/px_px_initialize.c
blob: 0ad147a00ab0553a5e1d0984c0c909efc1b55b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** POSIX wrapper for THREADX                                             */
/**                                                                       */
/**                                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* Include necessary system files.  */

#include "tx_api.h"    /* Threadx API */
#include "pthread.h"  /* Posix API */
#include "px_int.h"    /* Posix helper functions */

/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** POSIX wrapper for THREADX                                             */
/**                                                                       */
/**                                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* Include necessary system files.  */

#include "tx_api.h"    /* Threadx API */
#include "pthread.h"  /* Posix API */
#include "px_int.h"    /* Posix helper functions */




/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    posix_memory_init                                   PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function attempts to create a ThreadX byte pool that will      */
/*    act as a "heap" for the POSIX's dynamic, "behind-the-scenes"        */
/*    memory needs.                                                       */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    reqion0_ptr                           Pointer to region0 memory     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_byte_pool_create                   Create region0 byte pool      */
/*    posix_internal_error                  Generic posix error           */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    POSIX internal code                                                 */
/*                                                                        */
/**************************************************************************/
static VOID posix_memory_init(VOID * posix_heap_ptr)
{

INT    retval;

    /* Create a ThreadX byte pool that will provide memory
       needed by the POSIX.  */
    retval = tx_byte_pool_create((TX_BYTE_POOL *)&posix_heap_byte_pool,
                                 "POSIX HEAP",
                                 posix_heap_ptr,
                                 POSIX_HEAP_SIZE_IN_BYTES);

    /* Make sure the byte pool was created successfully.  */
    if (retval)
    {
        /* Error creating byte pool.  */
        posix_internal_error(9999);
    }
}



/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    posix_semaphore_init                                PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function initializes the POSIX's internal semaphore pool.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    POSIX internal Code                                                 */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
/*                                                                        */
/**************************************************************************/
static VOID posix_semaphore_init(VOID)
{

ULONG       i;
sem_t      *sem_ptr;

    /* Start at the front of the pool.  */
    sem_ptr = &(posix_sem_pool[0]);

    for (i = 0;  i < SEM_NSEMS_MAX;  i++, sem_ptr++)
    {
        /* This semaphore is not currently in use.  */
        sem_ptr->in_use = TX_FALSE;
        sem_ptr->count = 0;
        sem_ptr->sem_name = "";
        sem_ptr->refCnt = 0;
        sem_ptr->psemId = 0;
        sem_ptr->unlink_flag = TX_FALSE;
    }
}



/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    posix_initialize                                    PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sets up / configures / initializes all the            */
/*    "behind-the-scenes" data structures, tables, memory regions, etc.   */
/*    used by the POSIX at run-time.                                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    posix_memory                           POSIX memory pointer         */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    pointer                                Next free POSIX memory       */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    posix_memory_init                     Initialize posix memory       */
/*    tx_thread_create                      Create System Manager Thread  */
/*    tx_queue_create                       Create system manager queue   */
/*    posix_queue_init                      Initialize posix queues       */
/*    posix_qattr_init                      Initialize mqattr structure   */
/*    posix_semaphore_init                  Initialize posix semaphores   */
/*    set_default_mutexattr                 Set up default mutexattr obj  */
/*    posix_pthread_init                    Initialize a static pool of   */
/*                                          pthreads Control blocks       */
/*    set_default_pthread_attr              Set defualt pthread_attr obj  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Start-up code                                                       */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
/*                                                                        */
/**************************************************************************/
VOID *posix_initialize(VOID * posix_memory)
{

UCHAR   *pointer;

    /*  Setup temporary memory pointer, so we can start allocating
        space for the posix data structures.  The important thing to
        remember here is that the system thread's stack, the region0
        memory, and the queue are allocated sequentially from the
        address specified by posix_memory.                          */

    pointer =  (UCHAR *)posix_memory;

    /* Start up the System Manager thread.  */
    tx_thread_create(&posix_system_manager, "POSIX System Manager", posix_system_manager_entry,
                       0, pointer, POSIX_SYSTEM_STACK_SIZE,
                       SYSMGR_PRIORITY, SYSMGR_THRESHOLD,
                       TX_NO_TIME_SLICE, TX_AUTO_START);

    pointer =  pointer + POSIX_SYSTEM_STACK_SIZE;

    /* Set up a memory "heap" used internally by the POSIX.  */
    posix_memory_init(pointer);

    pointer =  pointer + POSIX_HEAP_SIZE_IN_BYTES;

    /* Create the work item message queue.  */
    tx_queue_create(&posix_work_queue, "POSIX work queue", WORK_REQ_SIZE,
                       pointer, WORK_QUEUE_DEPTH*WORK_REQ_SIZE);

    pointer = pointer + (WORK_QUEUE_DEPTH * WORK_REQ_SIZE);

    /* Initialize static pool of pthreads Control blocks.  */
    posix_pthread_init();

    /* Create a default pthread_attr */
    set_default_pthread_attr(&posix_default_pthread_attr);

#if POSIX_MAX_QUEUES != 0
    /* Set up a pool of message queues used internally by the POSIX.  */
    posix_queue_init();

    /* Set up a pool of q_attr used internally by the POSIX.  */
    posix_qattr_init();
#endif

#if SEM_NSEMS_MAX != 0
    /* Set up a pool of semaphores used internally by the POSIX.  */
    posix_semaphore_init();
#endif

    /* Create a default mutex_attr */
    set_default_mutexattr(&posix_default_mutex_attr);

    pointer = (VOID *) pointer;

    /* All done.  */
    return(pointer);
}