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