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 Pool                                                           */
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_trace.h"
29 #include "tx_thread.h"
30 #include "tx_byte_pool.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _tx_byte_pool_delete                                PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function deletes the specified byte pool.  All threads         */
46 /*    suspended on the byte pool are resumed with the TX_DELETED status   */
47 /*    code.                                                               */
48 /*                                                                        */
49 /*    It is important to note that the byte pool being deleted, or the    */
50 /*    memory associated with it should not be in use when this function   */
51 /*    is called.                                                          */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    pool_ptr                          Pointer to pool control block     */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    TX_SUCCESS                        Successful completion status      */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    _tx_thread_system_preempt_check   Check for preemption              */
64 /*    _tx_thread_system_resume          Resume thread service             */
65 /*    _tx_thread_system_ni_resume       Non-interruptable resume thread   */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application Code                                                    */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
76 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_tx_byte_pool_delete(TX_BYTE_POOL * pool_ptr)80 UINT  _tx_byte_pool_delete(TX_BYTE_POOL *pool_ptr)
81 {
82 
83 TX_INTERRUPT_SAVE_AREA
84 
85 TX_THREAD       *thread_ptr;
86 TX_THREAD       *next_thread;
87 UINT            suspended_count;
88 TX_BYTE_POOL    *next_pool;
89 TX_BYTE_POOL    *previous_pool;
90 
91 
92     /* Disable interrupts to remove the byte pool from the created list.  */
93     TX_DISABLE
94 
95     /* If trace is enabled, insert this event into the trace buffer.  */
96     TX_TRACE_IN_LINE_INSERT(TX_TRACE_BYTE_POOL_DELETE, pool_ptr, TX_POINTER_TO_ULONG_CONVERT(&thread_ptr), 0, 0, TX_TRACE_BYTE_POOL_EVENTS)
97 
98     /* Optional byte pool delete extended processing.  */
99     TX_BYTE_POOL_DELETE_EXTENSION(pool_ptr)
100 
101     /* If trace is enabled, unregister this object.  */
102     TX_TRACE_OBJECT_UNREGISTER(pool_ptr)
103 
104     /* Log this kernel call.  */
105     TX_EL_BYTE_POOL_DELETE_INSERT
106 
107     /* Clear the byte pool ID to make it invalid.  */
108     pool_ptr -> tx_byte_pool_id =  TX_CLEAR_ID;
109 
110     /* Decrement the number of byte pools created.  */
111     _tx_byte_pool_created_count--;
112 
113     /* See if the byte pool is the only one on the list.  */
114     if (_tx_byte_pool_created_count == TX_EMPTY)
115     {
116 
117         /* Only created byte pool, just set the created list to NULL.  */
118         _tx_byte_pool_created_ptr =  TX_NULL;
119     }
120     else
121     {
122 
123         /* Link-up the neighbors.  */
124         next_pool =                                   pool_ptr -> tx_byte_pool_created_next;
125         previous_pool =                               pool_ptr -> tx_byte_pool_created_previous;
126         next_pool -> tx_byte_pool_created_previous =  previous_pool;
127         previous_pool -> tx_byte_pool_created_next =  next_pool;
128 
129         /* See if we have to update the created list head pointer.  */
130         if (_tx_byte_pool_created_ptr == pool_ptr)
131         {
132 
133             /* Yes, move the head pointer to the next link. */
134             _tx_byte_pool_created_ptr =  next_pool;
135         }
136     }
137 
138     /* Temporarily disable preemption.  */
139     _tx_thread_preempt_disable++;
140 
141     /* Pickup the suspension information.  */
142     thread_ptr =                                pool_ptr -> tx_byte_pool_suspension_list;
143     pool_ptr -> tx_byte_pool_suspension_list =  TX_NULL;
144     suspended_count =                           pool_ptr -> tx_byte_pool_suspended_count;
145     pool_ptr -> tx_byte_pool_suspended_count =  TX_NO_SUSPENSIONS;
146 
147     /* Restore interrupts.  */
148     TX_RESTORE
149 
150     /* Walk through the byte pool list to resume any and all threads suspended
151        on this byte pool.  */
152     while (suspended_count != TX_NO_SUSPENSIONS)
153     {
154 
155         /* Decrement the suspension count.  */
156         suspended_count--;
157 
158         /* Lockout interrupts.  */
159         TX_DISABLE
160 
161         /* Clear the cleanup pointer, this prevents the timeout from doing
162            anything.  */
163         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
164 
165         /* Set the return status in the thread to TX_DELETED.  */
166         thread_ptr -> tx_thread_suspend_status =  TX_DELETED;
167 
168         /* Move the thread pointer ahead.  */
169         next_thread =  thread_ptr -> tx_thread_suspended_next;
170 
171 #ifdef TX_NOT_INTERRUPTABLE
172 
173         /* Resume the thread!  */
174         _tx_thread_system_ni_resume(thread_ptr);
175 
176         /* Restore interrupts.  */
177         TX_RESTORE
178 #else
179 
180         /* Temporarily disable preemption again.  */
181         _tx_thread_preempt_disable++;
182 
183         /* Restore interrupts.  */
184         TX_RESTORE
185 
186         /* Resume the thread.  */
187         _tx_thread_system_resume(thread_ptr);
188 #endif
189 
190         /* Move to next thread.  */
191         thread_ptr =  next_thread;
192     }
193 
194     /* Execute Port-Specific completion processing. If needed, it is typically defined in tx_port.h.  */
195     TX_BYTE_POOL_DELETE_PORT_COMPLETION(pool_ptr)
196 
197     /* Disable interrupts.  */
198     TX_DISABLE
199 
200     /* Release previous preempt disable.  */
201     _tx_thread_preempt_disable--;
202 
203     /* Restore interrupts.  */
204     TX_RESTORE
205 
206     /* Check for preemption.  */
207     _tx_thread_system_preempt_check();
208 
209     /* Return TX_SUCCESS.  */
210     return(TX_SUCCESS);
211 }
212 
213