1 /* This test is designed to test semaphore prioritize. */
2
3 #include <stdio.h>
4 #include "tx_api.h"
5
6
7 /* Define the ISR dispatch. */
8
9 extern VOID (*test_isr_dispatch)(void);
10
11
12 /* Define the external reference for the preempt disable flag. */
13
14 extern volatile UINT _tx_thread_preempt_disable;
15
16
17 static unsigned long thread_0_counter = 0;
18 static TX_THREAD thread_0;
19
20 static unsigned long thread_1_counter = 0;
21 static TX_THREAD thread_1;
22
23 static unsigned long thread_2_counter = 0;
24 static TX_THREAD thread_2;
25
26 static unsigned long thread_3_counter = 0;
27 static TX_THREAD thread_3;
28
29 static unsigned long thread_4_counter = 0;
30 static TX_THREAD thread_4;
31
32 static unsigned long thread_5_counter = 0;
33 static TX_THREAD thread_5;
34
35 static unsigned long thread_6_counter = 0;
36 static TX_THREAD thread_6;
37
38 static TX_SEMAPHORE semaphore_0;
39 static TX_SEMAPHORE semaphore_1;
40
41 static int test_status;
42
43
44 /* Define thread prototypes. */
45
46 static void thread_0_entry(ULONG thread_input);
47 static void thread_1_entry(ULONG thread_input);
48 static void thread_2_entry(ULONG thread_input);
49 static void thread_3_entry(ULONG thread_input);
50 static void thread_4_entry(ULONG thread_input);
51 static void thread_5_entry(ULONG thread_input);
52 static void thread_6_entry(ULONG thread_input);
53
54
55 /* Prototype for test control return. */
56
57 void test_control_return(UINT status);
58
59
60 /* Define the ISR dispatch routine. */
61
test_isr(void)62 static void test_isr(void)
63 {
64
65 /* Determine if the test case we are looking for is present. */
66 if ((_tx_thread_preempt_disable) && (test_status == 1))
67 {
68
69 /* Determine if thread 3 is at the front of the suspension list. */
70 if (semaphore_0.tx_semaphore_suspension_list == &thread_3)
71 {
72
73 /* Abort the wait of thread 3. */
74 tx_thread_wait_abort(&thread_3);
75 }
76 else
77 {
78
79 /* Abort the wait of thread 5. */
80 tx_thread_wait_abort(&thread_5);
81
82 /* End the ISR processing. */
83 test_status = 2;
84 test_isr_dispatch = TX_NULL;
85 }
86 }
87 }
88
89
put_notify(TX_SEMAPHORE * semaphore_ptr)90 static void put_notify(TX_SEMAPHORE *semaphore_ptr)
91 {
92
93 /* Don't need to do anything in here... */
94 }
95
96
97 /* Define what the initial system looks like. */
98
99 #ifdef CTEST
test_application_define(void * first_unused_memory)100 void test_application_define(void *first_unused_memory)
101 #else
102 void threadx_semaphore_prioritize_application_define(void *first_unused_memory)
103 #endif
104 {
105
106 UINT status;
107 CHAR *pointer;
108
109
110 /* Put first available memory address into a character pointer. */
111 pointer = (CHAR *) first_unused_memory;
112
113 /* Put system definition stuff in here, e.g. thread creates and other assorted
114 create information. */
115
116 status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
117 pointer, TEST_STACK_SIZE_PRINTF,
118 17, 17, 100, TX_AUTO_START);
119 pointer = pointer + TEST_STACK_SIZE_PRINTF;
120
121 /* Check for status. */
122 if (status != TX_SUCCESS)
123 {
124
125 printf("Running Semaphore Prioritize Test................................... ERROR #1\n");
126 test_control_return(1);
127 }
128
129 status = tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
130 pointer, TEST_STACK_SIZE_PRINTF,
131 16, 16, 100, TX_DONT_START);
132 pointer = pointer + TEST_STACK_SIZE_PRINTF;
133
134 /* Check for status. */
135 if (status != TX_SUCCESS)
136 {
137
138 printf("Running Semaphore Prioritize Test................................... ERROR #2\n");
139 test_control_return(1);
140 }
141
142 status = tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
143 pointer, TEST_STACK_SIZE_PRINTF,
144 15, 15, 100, TX_DONT_START);
145 pointer = pointer + TEST_STACK_SIZE_PRINTF;
146
147 /* Check for status. */
148 if (status != TX_SUCCESS)
149 {
150
151 printf("Running Semaphore Prioritize Test................................... ERROR #3\n");
152 test_control_return(1);
153 }
154
155 status = tx_thread_create(&thread_3, "thread 3", thread_3_entry, 3,
156 pointer, TEST_STACK_SIZE_PRINTF,
157 3, 3, 100, TX_DONT_START);
158 pointer = pointer + TEST_STACK_SIZE_PRINTF;
159
160 /* Check for status. */
161 if (status != TX_SUCCESS)
162 {
163
164 printf("Running Semaphore Prioritize Test................................... ERROR #4\n");
165 test_control_return(1);
166 }
167
168 status = tx_thread_create(&thread_4, "thread 4", thread_4_entry, 4,
169 pointer, TEST_STACK_SIZE_PRINTF,
170 4, 4, 100, TX_DONT_START);
171 pointer = pointer + TEST_STACK_SIZE_PRINTF;
172
173 /* Check for status. */
174 if (status != TX_SUCCESS)
175 {
176
177 printf("Running Semaphore Prioritize Test................................... ERROR #5\n");
178 test_control_return(1);
179 }
180
181 status = tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,
182 pointer, TEST_STACK_SIZE_PRINTF,
183 5, 5, 100, TX_DONT_START);
184 pointer = pointer + TEST_STACK_SIZE_PRINTF;
185
186 /* Check for status. */
187 if (status != TX_SUCCESS)
188 {
189
190 printf("Running Semaphore Prioritize Test................................... ERROR #6\n");
191 test_control_return(1);
192 }
193
194 status = tx_thread_create(&thread_6, "thread 6", thread_6_entry, 6,
195 pointer, TEST_STACK_SIZE_PRINTF,
196 6, 6, 100, TX_DONT_START);
197 pointer = pointer + TEST_STACK_SIZE_PRINTF;
198
199 /* Check for status. */
200 if (status != TX_SUCCESS)
201 {
202
203 printf("Running Semaphore Prioritize Test................................... ERROR #7\n");
204 test_control_return(1);
205 }
206
207 /* Create the semaphore with no instances. */
208 status = tx_semaphore_create(&semaphore_0, "semaphore 0", 0);
209
210 /* Check for status. */
211 if (status != TX_SUCCESS)
212 {
213
214 printf("Running Semaphore Prioritize Test................................... ERROR #8\n");
215 test_control_return(1);
216 }
217
218 /* Setup the semaphore notify callback. */
219 status = tx_semaphore_put_notify(&semaphore_0, put_notify);
220
221 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
222
223 /* Check for status. */
224 if (status != TX_SUCCESS)
225 {
226
227 printf("Running Semaphore Prioritize Test................................... ERROR #9\n");
228 test_control_return(1);
229 }
230
231 #else
232
233 /* Check for status. */
234 if (status != TX_FEATURE_NOT_ENABLED)
235 {
236
237 printf("Running Semaphore Prioritize Test................................... ERROR #10\n");
238 test_control_return(1);
239 }
240
241 #endif
242
243 }
244
245
246
247 /* Define the test threads. */
248
thread_0_entry(ULONG thread_input)249 static void thread_0_entry(ULONG thread_input)
250 {
251
252 UINT status;
253
254
255 /* Inform user. */
256 printf("Running Semaphore Prioritize Test................................... ");
257
258 #ifndef TX_DISABLE_ERROR_CHECKING
259
260 /* Attempt to prioritize a non-semaphore. */
261 status = tx_semaphore_prioritize(TX_NULL);
262
263 /* Check status. */
264 if (status != TX_SEMAPHORE_ERROR)
265 {
266
267 /* Semaphore error. */
268 printf("ERROR #11\n");
269 test_control_return(1);
270 }
271
272 /* Attempt to prioritize a non-created semaphore. */
273 semaphore_1.tx_semaphore_id = 0;
274 status = tx_semaphore_prioritize(&semaphore_1);
275
276 /* Check status. */
277 if (status != TX_SEMAPHORE_ERROR)
278 {
279
280 /* Semaphore error. */
281 printf("ERROR #12\n");
282 test_control_return(1);
283 }
284 #endif
285
286 /* Prioritize the semaphore suspension list - empty list case! */
287 status = tx_semaphore_prioritize(&semaphore_0);
288
289 /* Check status and make sure thread 1 is terminated. */
290 if ((status != TX_SUCCESS) || (semaphore_0.tx_semaphore_suspension_list != TX_NULL))
291 {
292
293 /* Semaphore error. */
294 printf("ERROR #13\n");
295 test_control_return(1);
296 }
297
298 tx_thread_resume(&thread_1);
299 tx_thread_resume(&thread_2);
300
301 /* Increment the thread counter. */
302 thread_0_counter++;
303
304 /* Make sure thread 1 and 2 are suspended on the semaphore. */
305 if ((thread_1.tx_thread_state != TX_SEMAPHORE_SUSP) || (thread_2.tx_thread_state != TX_SEMAPHORE_SUSP) ||
306 (semaphore_0.tx_semaphore_suspension_list != &thread_1))
307 {
308
309 /* Semaphore error. */
310 printf("ERROR #14\n");
311 test_control_return(1);
312 }
313
314 /* Prioritize the semaphore suspension list. */
315 status = tx_semaphore_prioritize(&semaphore_0);
316
317 /* Check status and make sure thread 2 is not at the front of the suspension list. */
318 if ((status != TX_SUCCESS) || (semaphore_0.tx_semaphore_suspension_list != &thread_2))
319 {
320
321 /* Semaphore error. */
322 printf("ERROR #15\n");
323 test_control_return(1);
324 }
325
326 /* Prioritize the semaphore suspension list again - in this case, the list is already prioritized. */
327 status = tx_semaphore_prioritize(&semaphore_0);
328
329 /* Check status and make sure thread 2 is not at the front of the suspension list. */
330 if ((status != TX_SUCCESS) || (semaphore_0.tx_semaphore_suspension_list != &thread_2))
331 {
332
333 /* Semaphore error. */
334 printf("ERROR #15a\n");
335 test_control_return(1);
336 }
337
338 /* At this point we are going to get more than 2 threads suspended. */
339 tx_thread_resume(&thread_1);
340 tx_thread_resume(&thread_2);
341 tx_thread_resume(&thread_3);
342 tx_thread_resume(&thread_4);
343 tx_thread_resume(&thread_5);
344 tx_thread_resume(&thread_6);
345
346 /* Prioritize the semaphore suspension list. */
347 status = tx_semaphore_prioritize(&semaphore_0);
348
349 /* Check status and make sure thread 3 is now at the front of the suspension list. */
350 if ((status != TX_SUCCESS) || (semaphore_0.tx_semaphore_suspension_list != &thread_3))
351 {
352
353 /* Semaphore error. */
354 printf("ERROR #16\n");
355 test_control_return(1);
356 }
357
358 /* Now loop to test the interrupt of the prioritize loop logic. */
359 test_status = 1;
360 test_isr_dispatch = test_isr;
361 do
362 {
363
364 /* Prioritize the semaphore suspension list. */
365 status = tx_semaphore_prioritize(&semaphore_0);
366
367 /* Check status and make sure thread 1 is terminated. */
368 if (status != TX_SUCCESS)
369 {
370
371 /* Semaphore error. */
372 printf("ERROR #17\n");
373 test_control_return(1);
374 }
375
376 } while (test_status == 1);
377
378 /* Check status and make sure thread 3 is now at the front of the suspension list. */
379 if ((status != TX_SUCCESS) || (semaphore_0.tx_semaphore_suspension_list != &thread_4))
380 {
381
382 /* Semaphore error. */
383 printf("ERROR #18\n");
384 test_control_return(1);
385 }
386 else
387 {
388
389 /* Successful test. */
390 printf("SUCCESS!\n");
391 test_control_return(0);
392 }
393 }
394
395
thread_1_entry(ULONG thread_input)396 static void thread_1_entry(ULONG thread_input)
397 {
398 UINT status;
399
400 /* Loop forever! */
401 while(1)
402 {
403
404
405 /* Get semaphore. */
406 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
407
408 if (status != TX_SUCCESS)
409 break;
410
411 /* Increment the thread counter. */
412 thread_1_counter++;
413 }
414 }
415
416
thread_2_entry(ULONG thread_input)417 static void thread_2_entry(ULONG thread_input)
418 {
419 UINT status;
420
421
422 /* Loop forever! */
423 while(1)
424 {
425
426
427 /* Get semaphore. */
428 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
429
430 if (status != TX_SUCCESS)
431 break;
432
433 /* Increment the thread counter. */
434 thread_2_counter++;
435 }
436 }
437
438
thread_3_entry(ULONG thread_input)439 static void thread_3_entry(ULONG thread_input)
440 {
441 UINT status;
442
443
444 /* Loop forever! */
445 while(1)
446 {
447
448
449 /* Get semaphore. */
450 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
451
452 if (status != TX_SUCCESS)
453 break;
454
455 /* Increment the thread counter. */
456 thread_3_counter++;
457 }
458 }
459
460
thread_4_entry(ULONG thread_input)461 static void thread_4_entry(ULONG thread_input)
462 {
463 UINT status;
464
465
466 /* Loop forever! */
467 while(1)
468 {
469
470
471 /* Get semaphore. */
472 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
473
474 if (status != TX_SUCCESS)
475 break;
476
477 /* Increment the thread counter. */
478 thread_4_counter++;
479 }
480 }
481
482
thread_5_entry(ULONG thread_input)483 static void thread_5_entry(ULONG thread_input)
484 {
485 UINT status;
486
487
488 /* Loop forever! */
489 while(1)
490 {
491
492
493 /* Get semaphore. */
494 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
495
496 if (status != TX_SUCCESS)
497 break;
498
499 /* Increment the thread counter. */
500 thread_5_counter++;
501 }
502 }
503
504
thread_6_entry(ULONG thread_input)505 static void thread_6_entry(ULONG thread_input)
506 {
507 UINT status;
508
509
510 /* Loop forever! */
511 while(1)
512 {
513
514
515 /* Get semaphore. */
516 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
517
518 if (status != TX_SUCCESS)
519 break;
520
521 /* Increment the thread counter. */
522 thread_6_counter++;
523 }
524 }
525
526