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_thread.h"
30 #include "tx_mutex.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _tx_mutex_cleanup                                   PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function processes mutex timeout and thread terminate          */
46 /*    actions that require the mutex data structures to be cleaned        */
47 /*    up.                                                                 */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    thread_ptr                        Pointer to suspended thread's     */
52 /*                                        control block                   */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _tx_thread_system_resume          Resume thread service             */
61 /*    _tx_thread_system_ni_resume       Non-interruptable resume thread   */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    _tx_thread_timeout                Thread timeout processing         */
66 /*    _tx_thread_terminate              Thread terminate processing       */
67 /*    _tx_thread_wait_abort             Thread wait abort processing      */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_tx_mutex_cleanup(TX_THREAD * thread_ptr,ULONG suspension_sequence)78 VOID  _tx_mutex_cleanup(TX_THREAD  *thread_ptr, ULONG suspension_sequence)
79 {
80 
81 #ifndef TX_NOT_INTERRUPTABLE
82 TX_INTERRUPT_SAVE_AREA
83 #endif
84 
85 TX_MUTEX            *mutex_ptr;
86 UINT                suspended_count;
87 TX_THREAD           *next_thread;
88 TX_THREAD           *previous_thread;
89 
90 
91 #ifndef TX_NOT_INTERRUPTABLE
92 
93     /* Disable interrupts to remove the suspended thread from the mutex.  */
94     TX_DISABLE
95 
96     /* Determine if the cleanup is still required.  */
97     if (thread_ptr -> tx_thread_suspend_cleanup == &(_tx_mutex_cleanup))
98     {
99 
100         /* Check for valid suspension sequence.  */
101         if (suspension_sequence == thread_ptr -> tx_thread_suspension_sequence)
102         {
103 
104             /* Setup pointer to mutex control block.  */
105             mutex_ptr =  TX_VOID_TO_MUTEX_POINTER_CONVERT(thread_ptr -> tx_thread_suspend_control_block);
106 
107             /* Check for NULL mutex pointer.  */
108             if (mutex_ptr != TX_NULL)
109             {
110 
111                 /* Determine if the mutex ID is valid.  */
112                 if (mutex_ptr -> tx_mutex_id == TX_MUTEX_ID)
113                 {
114 
115                     /* Determine if there are any thread suspensions.  */
116                     if (mutex_ptr -> tx_mutex_suspended_count != TX_NO_SUSPENSIONS)
117                     {
118 #else
119 
120                         /* Setup pointer to mutex control block.  */
121                         mutex_ptr =  TX_VOID_TO_MUTEX_POINTER_CONVERT(thread_ptr -> tx_thread_suspend_control_block);
122 #endif
123 
124                         /* Yes, we still have thread suspension!  */
125 
126                         /* Clear the suspension cleanup flag.  */
127                         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
128 
129                         /* Decrement the suspension count.  */
130                         mutex_ptr -> tx_mutex_suspended_count--;
131 
132                         /* Pickup the suspended count.  */
133                         suspended_count =  mutex_ptr -> tx_mutex_suspended_count;
134 
135                         /* Remove the suspended thread from the list.  */
136 
137                         /* See if this is the only suspended thread on the list.  */
138                         if (suspended_count == TX_NO_SUSPENSIONS)
139                         {
140 
141                             /* Yes, the only suspended thread.  */
142 
143                             /* Update the head pointer.  */
144                             mutex_ptr -> tx_mutex_suspension_list =  TX_NULL;
145                         }
146                         else
147                         {
148 
149                             /* At least one more thread is on the same suspension list.  */
150 
151                             /* Update the links of the adjacent threads.  */
152                             next_thread =                                   thread_ptr -> tx_thread_suspended_next;
153                             previous_thread =                               thread_ptr -> tx_thread_suspended_previous;
154                             next_thread -> tx_thread_suspended_previous =   previous_thread;
155                             previous_thread -> tx_thread_suspended_next =   next_thread;
156 
157                             /* Determine if we need to update the head pointer.  */
158                             if (mutex_ptr -> tx_mutex_suspension_list == thread_ptr)
159                             {
160 
161                                 /* Update the list head pointer.  */
162                                 mutex_ptr -> tx_mutex_suspension_list =         next_thread;
163                             }
164                         }
165 
166                         /* Now we need to determine if this cleanup is from a terminate, timeout,
167                            or from a wait abort.  */
168                         if (thread_ptr -> tx_thread_state == TX_MUTEX_SUSP)
169                         {
170 
171                             /* Timeout condition and the thread still suspended on the mutex.
172                                Setup return error status and resume the thread.  */
173 
174 #ifdef TX_MUTEX_ENABLE_PERFORMANCE_INFO
175 
176                             /* Increment the total timeouts counter.  */
177                             _tx_mutex_performance_timeout_count++;
178 
179                             /* Increment the number of timeouts on this semaphore.  */
180                             mutex_ptr -> tx_mutex_performance_timeout_count++;
181 #endif
182 
183                             /* Setup return status.  */
184                             thread_ptr -> tx_thread_suspend_status =  TX_NOT_AVAILABLE;
185 
186 #ifdef TX_NOT_INTERRUPTABLE
187 
188                             /* Resume the thread!  */
189                             _tx_thread_system_ni_resume(thread_ptr);
190 #else
191 
192                             /* Temporarily disable preemption.  */
193                             _tx_thread_preempt_disable++;
194 
195                             /* Restore interrupts.  */
196                             TX_RESTORE
197 
198                             /* Resume the thread!  */
199                             _tx_thread_system_resume(thread_ptr);
200 
201                             /* Disable interrupts.  */
202                             TX_DISABLE
203 #endif
204                         }
205 #ifndef TX_NOT_INTERRUPTABLE
206                     }
207                 }
208             }
209         }
210     }
211 
212     /* Restore interrupts.  */
213     TX_RESTORE
214 #endif
215 }
216 
217 
218 /**************************************************************************/
219 /*                                                                        */
220 /*  FUNCTION                                               RELEASE        */
221 /*                                                                        */
222 /*    _tx_mutex_thread_release                            PORTABLE C      */
223 /*                                                           6.1          */
224 /*  AUTHOR                                                                */
225 /*                                                                        */
226 /*    William E. Lamie, Microsoft Corporation                             */
227 /*                                                                        */
228 /*  DESCRIPTION                                                           */
229 /*                                                                        */
230 /*    This function releases all mutexes owned by the thread. This        */
231 /*    function is called when the thread completes or is terminated.      */
232 /*                                                                        */
233 /*  INPUT                                                                 */
234 /*                                                                        */
235 /*    thread_ptr                        Pointer to thread's control block */
236 /*                                                                        */
237 /*  OUTPUT                                                                */
238 /*                                                                        */
239 /*    None                                                                */
240 /*                                                                        */
241 /*  CALLS                                                                 */
242 /*                                                                        */
243 /*    _tx_mutex_put                     Release the mutex                 */
244 /*                                                                        */
245 /*  CALLED BY                                                             */
246 /*                                                                        */
247 /*    _tx_thread_shell_entry            Thread completion processing      */
248 /*    _tx_thread_terminate              Thread terminate processing       */
249 /*                                                                        */
250 /*  RELEASE HISTORY                                                       */
251 /*                                                                        */
252 /*    DATE              NAME                      DESCRIPTION             */
253 /*                                                                        */
254 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
255 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
256 /*                                            resulting in version 6.1    */
257 /*                                                                        */
258 /**************************************************************************/
_tx_mutex_thread_release(TX_THREAD * thread_ptr)259 VOID  _tx_mutex_thread_release(TX_THREAD  *thread_ptr)
260 {
261 
262 TX_INTERRUPT_SAVE_AREA
263 
264 TX_MUTEX    *mutex_ptr;
265 #ifdef TX_MISRA_ENABLE
266 UINT        status;
267 #endif
268 
269 
270     /* Disable interrupts.  */
271     TX_DISABLE
272 
273     /* Temporarily disable preemption.  */
274     _tx_thread_preempt_disable++;
275 
276     /* Loop to look at all the mutexes.  */
277     do
278     {
279 
280         /* Pickup the mutex head pointer.  */
281         mutex_ptr =  thread_ptr -> tx_thread_owned_mutex_list;
282 
283         /* Determine if there is a mutex.  */
284         if (mutex_ptr != TX_NULL)
285         {
286 
287             /* Yes, set the ownership count to 1.  */
288             mutex_ptr -> tx_mutex_ownership_count =  ((UINT) 1);
289 
290             /* Restore interrupts.   */
291             TX_RESTORE
292 
293 #ifdef TX_MISRA_ENABLE
294             /* Release the mutex.  */
295             do
296             {
297                 status =  _tx_mutex_put(mutex_ptr);
298             } while (status != TX_SUCCESS);
299 #else
300             _tx_mutex_put(mutex_ptr);
301 #endif
302 
303             /* Disable interrupts.  */
304             TX_DISABLE
305 
306             /* Move to the next mutex.  */
307             mutex_ptr =  thread_ptr -> tx_thread_owned_mutex_list;
308         }
309     } while (mutex_ptr != TX_NULL);
310 
311     /* Restore preemption.  */
312     _tx_thread_preempt_disable--;
313 
314     /* Restore interrupts.  */
315     TX_RESTORE
316 }
317 
318