1 /* This test is designed to test event flag suspension and resumption of three threads
2    waiting on the same event flag set.  */
3 
4 #include   <stdio.h>
5 #include   "tx_api.h"
6 
7 static unsigned long   thread_0_counter =  0;
8 static TX_THREAD       thread_0;
9 static unsigned long   thread_1_counter =  0;
10 static TX_THREAD       thread_1;
11 static unsigned long   thread_2_counter =  0;
12 static TX_THREAD       thread_2;
13 static unsigned long   thread_3_counter =  0;
14 static TX_THREAD       thread_3;
15 static unsigned long   thread_4_counter =  0;
16 static TX_THREAD       thread_4;
17 
18 
19 static TX_EVENT_FLAGS_GROUP group_0;
20 
21 
22 /* Define thread prototypes.  */
23 
24 static void    thread_0_entry(ULONG thread_input);
25 static void    thread_1_entry(ULONG thread_input);
26 static void    thread_2_entry(ULONG thread_input);
27 static void    thread_3_entry(ULONG thread_input);
28 static void    thread_4_entry(ULONG thread_input);
29 
30 
31 /* Prototype for test control return.  */
32 void  test_control_return(UINT status);
33 
34 
event_set_notify(TX_EVENT_FLAGS_GROUP * group)35 static void event_set_notify(TX_EVENT_FLAGS_GROUP *group)
36 {
37 
38     /* Not necessary to do anything in this function.  */
39 }
40 
41 
42 /* Define what the initial system looks like.  */
43 
44 #ifdef CTEST
test_application_define(void * first_unused_memory)45 void test_application_define(void *first_unused_memory)
46 #else
47 void    threadx_event_flag_suspension_application_define(void *first_unused_memory)
48 #endif
49 {
50 
51 UINT    status;
52 CHAR    *pointer;
53 
54 
55     /* Put first available memory address into a character pointer.  */
56     pointer =  (CHAR *) first_unused_memory;
57 
58     /* Put system definition stuff in here, e.g. thread creates and other assorted
59        create information.  */
60 
61     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
62             pointer, TEST_STACK_SIZE_PRINTF,
63             17, 17, 100, TX_AUTO_START);
64     pointer = pointer + TEST_STACK_SIZE_PRINTF;
65 
66     /* Check status.  */
67     if (status != TX_SUCCESS)
68     {
69 
70         printf("Running Event Flag Suspension Same Bit Test......................... ERROR #1\n");
71         test_control_return(1);
72     }
73 
74     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
75             pointer, TEST_STACK_SIZE_PRINTF,
76             16, 16, 100, TX_AUTO_START);
77     pointer = pointer + TEST_STACK_SIZE_PRINTF;
78 
79     /* Check status.  */
80     if (status != TX_SUCCESS)
81     {
82 
83         printf("Running Event Flag Suspension Same Bit Test......................... ERROR #2\n");
84         test_control_return(1);
85     }
86 
87     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 1,
88             pointer, TEST_STACK_SIZE_PRINTF,
89             16, 16, 100, TX_AUTO_START);
90     pointer = pointer + TEST_STACK_SIZE_PRINTF;
91 
92     /* Check status.  */
93     if (status != TX_SUCCESS)
94     {
95 
96         printf("Running Event Flag Suspension Same Bit Test......................... ERROR #3\n");
97         test_control_return(1);
98     }
99 
100     status =  tx_thread_create(&thread_3, "thread 3", thread_3_entry, 1,
101             pointer, TEST_STACK_SIZE_PRINTF,
102             16, 16, 100, TX_AUTO_START);
103     pointer = pointer + TEST_STACK_SIZE_PRINTF;
104 
105     /* Check status.  */
106     if (status != TX_SUCCESS)
107     {
108 
109         printf("Running Event Flag Suspension Same Bit Test......................... ERROR #4\n");
110         test_control_return(1);
111     }
112 
113     status =  tx_thread_create(&thread_4, "thread 4", thread_4_entry, 4,
114             pointer, TEST_STACK_SIZE_PRINTF,
115             16, 16, 100, TX_DONT_START);
116     pointer = pointer + TEST_STACK_SIZE_PRINTF;
117 
118     /* Check status.  */
119     if (status != TX_SUCCESS)
120     {
121 
122         printf("Running Event Flag Suspension Same Bit Test......................... ERROR #5\n");
123         test_control_return(1);
124     }
125 
126     /* Create event flag group 0.  */
127     status =  tx_event_flags_create(&group_0, "group 0");
128 
129     /* Check status.  */
130     if (status != TX_SUCCESS)
131     {
132 
133         printf("Running Event Flag Suspension Same Bit Test......................... ERROR #6\n");
134         test_control_return(1);
135     }
136 
137     /* Register the event set notify function.  */
138     status =  tx_event_flags_set_notify(&group_0, event_set_notify);
139 
140 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
141 
142     /* Check status.  */
143     if (status != TX_SUCCESS)
144     {
145 
146         printf("Running Event Flag Suspension Same Bit Test......................... ERROR #7\n");
147         test_control_return(1);
148     }
149 #else
150 
151     /* Check status.  */
152     if (status != TX_FEATURE_NOT_ENABLED)
153     {
154 
155         printf("Running Event Flag Suspension Same Bit Test......................... ERROR #8\n");
156         test_control_return(1);
157     }
158 
159 #endif
160 
161 }
162 
163 
164 
165 /* Define the test threads.  */
166 
thread_0_entry(ULONG thread_input)167 static void    thread_0_entry(ULONG thread_input)
168 {
169 
170 UINT    status;
171 ULONG   event_flag = 1;
172 int     i;
173 
174 
175     /* Inform user.  */
176     printf("Running Event Flag Suspension Same Bit Test......................... ");
177 
178     /* Set all event flags.  */
179     for (i = 0; i < 32; i++)
180     {
181 
182         /* Increment run counter.  */
183         thread_0_counter++;
184 
185         /* Set event flag.  */
186         status =  tx_event_flags_set(&group_0, event_flag, TX_OR);
187 
188         /* Check status.  */
189         if (status != TX_SUCCESS)
190         {
191 
192             /* Event flag error.  */
193             printf("ERROR #9\n");
194             test_control_return(1);
195         }
196 
197         /* Check the thread counters...  */
198         if ((i < 31) && ((thread_1_counter != 1) || (thread_2_counter != 1) || (thread_3_counter != 1)))
199         {
200 
201             /* Event flag error.  */
202             printf("ERROR #10\n");
203             test_control_return(1);
204         }
205         if ((i == 31) && ((thread_1_counter != 2) || (thread_2_counter != 2) || (thread_3_counter != 2)))
206         {
207 
208             /* Event flag error.  */
209             printf("ERROR #11\n");
210             test_control_return(1);
211         }
212 
213         /* Shift event flag up one bit.  */
214         event_flag =  event_flag << 1;
215 
216         /* Check for 0.  */
217         if (event_flag == 0)
218             event_flag = 1;
219     }
220 
221     /* Set the event flags to 0.  */
222     status =  tx_event_flags_set(&group_0, 0x0, TX_AND);
223 
224     /* Resume thread 4 so it can suspend on the event flag group too.  */
225     status +=  tx_thread_resume(&thread_4);
226 
227     /* Determine if there was an error.  */
228     if ((status != TX_SUCCESS) || (thread_4_counter != 1))
229     {
230 
231         /* Event flag error.  */
232         printf("ERROR #11\n");
233         test_control_return(1);
234     }
235 
236     /* Now set the event flag that only thread 4 is waiting on to ensure that we are not satisfying the first thread on the list.  */
237     status =  tx_event_flags_set(&group_0, 0x1, TX_OR);
238 
239     /* Determine if there was an error.  */
240     if ((status != TX_SUCCESS) || (thread_4_counter != 2))
241     {
242 
243         /* Event flag error.  */
244         printf("ERROR #12\n");
245         test_control_return(1);
246     }
247 
248     /* Successful test.  */
249     printf("SUCCESS!\n");
250     test_control_return(0);
251 }
252 
253 
thread_1_entry(ULONG thread_input)254 static void    thread_1_entry(ULONG thread_input)
255 {
256 
257 UINT    status;
258 //ULONG   event_flag = 1;
259 ULONG   actual_events;
260 
261     /* Wait for event flags.  */
262     while(1)
263     {
264 
265         /* Increment run counter.  */
266         thread_1_counter++;
267 
268         /* Attempt to get events from event flag group.  AND option.  */
269         status =  tx_event_flags_get(&group_0, 0x80008000, TX_AND, &actual_events, TX_WAIT_FOREVER);
270 
271         /* Check status.  */
272         if ((status != TX_SUCCESS) || (actual_events != 0xFFFFFFFFUL))
273             return;
274 
275         /* Clear the event flags. */
276         tx_event_flags_set(&group_0, 0, TX_AND);
277     }
278 }
279 
280 
thread_2_entry(ULONG thread_input)281 static void    thread_2_entry(ULONG thread_input)
282 {
283 
284 UINT    status;
285 //ULONG   event_flag = 1;
286 ULONG   actual_events;
287 
288     /* Wait for event flags.  */
289     while(1)
290     {
291 
292         /* Increment run counter.  */
293         thread_2_counter++;
294 
295         /* Attempt to get events from event flag group.  AND option.  */
296         status =  tx_event_flags_get(&group_0, 0x80008000, TX_AND, &actual_events, TX_WAIT_FOREVER);
297 
298         /* Check status.  */
299         if ((status != TX_SUCCESS) || (actual_events != 0xFFFFFFFFUL))
300             return;
301     }
302 }
303 
304 
thread_3_entry(ULONG thread_input)305 static void    thread_3_entry(ULONG thread_input)
306 {
307 
308 UINT    status;
309 //ULONG   event_flag = 1;
310 ULONG   actual_events;
311 
312     /* Wait for event flags.  */
313     while(1)
314     {
315 
316         /* Increment run counter.  */
317         thread_3_counter++;
318 
319         /* Attempt to get events from event flag group.  AND option.  */
320         status =  tx_event_flags_get(&group_0, 0x80008000, TX_AND, &actual_events, TX_WAIT_FOREVER);
321 
322         /* Check status.  */
323         if ((status != TX_SUCCESS) || (actual_events != 0xFFFFFFFFUL))
324             return;
325     }
326 }
327 
328 
thread_4_entry(ULONG thread_input)329 static void    thread_4_entry(ULONG thread_input)
330 {
331 
332 UINT    status;
333 ULONG   actual_events;
334 
335     /* Wait for event flags.  */
336     while(1)
337     {
338 
339         /* Increment run counter.  */
340         thread_4_counter++;
341 
342         /* Attempt to get events from event flag group.  OR option.  */
343         status =  tx_event_flags_get(&group_0, 0x00000001, TX_OR_CLEAR, &actual_events, TX_WAIT_FOREVER);
344 
345         /* Check status.  */
346         if ((status != TX_SUCCESS) || (actual_events != 0x1UL))
347             return;
348     }
349 }
350