1 /* This test is designed to test event flag suspension with the suspended threads
2    being terminated.  */
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 
14 
15 static TX_EVENT_FLAGS_GROUP group_0;
16 
17 
18 /* Define thread prototypes.  */
19 
20 static void    thread_0_entry(ULONG thread_input);
21 static void    thread_1_entry(ULONG thread_input);
22 static void    thread_2_entry(ULONG thread_input);
23 
24 
25 /* Prototype for test control return.  */
26 void  test_control_return(UINT status);
27 
28 
event_set_notify(TX_EVENT_FLAGS_GROUP * group)29 static void event_set_notify(TX_EVENT_FLAGS_GROUP *group)
30 {
31 
32     /* Not necessary to do anything in this function.  */
33 }
34 
35 
36 /* Define what the initial system looks like.  */
37 
38 #ifdef CTEST
test_application_define(void * first_unused_memory)39 void test_application_define(void *first_unused_memory)
40 #else
41 void    threadx_event_flag_thread_terminate_application_define(void *first_unused_memory)
42 #endif
43 {
44 
45 UINT    status;
46 CHAR    *pointer;
47 
48 
49     /* Put first available memory address into a character pointer.  */
50     pointer =  (CHAR *) first_unused_memory;
51 
52     /* Put system definition stuff in here, e.g. thread creates and other assorted
53        create information.  */
54 
55     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
56             pointer, TEST_STACK_SIZE_PRINTF,
57             17, 17, 100, TX_AUTO_START);
58     pointer = pointer + TEST_STACK_SIZE_PRINTF;
59 
60     /* Check status.  */
61     if (status != TX_SUCCESS)
62     {
63 
64         printf("Running Event Flag Thread Terminate Test............................ ERROR #1\n");
65         test_control_return(1);
66     }
67 
68     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
69             pointer, TEST_STACK_SIZE_PRINTF,
70             16, 16, 100, TX_AUTO_START);
71     pointer = pointer + TEST_STACK_SIZE_PRINTF;
72 
73     /* Check status.  */
74     if (status != TX_SUCCESS)
75     {
76 
77         printf("Running Event Flag Thread Terminate Test............................ ERROR #2\n");
78         test_control_return(1);
79     }
80 
81     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 1,
82             pointer, TEST_STACK_SIZE_PRINTF,
83             16, 16, 100, TX_AUTO_START);
84     pointer = pointer + TEST_STACK_SIZE_PRINTF;
85 
86     /* Check status.  */
87     if (status != TX_SUCCESS)
88     {
89 
90         printf("Running Event Flag Thread Terminate Test............................ ERROR #3\n");
91         test_control_return(1);
92     }
93 
94     /* Create event flag group 0.  */
95     status =  tx_event_flags_create(&group_0, "group 0");
96 
97     /* Check status.  */
98     if (status != TX_SUCCESS)
99     {
100 
101         printf("Running Event Flag Thread Terminate Test............................ ERROR #4\n");
102         test_control_return(1);
103     }
104 
105     /* Register the event set notify function.  */
106     status =  tx_event_flags_set_notify(&group_0, event_set_notify);
107 
108 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
109 
110     /* Check status.  */
111     if (status != TX_SUCCESS)
112     {
113 
114         printf("Running Event Flag Thread Terminate Test............................ ERROR #5\n");
115         test_control_return(1);
116     }
117 #else
118 
119     /* Check status.  */
120     if (status != TX_FEATURE_NOT_ENABLED)
121     {
122 
123         printf("Running Event Flag Thread Terminate Test............................ ERROR #6\n");
124         test_control_return(1);
125     }
126 
127 #endif
128 
129 }
130 
131 
132 
133 /* Define the test threads.  */
134 
thread_0_entry(ULONG thread_input)135 static void    thread_0_entry(ULONG thread_input)
136 {
137 
138 UINT    status;
139 
140 
141     /* Inform user.  */
142     printf("Running Event Flag Thread Terminate Test............................ ");
143 
144     /* Increment run counter.  */
145     thread_0_counter++;
146 
147     /* Terminate thread 2.  */
148     status =  tx_thread_terminate(&thread_2);
149 
150     /* Check status.  */
151     if (status != TX_SUCCESS)
152     {
153 
154         /* Event flag error.  */
155         printf("ERROR #7\n");
156         test_control_return(1);
157     }
158 
159     /* Terminate thread 1.  */
160     status =  tx_thread_terminate(&thread_1);
161 
162     /* Check status.  */
163     if (status != TX_SUCCESS)
164     {
165 
166         /* Event flag error.  */
167         printf("ERROR #8\n");
168         test_control_return(1);
169     }
170 
171     /* Set event flags to make sure no threads are suspended.  */
172     status =  tx_event_flags_set(&group_0, 0xFFFFFFFF, TX_OR);
173 
174     /* Check status and run counters.  */
175     if ((status != TX_SUCCESS) || (thread_1_counter != 1) || (thread_2_counter != 1))
176     {
177 
178         /* Event flag error.  */
179         printf("ERROR #9\n");
180         test_control_return(1);
181     }
182 
183     /* Successful test.  */
184     printf("SUCCESS!\n");
185     test_control_return(0);
186 }
187 
188 
thread_1_entry(ULONG thread_input)189 static void    thread_1_entry(ULONG thread_input)
190 {
191 
192 UINT    status;
193 ULONG   actual_events;
194 
195     /* Wait for event flags.  */
196     while(1)
197     {
198 
199         /* Increment run counter.  */
200         thread_1_counter++;
201 
202         /* Attempt to get events from event flag group.  AND option.  */
203         status =  tx_event_flags_get(&group_0, 0x80000000, TX_AND_CLEAR, &actual_events, TX_WAIT_FOREVER);
204 
205         /* Check status.  */
206         if (status != TX_NO_EVENTS)
207             return;
208     }
209 }
210 
211 
thread_2_entry(ULONG thread_input)212 static void    thread_2_entry(ULONG thread_input)
213 {
214 
215 UINT    status;
216 //ULONG   event_flag = 1;
217 ULONG   actual_events;
218 
219     /* Wait for event flags.  */
220     while(1)
221     {
222 
223         /* Increment run counter.  */
224         thread_2_counter++;
225 
226         /* Attempt to get events from event flag group.  AND option.  */
227         status =  tx_event_flags_get(&group_0, 0x00008000, TX_AND_CLEAR, &actual_events, TX_WAIT_FOREVER);
228 
229         /* Check status.  */
230         if (status != TX_NO_EVENTS)
231             return;
232     }
233 }
234 
235