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_initialize.h"
29 #include "tx_thread.h"
30 #include "tx_timer.h"
31 #include "tx_event_flags.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _txe_event_flags_create                             PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function checks for errors in the event flag creation function */
47 /*    call.                                                               */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    group_ptr                         Pointer to event flags group      */
52 /*                                        control block                   */
53 /*    name_ptr                          Pointer to event flags name       */
54 /*    event_control_block_size          Size of event flags control block */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    TX_GROUP_ERROR                    Invalid event flag group pointer  */
59 /*    TX_CALLER_ERROR                   Invalid calling function          */
60 /*    status                            Actual completion status          */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _tx_event_flags_create            Actual create function            */
65 /*    _tx_thread_system_preempt_check   Check for preemption              */
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 /**************************************************************************/
_txe_event_flags_create(TX_EVENT_FLAGS_GROUP * group_ptr,CHAR * name_ptr,UINT event_control_block_size)80 UINT  _txe_event_flags_create(TX_EVENT_FLAGS_GROUP *group_ptr, CHAR *name_ptr, UINT event_control_block_size)
81 {
82 
83 TX_INTERRUPT_SAVE_AREA
84 
85 UINT                        status;
86 ULONG                       i;
87 TX_EVENT_FLAGS_GROUP        *next_group;
88 #ifndef TX_TIMER_PROCESS_IN_ISR
89 TX_THREAD                   *thread_ptr;
90 #endif
91 
92 
93     /* Default status to success.  */
94     status =  TX_SUCCESS;
95 
96     /* Check for an invalid event flags group pointer.  */
97     if (group_ptr == TX_NULL)
98     {
99 
100         /* Event flags group pointer is invalid, return appropriate error code.  */
101         status =  TX_GROUP_ERROR;
102     }
103 
104     /* Now check for proper control block size.  */
105     else if (event_control_block_size != (sizeof(TX_EVENT_FLAGS_GROUP)))
106     {
107 
108         /* Event flags group pointer is invalid, return appropriate error code.  */
109         status =  TX_GROUP_ERROR;
110     }
111     else
112     {
113 
114         /* Disable interrupts.  */
115         TX_DISABLE
116 
117         /* Increment the preempt disable flag.  */
118         _tx_thread_preempt_disable++;
119 
120         /* Restore interrupts.  */
121         TX_RESTORE
122 
123         /* Next see if it is already in the created list.  */
124         next_group =   _tx_event_flags_created_ptr;
125         for (i = ((ULONG) 0); i < _tx_event_flags_created_count; i++)
126         {
127 
128             /* Determine if this group matches the event flags group in the list.  */
129             if (group_ptr == next_group)
130             {
131 
132                 break;
133             }
134             else
135             {
136 
137                 /* Move to the next group.  */
138                 next_group =  next_group -> tx_event_flags_group_created_next;
139             }
140         }
141 
142         /* Disable interrupts.  */
143         TX_DISABLE
144 
145         /* Decrement the preempt disable flag.  */
146         _tx_thread_preempt_disable--;
147 
148         /* Restore interrupts.  */
149         TX_RESTORE
150 
151         /* Check for preemption.  */
152         _tx_thread_system_preempt_check();
153 
154         /* At this point, check to see if there is a duplicate event flag group.  */
155         if (group_ptr == next_group)
156         {
157 
158             /* Group is already created, return appropriate error code.  */
159             status =  TX_GROUP_ERROR;
160         }
161         else
162         {
163 
164 #ifndef TX_TIMER_PROCESS_IN_ISR
165 
166             /* Pickup thread pointer.  */
167             TX_THREAD_GET_CURRENT(thread_ptr)
168 
169             /* Check for invalid caller of this function.  First check for a calling thread.  */
170             if (thread_ptr == &_tx_timer_thread)
171             {
172 
173                 /* Invalid caller of this function, return appropriate error code.  */
174                 status =  TX_CALLER_ERROR;
175             }
176 #endif
177 
178             /* Check for interrupt call.  */
179             if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
180             {
181 
182                 /* Now, make sure the call is from an interrupt and not initialization.  */
183                 if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
184                 {
185 
186                     /* Invalid caller of this function, return appropriate error code.  */
187                     status =  TX_CALLER_ERROR;
188                 }
189             }
190         }
191     }
192 
193     /* Determine if everything is okay.  */
194     if (status == TX_SUCCESS)
195     {
196 
197         /* Call actual event flags create function.  */
198         status =  _tx_event_flags_create(group_ptr, name_ptr);
199     }
200 
201     /* Return completion status.  */
202     return(status);
203 }
204 
205