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