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 /**   Block 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_block_pool.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _tx_block_release                                   PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function returns a previously allocated block to its           */
47 /*    associated memory block pool.                                       */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    block_ptr                         Pointer to memory block           */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    TX_SUCCESS                        Successful completion status      */
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 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_tx_block_release(VOID * block_ptr)75 UINT  _tx_block_release(VOID *block_ptr)
76 {
77 
78 TX_INTERRUPT_SAVE_AREA
79 
80 TX_BLOCK_POOL       *pool_ptr;
81 TX_THREAD           *thread_ptr;
82 UCHAR               *work_ptr;
83 UCHAR               **return_block_ptr;
84 UCHAR               **next_block_ptr;
85 UINT                suspended_count;
86 TX_THREAD           *next_thread;
87 TX_THREAD           *previous_thread;
88 
89 
90     /* Disable interrupts to put this block back in the pool.  */
91     TX_DISABLE
92 
93     /* Pickup the pool pointer which is just previous to the starting
94        address of the block that the caller sees.  */
95     work_ptr =        TX_VOID_TO_UCHAR_POINTER_CONVERT(block_ptr);
96     work_ptr =        TX_UCHAR_POINTER_SUB(work_ptr, (sizeof(UCHAR *)));
97     next_block_ptr =  TX_UCHAR_TO_INDIRECT_UCHAR_POINTER_CONVERT(work_ptr);
98     pool_ptr =        TX_UCHAR_TO_BLOCK_POOL_POINTER_CONVERT((*next_block_ptr));
99 
100 #ifdef TX_BLOCK_POOL_ENABLE_PERFORMANCE_INFO
101 
102     /* Increment the total releases counter.  */
103     _tx_block_pool_performance_release_count++;
104 
105     /* Increment the number of releases on this pool.  */
106     pool_ptr -> tx_block_pool_performance_release_count++;
107 #endif
108 
109     /* If trace is enabled, insert this event into the trace buffer.  */
110     TX_TRACE_IN_LINE_INSERT(TX_TRACE_BLOCK_RELEASE, pool_ptr, TX_POINTER_TO_ULONG_CONVERT(block_ptr), pool_ptr -> tx_block_pool_suspended_count, TX_POINTER_TO_ULONG_CONVERT(&work_ptr), TX_TRACE_BLOCK_POOL_EVENTS)
111 
112     /* Log this kernel call.  */
113     TX_EL_BLOCK_RELEASE_INSERT
114 
115     /* Determine if there are any threads suspended on the block pool.  */
116     thread_ptr =  pool_ptr -> tx_block_pool_suspension_list;
117     if (thread_ptr != TX_NULL)
118     {
119 
120         /* Remove the suspended thread from the list.  */
121 
122         /* Decrement the number of threads suspended.  */
123         (pool_ptr -> tx_block_pool_suspended_count)--;
124 
125         /* Pickup the suspended count.  */
126         suspended_count =  (pool_ptr -> tx_block_pool_suspended_count);
127 
128         /* See if this is the only suspended thread on the list.  */
129         if (suspended_count == TX_NO_SUSPENSIONS)
130         {
131 
132             /* Yes, the only suspended thread.  */
133 
134             /* Update the head pointer.  */
135             pool_ptr -> tx_block_pool_suspension_list =  TX_NULL;
136         }
137         else
138         {
139 
140             /* At least one more thread is on the same expiration list.  */
141 
142             /* Update the list head pointer.  */
143             next_thread =                                thread_ptr -> tx_thread_suspended_next;
144             pool_ptr -> tx_block_pool_suspension_list =  next_thread;
145 
146             /* Update the links of the adjacent threads.  */
147             previous_thread =                              thread_ptr -> tx_thread_suspended_previous;
148             next_thread -> tx_thread_suspended_previous =  previous_thread;
149             previous_thread -> tx_thread_suspended_next =  next_thread;
150         }
151 
152         /* Prepare for resumption of the first thread.  */
153 
154         /* Clear cleanup routine to avoid timeout.  */
155         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
156 
157         /* Return this block pointer to the suspended thread waiting for
158            a block.  */
159         return_block_ptr =  TX_VOID_TO_INDIRECT_UCHAR_POINTER_CONVERT(thread_ptr -> tx_thread_additional_suspend_info);
160         work_ptr =          TX_VOID_TO_UCHAR_POINTER_CONVERT(block_ptr);
161         *return_block_ptr =  work_ptr;
162 
163         /* Put return status into the thread control block.  */
164         thread_ptr -> tx_thread_suspend_status =  TX_SUCCESS;
165 
166 #ifdef TX_NOT_INTERRUPTABLE
167 
168         /* Resume the thread!  */
169         _tx_thread_system_ni_resume(thread_ptr);
170 
171         /* Restore interrupts.  */
172         TX_RESTORE
173 #else
174 
175         /* Temporarily disable preemption.  */
176         _tx_thread_preempt_disable++;
177 
178         /* Restore interrupts.  */
179         TX_RESTORE
180 
181         /* Resume thread.  */
182         _tx_thread_system_resume(thread_ptr);
183 #endif
184     }
185     else
186     {
187 
188         /* No thread is suspended for a memory block.  */
189 
190         /* Put the block back in the available list.  */
191         *next_block_ptr =  pool_ptr -> tx_block_pool_available_list;
192 
193         /* Adjust the head pointer.  */
194         pool_ptr -> tx_block_pool_available_list =  work_ptr;
195 
196         /* Increment the count of available blocks.  */
197         pool_ptr -> tx_block_pool_available++;
198 
199         /* Restore interrupts.  */
200         TX_RESTORE
201     }
202 
203     /* Return successful completion status.  */
204     return(TX_SUCCESS);
205 }
206 
207