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