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 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Module Manager                                                      */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define TX_SOURCE_CODE
23 #define TX_MODULE_MANAGER_INIT
24 
25 #include "tx_api.h"
26 #include "tx_initialize.h"
27 #include "tx_thread.h"
28 #include "tx_byte_pool.h"
29 #include "tx_queue.h"
30 #include "tx_mutex.h"
31 #include "txm_module.h"
32 
33 
34 TXM_MODULE_MANAGER_VERSION_ID
35 
36 
37 /* Define global variables associated with the module manager.  */
38 
39 
40 /* Define the module properties supported by this module manager.  */
41 
42 ULONG                   _txm_module_manager_properties_supported;
43 
44 
45 /* Define the module properties required by this module manager.   */
46 
47 ULONG                   _txm_module_manager_properties_required;
48 
49 
50 /* Define byte pool that will be used for allocating module data areas.  */
51 
52 TX_BYTE_POOL            _txm_module_manager_byte_pool;
53 
54 
55 /* Define byte pool that will be used for allocating external memory for module objects.  */
56 
57 TX_BYTE_POOL            _txm_module_manager_object_pool;
58 
59 
60 /* Define the flag indicating that the module manager byte pool is created.  */
61 
62 UINT                    _txm_module_manager_object_pool_created;
63 
64 
65 /* Define module manager protection mutex.  */
66 
67 TX_MUTEX                _txm_module_manager_mutex;
68 
69 
70 /* Define the loaded modules list, which keeps track of all loaded modules.  */
71 
72 TXM_MODULE_INSTANCE     *_txm_module_manager_loaded_list_ptr;
73 
74 
75 /* Define the count of loaded modules.  */
76 
77 ULONG                   _txm_module_manger_loaded_count;
78 
79 
80 /* Define the ready flag, which is checked by other module manager APIs
81    to make sure the manager has been initialized.  */
82 
83 UINT                    _txm_module_manager_ready;
84 
85 
86 /* Define the total callback activation count. This is simply incremented on every
87    callback activation.  */
88 
89 ULONG                   _txm_module_manager_callback_total_count;
90 
91 
92 /* Define the callback activation error count. This occurs when the available callback
93    structures have been exhausted.  */
94 
95 ULONG                   _txm_module_manager_callback_error_count;
96 
97 
98 
99 /**************************************************************************/
100 /*                                                                        */
101 /*  FUNCTION                                               RELEASE        */
102 /*                                                                        */
103 /*    _txm_module_manager_initialize                      PORTABLE C      */
104 /*                                                           6.1          */
105 /*  AUTHOR                                                                */
106 /*                                                                        */
107 /*    Scott Larson, Microsoft Corporation                                 */
108 /*                                                                        */
109 /*  DESCRIPTION                                                           */
110 /*                                                                        */
111 /*    This function initializes the module manager.                       */
112 /*                                                                        */
113 /*  INPUT                                                                 */
114 /*                                                                        */
115 /*    module_memory_start               Start of module area              */
116 /*    module_memory_size                Size in bytes of module area      */
117 /*                                                                        */
118 /*  OUTPUT                                                                */
119 /*                                                                        */
120 /*    status                            Completion status                 */
121 /*                                                                        */
122 /*  CALLS                                                                 */
123 /*                                                                        */
124 /*    _tx_byte_pool_create              Create module memory byte pool    */
125 /*    _tx_mutex_create                  Create module manager             */
126 /*                                        protection mutex                */
127 /*                                                                        */
128 /*  CALLED BY                                                             */
129 /*                                                                        */
130 /*    Application code                                                    */
131 /*                                                                        */
132 /*  RELEASE HISTORY                                                       */
133 /*                                                                        */
134 /*    DATE              NAME                      DESCRIPTION             */
135 /*                                                                        */
136 /*  09-30-2020      Scott Larson            Initial Version 6.1           */
137 /*                                                                        */
138 /**************************************************************************/
_txm_module_manager_initialize(VOID * module_memory_start,ULONG module_memory_size)139 UINT  _txm_module_manager_initialize(VOID *module_memory_start, ULONG module_memory_size)
140 {
141 
142     /* Check for interrupt call.  */
143     if (TX_THREAD_GET_SYSTEM_STATE() != 0)
144     {
145 
146         /* Now, make sure the call is from an interrupt and not initialization.  */
147         if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
148         {
149 
150             /* Invalid caller of this function, return appropriate error code.  */
151             return(TX_CALLER_ERROR);
152         }
153     }
154 
155     /* Setup the module properties supported by this module manager.  */
156     _txm_module_manager_properties_supported =  TXM_MODULE_MANAGER_SUPPORTED_OPTIONS;
157 
158     /* Setup the module properties required by this module manager.   */
159     _txm_module_manager_properties_required =   TXM_MODULE_MANAGER_REQUIRED_OPTIONS;
160 
161     /* Clear the module manager ready flag.  */
162     _txm_module_manager_ready =  TX_FALSE;
163 
164     /* Initialize the empty module list.  */
165     _txm_module_manager_loaded_list_ptr =  TX_NULL;
166 
167     /* Clear the number of loaded modules.  */
168     _txm_module_manger_loaded_count =  0;
169 
170     /* Create the module manager protection mutex.  */
171     _tx_mutex_create(&_txm_module_manager_mutex, "Module Manager Protection Mutex", TX_NO_INHERIT);
172 
173     /* Create a byte pool for allocating RAM areas for modules.  */
174     _tx_byte_pool_create(&_txm_module_manager_byte_pool, "Module Manager Byte Pool", module_memory_start, module_memory_size);
175 
176     /* Indicate the module manager object pool has not been created.  */
177     _txm_module_manager_object_pool_created =  TX_FALSE;
178 
179     /* Mark the module manager as ready!  */
180     _txm_module_manager_ready =  TX_TRUE;
181 
182     /* Return success.  */
183     return(TX_SUCCESS);
184 }
185 
186