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