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 /**   Event Flags                                                         */
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_event_flags.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _tx_event_flags_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 event flag group.  All threads  */
46 /*    suspended on the group are resumed with the TX_DELETED status       */
47 /*    code.                                                               */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    group_ptr                         Pointer to group 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_event_flags_delete(TX_EVENT_FLAGS_GROUP * group_ptr)76 UINT  _tx_event_flags_delete(TX_EVENT_FLAGS_GROUP *group_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_EVENT_FLAGS_GROUP    *next_group;
85 TX_EVENT_FLAGS_GROUP    *previous_group;
86 
87 
88     /* Disable interrupts to remove the group 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_EVENT_FLAGS_DELETE, group_ptr, TX_POINTER_TO_ULONG_CONVERT(&thread_ptr), 0, 0, TX_TRACE_EVENT_FLAGS_EVENTS)
93 
94     /* Optional event flags group delete extended processing.  */
95     TX_EVENT_FLAGS_GROUP_DELETE_EXTENSION(group_ptr)
96 
97     /* If trace is enabled, unregister this object.  */
98     TX_TRACE_OBJECT_UNREGISTER(group_ptr)
99 
100     /* Log this kernel call.  */
101     TX_EL_EVENT_FLAGS_DELETE_INSERT
102 
103     /* Clear the event flag group ID to make it invalid.  */
104     group_ptr -> tx_event_flags_group_id =  TX_CLEAR_ID;
105 
106     /* Decrement the number of created event flag groups.  */
107     _tx_event_flags_created_count--;
108 
109     /* See if this group is the only one on the list.  */
110     if (_tx_event_flags_created_count == TX_EMPTY)
111     {
112 
113         /* Only created event flag group, just set the created list to NULL.  */
114         _tx_event_flags_created_ptr =  TX_NULL;
115     }
116     else
117     {
118 
119         /* Link-up the neighbors.  */
120         next_group =                                           group_ptr -> tx_event_flags_group_created_next;
121         previous_group =                                       group_ptr -> tx_event_flags_group_created_previous;
122         next_group -> tx_event_flags_group_created_previous =  previous_group;
123         previous_group -> tx_event_flags_group_created_next =  next_group;
124 
125         /* See if we have to update the created list head pointer.  */
126         if (_tx_event_flags_created_ptr == group_ptr)
127         {
128 
129             /* Yes, move the head pointer to the next link. */
130             _tx_event_flags_created_ptr =  next_group;
131         }
132     }
133 
134     /* Temporarily disable preemption.  */
135     _tx_thread_preempt_disable++;
136 
137     /* Pickup the suspension information.  */
138     thread_ptr =                                         group_ptr -> tx_event_flags_group_suspension_list;
139     group_ptr -> tx_event_flags_group_suspension_list =  TX_NULL;
140     suspended_count =                                    group_ptr -> tx_event_flags_group_suspended_count;
141     group_ptr -> tx_event_flags_group_suspended_count =  TX_NO_SUSPENSIONS;
142 
143     /* Restore interrupts.  */
144     TX_RESTORE
145 
146     /* Walk through the event flag suspension list to resume any and all threads
147        suspended on this group.  */
148     while (suspended_count != TX_NO_SUSPENSIONS)
149     {
150 
151         /* Decrement the number of suspended threads.  */
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_EVENT_FLAGS_GROUP_DELETE_PORT_COMPLETION(group_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