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 /** Mutex */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #define TX_SOURCE_CODE
24
25
26 /* Include necessary system files. */
27
28 #include "tx_api.h"
29 #include "tx_initialize.h"
30 #include "tx_thread.h"
31 #include "tx_timer.h"
32 #include "tx_mutex.h"
33
34
35 /**************************************************************************/
36 /* */
37 /* FUNCTION RELEASE */
38 /* */
39 /* _txe_mutex_create PORTABLE C */
40 /* 6.1 */
41 /* AUTHOR */
42 /* */
43 /* William E. Lamie, Microsoft Corporation */
44 /* */
45 /* DESCRIPTION */
46 /* */
47 /* This function checks for errors in the create mutex function */
48 /* call. */
49 /* */
50 /* INPUT */
51 /* */
52 /* mutex_ptr Pointer to mutex control block */
53 /* name_ptr Pointer to mutex name */
54 /* inherit Initial mutex count */
55 /* mutex_control_block_size Size of mutex control block */
56 /* */
57 /* OUTPUT */
58 /* */
59 /* TX_MUTEX_ERROR Invalid mutex pointer */
60 /* TX_CALLER_ERROR Invalid caller of this function */
61 /* TX_INHERIT_ERROR Invalid inherit option */
62 /* status Actual completion status */
63 /* */
64 /* CALLS */
65 /* */
66 /* _tx_mutex_create Actual create mutex function */
67 /* _tx_thread_system_preempt_check Check for preemption */
68 /* */
69 /* CALLED BY */
70 /* */
71 /* Application Code */
72 /* */
73 /* RELEASE HISTORY */
74 /* */
75 /* DATE NAME DESCRIPTION */
76 /* */
77 /* 05-19-2020 William E. Lamie Initial Version 6.0 */
78 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
79 /* resulting in version 6.1 */
80 /* */
81 /**************************************************************************/
_txe_mutex_create(TX_MUTEX * mutex_ptr,CHAR * name_ptr,UINT inherit,UINT mutex_control_block_size)82 UINT _txe_mutex_create(TX_MUTEX *mutex_ptr, CHAR *name_ptr, UINT inherit, UINT mutex_control_block_size)
83 {
84
85 TX_INTERRUPT_SAVE_AREA
86
87 UINT status;
88 ULONG i;
89 TX_MUTEX *next_mutex;
90 #ifndef TX_TIMER_PROCESS_IN_ISR
91 TX_THREAD *thread_ptr;
92 #endif
93
94
95 /* Default status to success. */
96 status = TX_SUCCESS;
97
98 /* Check for an invalid mutex pointer. */
99 if (mutex_ptr == TX_NULL)
100 {
101
102 /* Mutex pointer is invalid, return appropriate error code. */
103 status = TX_MUTEX_ERROR;
104 }
105
106 /* Now check to make sure the control block is the correct size. */
107 else if (mutex_control_block_size != (sizeof(TX_MUTEX)))
108 {
109
110 /* Mutex pointer is invalid, return appropriate error code. */
111 status = TX_MUTEX_ERROR;
112 }
113 else
114 {
115
116 /* Disable interrupts. */
117 TX_DISABLE
118
119 /* Increment the preempt disable flag. */
120 _tx_thread_preempt_disable++;
121
122 /* Restore interrupts. */
123 TX_RESTORE
124
125 /* Next see if it is already in the created list. */
126 next_mutex = _tx_mutex_created_ptr;
127 for (i = ((ULONG) 0); i < _tx_mutex_created_count; i++)
128 {
129
130 /* Determine if this mutex matches the mutex in the list. */
131 if (mutex_ptr == next_mutex)
132 {
133
134 break;
135 }
136 else
137 {
138
139 /* Move to the next mutex. */
140 next_mutex = next_mutex -> tx_mutex_created_next;
141 }
142 }
143
144 /* Disable interrupts. */
145 TX_DISABLE
146
147 /* Decrement the preempt disable flag. */
148 _tx_thread_preempt_disable--;
149
150 /* Restore interrupts. */
151 TX_RESTORE
152
153 /* Check for preemption. */
154 _tx_thread_system_preempt_check();
155
156 /* At this point, check to see if there is a duplicate mutex. */
157 if (mutex_ptr == next_mutex)
158 {
159
160 /* Mutex is already created, return appropriate error code. */
161 status = TX_MUTEX_ERROR;
162 }
163 else
164 {
165
166 /* Check for a valid inherit option. */
167 if (inherit != TX_INHERIT)
168 {
169
170 if (inherit != TX_NO_INHERIT)
171 {
172
173 /* Inherit option is illegal. */
174 status = TX_INHERIT_ERROR;
175 }
176 }
177 }
178 }
179
180 /* Determine if everything is okay. */
181 if (status == TX_SUCCESS)
182 {
183
184 #ifndef TX_TIMER_PROCESS_IN_ISR
185
186 /* Pickup thread pointer. */
187 TX_THREAD_GET_CURRENT(thread_ptr)
188
189 /* Check for invalid caller of this function. First check for a calling thread. */
190 if (thread_ptr == &_tx_timer_thread)
191 {
192
193 /* Invalid caller of this function, return appropriate error code. */
194 status = TX_CALLER_ERROR;
195 }
196 #endif
197
198 /* Check for interrupt call. */
199 if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
200 {
201
202 /* Now, make sure the call is from an interrupt and not initialization. */
203 if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
204 {
205
206 /* Invalid caller of this function, return appropriate error code. */
207 status = TX_CALLER_ERROR;
208 }
209 }
210 }
211
212 /* Determine if everything is okay. */
213 if (status == TX_SUCCESS)
214 {
215
216 /* Call actual mutex create function. */
217 status = _tx_mutex_create(mutex_ptr, name_ptr, inherit);
218 }
219
220 /* Return completion status. */
221 return(status);
222 }
223
224