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