1 /* This test is designed to see if multiple threads can be created and suspend.
2    The order the suspension and resumption occurs makes sure everything is working
3    right. All the counters should increment at the same rate.  */
4 
5 #include   <stdio.h>
6 #include   "tx_api.h"
7 #include   "tx_thread.h"
8 
9 
10 static unsigned long   thread_0_counter = 0;
11 static TX_THREAD       thread_0;
12 
13 static unsigned long   thread_1_counter = 0;
14 static TX_THREAD       thread_1;
15 
16 static unsigned long   thread_2_counter = 0;
17 static TX_THREAD       thread_2;
18 
19 static unsigned long   thread_3_counter = 0;
20 static TX_THREAD       thread_3;
21 
22 static unsigned long   thread_4_counter = 0;
23 static TX_THREAD       thread_4;
24 
25 static unsigned long   thread_5_counter = 0;
26 static TX_THREAD       thread_5;
27 
28 
29 /* Define thread prototypes.  */
30 
31 static void    thread_0_entry(ULONG thread_input);
32 static void    thread_1_entry(ULONG thread_input);
33 static void    thread_2_entry(ULONG thread_input);
34 static void    thread_3_entry(ULONG thread_input);
35 static void    thread_4_entry(ULONG thread_input);
36 static void    thread_5_entry(ULONG thread_input);
37 
38 
39 /* Prototype for test control return.  */
40 void  test_control_return(UINT status);
41 
42 
43 /* Define what the initial system looks like.  */
44 
45 #ifdef CTEST
test_application_define(void * first_unused_memory)46 void test_application_define(void *first_unused_memory)
47 #else
48 void    threadx_thread_multiple_suspension_application_define(void *first_unused_memory)
49 #endif
50 {
51 
52 UINT    status;
53 CHAR    *pointer;
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     /* Create thread 0.  */
62     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
63             pointer, TEST_STACK_SIZE_PRINTF,
64             13, 13, TX_NO_TIME_SLICE, TX_AUTO_START);
65     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
66 
67     /* Check for status.  */
68     if (status != TX_SUCCESS)
69     {
70 
71         printf("Running Thread Multiple Thread Suspend/Resume Test.................. ERROR #1\n");
72         test_control_return(1);
73     }
74 
75     /* Create thread 1.  */
76     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
77             pointer, TEST_STACK_SIZE_PRINTF,
78             (TX_MAX_PRIORITIES/2), (TX_MAX_PRIORITIES/2), TX_NO_TIME_SLICE, TX_AUTO_START);
79     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
80 
81     /* Check for status.  */
82     if (status != TX_SUCCESS)
83     {
84 
85         printf("Running Thread Multiple Thread Suspend/Resume Test.................. ERROR #2\n");
86         test_control_return(1);
87     }
88 
89     /* Create thread 2.  */
90     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 1,
91             pointer, TEST_STACK_SIZE_PRINTF,
92             (TX_MAX_PRIORITIES/2), (TX_MAX_PRIORITIES/2), TX_NO_TIME_SLICE, TX_AUTO_START);
93     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
94 
95     /* Check for status.  */
96     if (status != TX_SUCCESS)
97     {
98 
99         printf("Running Thread Multiple Thread Suspend/Resume Test.................. ERROR #3\n");
100         test_control_return(1);
101     }
102 
103     /* Create thread 3.  */
104     status =  tx_thread_create(&thread_3, "thread 3", thread_3_entry, 1,
105             pointer, TEST_STACK_SIZE_PRINTF,
106             (TX_MAX_PRIORITIES/2), (TX_MAX_PRIORITIES/2), TX_NO_TIME_SLICE, TX_AUTO_START);
107     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
108 
109     /* Check for status.  */
110     if (status != TX_SUCCESS)
111     {
112 
113         printf("Running Thread Multiple Thread Suspend/Resume Test.................. ERROR #4\n");
114         test_control_return(1);
115     }
116 
117     /* Create thread 4.  */
118     status =  tx_thread_create(&thread_4, "thread 4", thread_4_entry, 1,
119             pointer, TEST_STACK_SIZE_PRINTF,
120             15, 15, TX_NO_TIME_SLICE, TX_AUTO_START);
121     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
122 
123     /* Check for status.  */
124     if (status != TX_SUCCESS)
125     {
126 
127         printf("Running Thread Multiple Thread Suspend/Resume Test.................. ERROR #5\n");
128         test_control_return(1);
129     }
130 
131     /* Create thread 5.  Make this thread non-preemptable for the range of priorities here...  */
132     status =  tx_thread_create(&thread_5, "thread 5", thread_5_entry, 1,
133             pointer, TEST_STACK_SIZE_PRINTF,
134             (TX_MAX_PRIORITIES-1), 13, TX_NO_TIME_SLICE, TX_AUTO_START);
135     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
136 
137     /* Check for status.  */
138     if (status != TX_SUCCESS)
139     {
140 
141         printf("Running Thread Multiple Thread Suspend/Resume Test.................. ERROR #6\n");
142         test_control_return(1);
143     }
144 }
145 
146 
147 
148 /* Define the test threads.  */
149 
thread_0_entry(ULONG thread_input)150 static void    thread_0_entry(ULONG thread_input)
151 {
152 
153 
154     /* Enter into a forever loop.  */
155     while(1)
156     {
157 
158         /* Increment thread 0 counter.  */
159         thread_0_counter++;
160 
161         /* Suspend this thread... */
162         tx_thread_suspend(&thread_0);
163 
164         /* Resume thread 5...  */
165         tx_thread_resume(&thread_5);
166     }
167 }
168 
169 
thread_1_entry(ULONG thread_input)170 static void    thread_1_entry(ULONG thread_input)
171 {
172 
173 
174     /* Enter into a forever loop.  */
175     while(1)
176     {
177 
178         /* Increment thread 1 counter.  */
179         thread_1_counter++;
180 
181         /* Suspend this thread.  */
182         tx_thread_suspend(&thread_1);
183     }
184 }
185 
186 
thread_2_entry(ULONG thread_input)187 static void    thread_2_entry(ULONG thread_input)
188 {
189 
190 
191     /* Enter into a forever loop.  */
192     while(1)
193     {
194 
195         /* Increment thread 2 counter.  */
196         thread_2_counter++;
197 
198         /* Suspend this thread.  */
199         tx_thread_suspend(&thread_2);
200     }
201 }
202 
thread_3_entry(ULONG thread_input)203 static void    thread_3_entry(ULONG thread_input)
204 {
205 
206 
207     /* Enter into a forever loop.  */
208     while(1)
209     {
210 
211         /* Increment thread 3 counter.  */
212         thread_3_counter++;
213 
214         /* Suspend this thread.  */
215         tx_thread_suspend(&thread_3);
216     }
217 }
218 
thread_4_entry(ULONG thread_input)219 static void    thread_4_entry(ULONG thread_input)
220 {
221 
222 
223     /* Enter into a forever loop.  */
224     while(1)
225     {
226 
227         /* Increment thread 4 counter.  */
228         thread_4_counter++;
229 
230         /* Suspend this thread.  */
231         tx_thread_suspend(&thread_4);
232     }
233 }
234 
thread_5_entry(ULONG thread_input)235 static void    thread_5_entry(ULONG thread_input)
236 {
237 
238 UINT    status;
239 
240     /* Inform user.  */
241     printf("Running Thread Multiple Thread Suspend/Resume Test.................. ");
242 
243     /* Determine if all the other threads are in a suspended state.  */
244     if ((thread_0.tx_thread_state != TX_SUSPENDED) || (thread_1.tx_thread_state != TX_SUSPENDED) ||
245         (thread_2.tx_thread_state != TX_SUSPENDED) || (thread_3.tx_thread_state != TX_SUSPENDED) ||
246         (thread_4.tx_thread_state != TX_SUSPENDED))
247     {
248 
249         /* Thread Suspend error.  */
250         printf("ERROR #7\n");
251         test_control_return(1);
252     }
253 
254     /* Make sure that each thread has run once.  */
255     if ((thread_0_counter != 1) || (thread_1_counter != 1) ||
256         (thread_2_counter != 1) || (thread_3_counter != 1) ||
257         (thread_4_counter != 1))
258     {
259 
260         /* Thread Suspend error.  */
261         printf("ERROR #8\n");
262         test_control_return(1);
263     }
264 
265     /* Resume all of the threads.  */
266     status =  tx_thread_resume(&thread_0);
267 
268     /* Determine if all the other threads are in the proper state.  */
269     if ((thread_0.tx_thread_state != TX_READY)     || (thread_1.tx_thread_state != TX_SUSPENDED) ||
270         (thread_2.tx_thread_state != TX_SUSPENDED) || (thread_3.tx_thread_state != TX_SUSPENDED) ||
271         (thread_4.tx_thread_state != TX_SUSPENDED) || (status != TX_SUCCESS))
272     {
273 
274         /* Thread Suspend error.  */
275         printf("ERROR #9\n");
276         test_control_return(1);
277     }
278 
279     /* Make sure that each thread has run once.  */
280     if ((thread_0_counter != 1) || (thread_1_counter != 1) ||
281         (thread_2_counter != 1) || (thread_3_counter != 1) ||
282         (thread_4_counter != 1))
283     {
284 
285         /* Thread Suspend error.  */
286         printf("ERROR #10\n");
287         test_control_return(1);
288     }
289 
290     status =  tx_thread_resume(&thread_1);
291 
292     /* Determine if all the other threads are in the proper state.  */
293     if ((thread_0.tx_thread_state != TX_READY)     || (thread_1.tx_thread_state != TX_READY) ||
294         (thread_2.tx_thread_state != TX_SUSPENDED) || (thread_3.tx_thread_state != TX_SUSPENDED) ||
295         (thread_4.tx_thread_state != TX_SUSPENDED) || (status != TX_SUCCESS))
296     {
297 
298         /* Thread Suspend error.  */
299         printf("ERROR #11\n");
300         test_control_return(1);
301     }
302 
303     /* Make sure that each thread has run once.  */
304     if ((thread_0_counter != 1) || (thread_1_counter != 1) ||
305         (thread_2_counter != 1) || (thread_3_counter != 1) ||
306         (thread_4_counter != 1))
307     {
308 
309         /* Thread Suspend error.  */
310         printf("ERROR #12\n");
311         test_control_return(1);
312     }
313 
314     status =  tx_thread_resume(&thread_2);
315 
316     /* Determine if all the other threads are in the proper state.  */
317     if ((thread_0.tx_thread_state != TX_READY)     || (thread_1.tx_thread_state != TX_READY) ||
318         (thread_2.tx_thread_state != TX_READY)     || (thread_3.tx_thread_state != TX_SUSPENDED) ||
319         (thread_4.tx_thread_state != TX_SUSPENDED) || (status != TX_SUCCESS))
320     {
321 
322         /* Thread Suspend error.  */
323         printf("ERROR #13\n");
324         test_control_return(1);
325     }
326 
327     /* Make sure that each thread has run once.  */
328     if ((thread_0_counter != 1) || (thread_1_counter != 1) ||
329         (thread_2_counter != 1) || (thread_3_counter != 1) ||
330         (thread_4_counter != 1))
331     {
332 
333         /* Thread Suspend error.  */
334         printf("ERROR #14\n");
335         test_control_return(1);
336     }
337 
338     status =  tx_thread_resume(&thread_3);
339 
340     /* Determine if all the other threads are in the proper state.  */
341     if ((thread_0.tx_thread_state != TX_READY)     || (thread_1.tx_thread_state != TX_READY) ||
342         (thread_2.tx_thread_state != TX_READY)     || (thread_3.tx_thread_state != TX_READY) ||
343         (thread_4.tx_thread_state != TX_SUSPENDED) || (status != TX_SUCCESS))
344     {
345 
346         /* Thread Suspend error.  */
347         printf("ERROR #15\n");
348         test_control_return(1);
349     }
350 
351     /* Make sure that each thread has run once.  */
352     if ((thread_0_counter != 1) || (thread_1_counter != 1) ||
353         (thread_2_counter != 1) || (thread_3_counter != 1) ||
354         (thread_4_counter != 1))
355     {
356 
357         /* Thread Suspend error.  */
358         printf("ERROR #16\n");
359         test_control_return(1);
360     }
361 
362     status =  tx_thread_resume(&thread_4);
363 
364     /* Determine if all the other threads are in the proper state.  */
365     if ((thread_0.tx_thread_state != TX_READY)     || (thread_1.tx_thread_state != TX_READY) ||
366         (thread_2.tx_thread_state != TX_READY)     || (thread_3.tx_thread_state != TX_READY) ||
367         (thread_4.tx_thread_state != TX_READY)     || (status != TX_SUCCESS))
368     {
369 
370         /* Thread Suspend error.  */
371         printf("ERROR #17\n");
372         test_control_return(1);
373     }
374 
375     /* Make sure that each thread has run once.  */
376     if ((thread_0_counter != 1) || (thread_1_counter != 1) ||
377         (thread_2_counter != 1) || (thread_3_counter != 1) ||
378         (thread_4_counter != 1))
379     {
380 
381         /* Thread Suspend error.  */
382         printf("ERROR #18\n");
383         test_control_return(1);
384     }
385 
386     /* Relinquish to allow other threads to run.  */
387     tx_thread_relinquish();
388 
389     /* Determine if all the other threads are in a suspended state.  */
390     if ((thread_0.tx_thread_state != TX_SUSPENDED) || (thread_1.tx_thread_state != TX_SUSPENDED) ||
391         (thread_2.tx_thread_state != TX_SUSPENDED) || (thread_3.tx_thread_state != TX_SUSPENDED) ||
392         (thread_4.tx_thread_state != TX_SUSPENDED))
393     {
394 
395         /* Thread Suspend error.  */
396         printf("ERROR #19\n");
397         test_control_return(1);
398     }
399 
400     /* Make sure that each thread has run twice.  */
401     if ((thread_0_counter != 2) || (thread_1_counter != 2) ||
402         (thread_2_counter != 2) || (thread_3_counter != 2) ||
403         (thread_4_counter != 2))
404     {
405 
406         /* Thread Suspend error.  */
407         printf("ERROR #20\n");
408         test_control_return(1);
409     }
410 
411     /* Suspend a thread that is already suspended.  */
412     status = tx_thread_suspend(&thread_4);
413 
414     /* Check for error condition.  */
415     if (status != TX_SUCCESS)
416     {
417         /* Thread Suspend error.  */
418         printf("ERROR #21\n");
419         test_control_return(1);
420     }
421     else
422     {
423 
424         /* Increment thread 5 counter.  */
425         thread_5_counter++;
426 
427         /* Successful Thread Suspend test.  */
428         printf("SUCCESS!\n");
429         test_control_return(0);
430     }
431 }
432 
433