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 /** Byte Memory */ 18 /** */ 19 /**************************************************************************/ 20 /**************************************************************************/ 21 22 #define TX_SOURCE_CODE 23 24 25 /* Include necessary system files. */ 26 27 #include "tx_api.h" 28 #include "tx_thread.h" 29 #include "tx_byte_pool.h" 30 31 32 /**************************************************************************/ 33 /* */ 34 /* FUNCTION RELEASE */ 35 /* */ 36 /* _tx_byte_pool_cleanup PORTABLE C */ 37 /* 6.1 */ 38 /* AUTHOR */ 39 /* */ 40 /* William E. Lamie, Microsoft Corporation */ 41 /* */ 42 /* DESCRIPTION */ 43 /* */ 44 /* This function processes byte allocate timeout and thread terminate */ 45 /* actions that require the byte pool data structures to be cleaned */ 46 /* up. */ 47 /* */ 48 /* INPUT */ 49 /* */ 50 /* thread_ptr Pointer to suspended thread's */ 51 /* control block */ 52 /* */ 53 /* OUTPUT */ 54 /* */ 55 /* None */ 56 /* */ 57 /* CALLS */ 58 /* */ 59 /* _tx_thread_system_resume Resume thread service */ 60 /* _tx_thread_system_ni_resume Non-interruptable resume thread */ 61 /* */ 62 /* CALLED BY */ 63 /* */ 64 /* _tx_thread_timeout Thread timeout processing */ 65 /* _tx_thread_terminate Thread terminate processing */ 66 /* _tx_thread_wait_abort Thread wait abort processing */ 67 /* */ 68 /* RELEASE HISTORY */ 69 /* */ 70 /* DATE NAME DESCRIPTION */ 71 /* */ 72 /* 05-19-2020 William E. Lamie Initial Version 6.0 */ 73 /* 09-30-2020 Yuxin Zhou Modified comment(s), */ 74 /* resulting in version 6.1 */ 75 /* */ 76 /**************************************************************************/ _tx_byte_pool_cleanup(TX_THREAD * thread_ptr,ULONG suspension_sequence)77VOID _tx_byte_pool_cleanup(TX_THREAD *thread_ptr, ULONG suspension_sequence) 78 { 79 80 #ifndef TX_NOT_INTERRUPTABLE 81 TX_INTERRUPT_SAVE_AREA 82 #endif 83 84 TX_BYTE_POOL *pool_ptr; 85 UINT suspended_count; 86 TX_THREAD *next_thread; 87 TX_THREAD *previous_thread; 88 89 90 #ifndef TX_NOT_INTERRUPTABLE 91 92 /* Disable interrupts to remove the suspended thread from the byte pool. */ 93 TX_DISABLE 94 95 /* Determine if the cleanup is still required. */ 96 if (thread_ptr -> tx_thread_suspend_cleanup == &(_tx_byte_pool_cleanup)) 97 { 98 99 /* Check for valid suspension sequence. */ 100 if (suspension_sequence == thread_ptr -> tx_thread_suspension_sequence) 101 { 102 103 /* Setup pointer to byte pool control block. */ 104 pool_ptr = TX_VOID_TO_BYTE_POOL_POINTER_CONVERT(thread_ptr -> tx_thread_suspend_control_block); 105 106 /* Check for a NULL byte pool pointer. */ 107 if (pool_ptr != TX_NULL) 108 { 109 110 /* Check for valid pool ID. */ 111 if (pool_ptr -> tx_byte_pool_id == TX_BYTE_POOL_ID) 112 { 113 114 /* Determine if there are any thread suspensions. */ 115 if (pool_ptr -> tx_byte_pool_suspended_count != TX_NO_SUSPENSIONS) 116 { 117 #else 118 119 /* Setup pointer to byte pool control block. */ 120 pool_ptr = TX_VOID_TO_BYTE_POOL_POINTER_CONVERT(thread_ptr -> tx_thread_suspend_control_block); 121 #endif 122 123 /* Thread suspended for memory... Clear the suspension cleanup flag. */ 124 thread_ptr -> tx_thread_suspend_cleanup = TX_NULL; 125 126 /* Decrement the suspension count. */ 127 pool_ptr -> tx_byte_pool_suspended_count--; 128 129 /* Pickup the suspended count. */ 130 suspended_count = pool_ptr -> tx_byte_pool_suspended_count; 131 132 /* Remove the suspended thread from the list. */ 133 134 /* See if this is the only suspended thread on the list. */ 135 if (suspended_count == TX_NO_SUSPENSIONS) 136 { 137 138 /* Yes, the only suspended thread. */ 139 140 /* Update the head pointer. */ 141 pool_ptr -> tx_byte_pool_suspension_list = TX_NULL; 142 } 143 else 144 { 145 146 /* At least one more thread is on the same suspension list. */ 147 148 /* Update the links of the adjacent threads. */ 149 next_thread = thread_ptr -> tx_thread_suspended_next; 150 previous_thread = thread_ptr -> tx_thread_suspended_previous; 151 next_thread -> tx_thread_suspended_previous = previous_thread; 152 previous_thread -> tx_thread_suspended_next = next_thread; 153 154 /* Determine if we need to update the head pointer. */ 155 if (pool_ptr -> tx_byte_pool_suspension_list == thread_ptr) 156 { 157 158 /* Update the list head pointer. */ 159 pool_ptr -> tx_byte_pool_suspension_list = next_thread; 160 } 161 } 162 163 /* Now we need to determine if this cleanup is from a terminate, timeout, 164 or from a wait abort. */ 165 if (thread_ptr -> tx_thread_state == TX_BYTE_MEMORY) 166 { 167 168 /* Timeout condition and the thread still suspended on the byte pool. 169 Setup return error status and resume the thread. */ 170 171 #ifdef TX_BYTE_POOL_ENABLE_PERFORMANCE_INFO 172 173 /* Increment the total timeouts counter. */ 174 _tx_byte_pool_performance_timeout_count++; 175 176 /* Increment the number of timeouts on this byte pool. */ 177 pool_ptr -> tx_byte_pool_performance_timeout_count++; 178 #endif 179 180 /* Setup return status. */ 181 thread_ptr -> tx_thread_suspend_status = TX_NO_MEMORY; 182 183 #ifdef TX_NOT_INTERRUPTABLE 184 185 /* Resume the thread! */ 186 _tx_thread_system_ni_resume(thread_ptr); 187 #else 188 189 /* Temporarily disable preemption. */ 190 _tx_thread_preempt_disable++; 191 192 /* Restore interrupts. */ 193 TX_RESTORE 194 195 /* Resume the thread! */ 196 _tx_thread_system_resume(thread_ptr); 197 198 /* Disable interrupts. */ 199 TX_DISABLE 200 #endif 201 } 202 #ifndef TX_NOT_INTERRUPTABLE 203 } 204 } 205 } 206 } 207 } 208 209 /* Restore interrupts. */ 210 TX_RESTORE 211 #endif 212 } 213 214