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 /** Semaphore */ 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_semaphore.h" 31 32 33 /**************************************************************************/ 34 /* */ 35 /* FUNCTION RELEASE */ 36 /* */ 37 /* _tx_semaphore_cleanup PORTABLE C */ 38 /* 6.1 */ 39 /* AUTHOR */ 40 /* */ 41 /* William E. Lamie, Microsoft Corporation */ 42 /* */ 43 /* DESCRIPTION */ 44 /* */ 45 /* This function processes semaphore timeout and thread terminate */ 46 /* actions that require the semaphore 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_semaphore_cleanup(TX_THREAD * thread_ptr,ULONG suspension_sequence)78VOID _tx_semaphore_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_SEMAPHORE *semaphore_ptr; 86 UINT suspended_count; 87 TX_THREAD *next_thread; 88 TX_THREAD *previous_thread; 89 90 91 92 #ifndef TX_NOT_INTERRUPTABLE 93 94 /* Disable interrupts to remove the suspended thread from the semaphore. */ 95 TX_DISABLE 96 97 /* Determine if the cleanup is still required. */ 98 if (thread_ptr -> tx_thread_suspend_cleanup == &(_tx_semaphore_cleanup)) 99 { 100 101 /* Check for valid suspension sequence. */ 102 if (suspension_sequence == thread_ptr -> tx_thread_suspension_sequence) 103 { 104 105 /* Setup pointer to semaphore control block. */ 106 semaphore_ptr = TX_VOID_TO_SEMAPHORE_POINTER_CONVERT(thread_ptr -> tx_thread_suspend_control_block); 107 108 /* Check for a NULL semaphore pointer. */ 109 if (semaphore_ptr != TX_NULL) 110 { 111 112 /* Check for a valid semaphore ID. */ 113 if (semaphore_ptr -> tx_semaphore_id == TX_SEMAPHORE_ID) 114 { 115 116 /* Determine if there are any thread suspensions. */ 117 if (semaphore_ptr -> tx_semaphore_suspended_count != TX_NO_SUSPENSIONS) 118 { 119 #else 120 121 /* Setup pointer to semaphore control block. */ 122 semaphore_ptr = TX_VOID_TO_SEMAPHORE_POINTER_CONVERT(thread_ptr -> tx_thread_suspend_control_block); 123 #endif 124 125 /* Yes, we still have thread suspension! */ 126 127 /* Clear the suspension cleanup flag. */ 128 thread_ptr -> tx_thread_suspend_cleanup = TX_NULL; 129 130 /* Decrement the suspended count. */ 131 semaphore_ptr -> tx_semaphore_suspended_count--; 132 133 /* Pickup the suspended count. */ 134 suspended_count = semaphore_ptr -> tx_semaphore_suspended_count; 135 136 /* Remove the suspended thread from the list. */ 137 138 /* See if this is the only suspended thread on the list. */ 139 if (suspended_count == TX_NO_SUSPENSIONS) 140 { 141 142 /* Yes, the only suspended thread. */ 143 144 /* Update the head pointer. */ 145 semaphore_ptr -> tx_semaphore_suspension_list = TX_NULL; 146 } 147 else 148 { 149 150 /* At least one more thread is on the same suspension list. */ 151 152 /* Update the links of the adjacent threads. */ 153 next_thread = thread_ptr -> tx_thread_suspended_next; 154 previous_thread = thread_ptr -> tx_thread_suspended_previous; 155 next_thread -> tx_thread_suspended_previous = previous_thread; 156 previous_thread -> tx_thread_suspended_next = next_thread; 157 158 /* Determine if we need to update the head pointer. */ 159 if (semaphore_ptr -> tx_semaphore_suspension_list == thread_ptr) 160 { 161 162 /* Update the list head pointer. */ 163 semaphore_ptr -> tx_semaphore_suspension_list = next_thread; 164 } 165 } 166 167 /* Now we need to determine if this cleanup is from a terminate, timeout, 168 or from a wait abort. */ 169 if (thread_ptr -> tx_thread_state == TX_SEMAPHORE_SUSP) 170 { 171 172 /* Timeout condition and the thread is still suspended on the semaphore. 173 Setup return error status and resume the thread. */ 174 175 #ifdef TX_SEMAPHORE_ENABLE_PERFORMANCE_INFO 176 177 /* Increment the total timeouts counter. */ 178 _tx_semaphore_performance_timeout_count++; 179 180 /* Increment the number of timeouts on this semaphore. */ 181 semaphore_ptr -> tx_semaphore_performance_timeout_count++; 182 #endif 183 184 /* Setup return status. */ 185 thread_ptr -> tx_thread_suspend_status = TX_NO_INSTANCE; 186 187 #ifdef TX_NOT_INTERRUPTABLE 188 189 /* Resume the thread! */ 190 _tx_thread_system_ni_resume(thread_ptr); 191 #else 192 193 /* Temporarily disable preemption. */ 194 _tx_thread_preempt_disable++; 195 196 /* Restore interrupts. */ 197 TX_RESTORE 198 199 /* Resume the thread! */ 200 _tx_thread_system_resume(thread_ptr); 201 202 /* Disable interrupts. */ 203 TX_DISABLE 204 #endif 205 } 206 #ifndef TX_NOT_INTERRUPTABLE 207 } 208 } 209 } 210 } 211 } 212 213 /* Restore interrupts. */ 214 TX_RESTORE 215 #endif 216 } 217 218