1 /* This test is designed to test immediate response queue services including queue front send.
2    This test is for queue sizes of 2 ULONG.  */
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 
10 static unsigned long   thread_1_counter =  0;
11 static TX_THREAD       thread_1;
12 
13 static TX_THREAD       thread_2;
14 
15 static TX_QUEUE        queue_0;
16 
17 
18 static unsigned long   thread_1a_counter =  0;
19 static TX_THREAD       thread_1a;
20 
21 static TX_THREAD       thread_2a;
22 
23 static TX_QUEUE        queue_0a;
24 
25 /* Define thread prototypes.  */
26 
27 static void    thread_0_entry(ULONG thread_input);
28 static void    thread_1_entry(ULONG thread_input);
29 static void    thread_2_entry(ULONG thread_input);
30 static void    thread_1a_entry(ULONG thread_input);
31 static void    thread_2a_entry(ULONG thread_input);
32 
33 
34 /* Prototype for test control return.  */
35 
36 void  test_control_return(UINT status);
37 
38 
queue_notify(TX_QUEUE * queue_ptr)39 static void    queue_notify(TX_QUEUE *queue_ptr)
40 {
41 
42 }
43 
44 
45 /* Define what the initial system looks like.  */
46 
47 #ifdef CTEST
test_application_define(void * first_unused_memory)48 void test_application_define(void *first_unused_memory)
49 #else
50 void    threadx_queue_front_send_application_define(void *first_unused_memory)
51 #endif
52 {
53 
54 UINT    status;
55 CHAR    *pointer;
56 
57 
58     /* Put first available memory address into a character pointer.  */
59     pointer =  (CHAR *) first_unused_memory;
60 
61     /* Put system definition stuff in here, e.g. thread creates and other assorted
62        create information.  */
63     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
64             pointer, TEST_STACK_SIZE_PRINTF,
65             16, 16, 100, TX_AUTO_START);
66     pointer = pointer + TEST_STACK_SIZE_PRINTF;
67 
68     /* Check for status.  */
69     if (status != TX_SUCCESS)
70     {
71 
72         printf("Running Queue Front Test............................................ ERROR #1\n");
73         test_control_return(1);
74     }
75 
76     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
77             pointer, TEST_STACK_SIZE_PRINTF,
78             16, 16, 100, TX_DONT_START);
79     pointer = pointer + TEST_STACK_SIZE_PRINTF;
80     status =  tx_thread_create(&thread_1a, "thread 1a", thread_1a_entry, 1,
81             pointer, TEST_STACK_SIZE_PRINTF,
82             16, 16, 100, TX_DONT_START);
83     pointer = pointer + TEST_STACK_SIZE_PRINTF;
84 
85     /* Check for status.  */
86     if (status != TX_SUCCESS)
87     {
88 
89         printf("Running Queue Front Test............................................ ERROR #2\n");
90         test_control_return(1);
91     }
92 
93     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
94             pointer, TEST_STACK_SIZE_PRINTF,
95             15, 15, 100, TX_DONT_START);
96     pointer = pointer + TEST_STACK_SIZE_PRINTF;
97     status =  tx_thread_create(&thread_2a, "thread 2a", thread_2a_entry, 2,
98             pointer, TEST_STACK_SIZE_PRINTF,
99             15, 15, 100, TX_DONT_START);
100     pointer = pointer + TEST_STACK_SIZE_PRINTF;
101 
102     /* Check for status.  */
103     if (status != TX_SUCCESS)
104     {
105 
106         printf("Running Queue Front Test............................................ ERROR #3\n");
107         test_control_return(1);
108     }
109 
110     /* Create the queues.  */
111     status =  tx_queue_create(&queue_0, "queue 0", TX_2_ULONG, pointer, 2*2*sizeof(ULONG));
112     pointer = pointer + 2*2*sizeof(ULONG);
113     status =  tx_queue_create(&queue_0a, "queue 0a", TX_1_ULONG, pointer, 2*1*sizeof(ULONG));
114     pointer = pointer + 2*1*sizeof(ULONG);
115 
116     /* Check for status.  */
117     if (status != TX_SUCCESS)
118     {
119 
120         printf("Running Queue Front Test............................................ ERROR #4\n");
121         test_control_return(1);
122     }
123 
124     /* Setup queue send notification.  */
125     status =  tx_queue_send_notify(&queue_0, queue_notify);
126 
127 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
128 
129     /* Check for status.  */
130     if (status != TX_SUCCESS)
131     {
132 
133         printf("Running Queue Front Test............................................ ERROR #5\n");
134         test_control_return(1);
135     }
136 #else
137 
138     /* Check for status.  */
139     if (status != TX_FEATURE_NOT_ENABLED)
140     {
141 
142         printf("Running Queue Front Test............................................ ERROR #6\n");
143         test_control_return(1);
144     }
145 
146 #endif
147 
148 }
149 
150 
151 
152 /* Define the test threads.  */
153 
thread_0_entry(ULONG thread_input)154 static void    thread_0_entry(ULONG thread_input)
155 {
156 
157 UINT    status;
158 ULONG   source_message[2] = {0x12345678, 0};
159 ULONG   dest_message[2];
160 ULONG   temp[2];
161 
162 
163     /* Inform user.  */
164     printf("Running Queue Front Test............................................ ");
165 
166     /* Perform the 1 word queue front send test.  */
167 
168     /* Increment thread 0 counter.  */
169     thread_0_counter++;
170 
171     /* Place something on queue 0a.  */
172     status =  tx_queue_send(&queue_0a, &source_message[0], TX_NO_WAIT);
173 
174     if (status != TX_SUCCESS)
175     {
176 
177         /* Queue error.  */
178         printf("ERROR #7a\n");
179         test_control_return(1);
180     }
181 
182     /* Place a new message on the front of the queue.  */
183     temp[0] =  0xF000001;
184     status =  tx_queue_front_send(&queue_0a, &temp[0], TX_NO_WAIT);
185 
186     if (status != TX_SUCCESS)
187     {
188 
189         /* Queue error.  */
190         printf("ERROR #8a\n");
191         test_control_return(1);
192     }
193 
194     /* Attempt to receive something from queue 0a.  */
195     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
196 
197     /* Should be successful and dest_message should equal source. */
198     if ((status != TX_SUCCESS) || (dest_message[0] != temp[0]))
199     {
200 
201         /* Queue error.  */
202         printf("ERROR #9a\n");
203         test_control_return(1);
204     }
205 
206     /* Attempt to receive something from queue 0a.  */
207     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
208 
209     /* Should be successful and dest_message should equal source. */
210     if ((status != TX_SUCCESS) || (dest_message[0] != source_message[0]))
211     {
212 
213         /* Queue error.  */
214         printf("ERROR #10a\n");
215         test_control_return(1);
216     }
217 
218     /* Attempt to receive another message from the queue.  */
219     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
220 
221     /* Should be an error.  */
222     if (status != TX_QUEUE_EMPTY)
223     {
224 
225         /* Queue error.  */
226         printf("ERROR #11a\n");
227         test_control_return(1);
228     }
229 
230     /* At this point the queue is empty. Resume another thread to
231        suspend on an empty queue.  */
232     tx_thread_resume(&thread_1a);
233 
234     /* Relinquish to get this thread suspended on the empty queue.  */
235     tx_thread_relinquish();
236 
237     /* Resume thread 2a to get another thread suspended on the empty queue.  */
238     tx_thread_resume(&thread_2a);
239 
240     /* Now send something to the front of the queue, which will resume
241        the first waiting thread.  */
242     temp[0] =  0xFF00002;
243     status =  tx_queue_front_send(&queue_0a, &temp[0], TX_NO_WAIT);
244 
245     if (status != TX_SUCCESS)
246     {
247 
248         /* Queue error.  */
249         printf("ERROR #12a\n");
250         test_control_return(1);
251     }
252 
253     /* Now send something to the front of the queue, which will resume
254        the second waiting thread.  */
255     temp[0] =  0xFF00002;
256     status =  tx_queue_front_send(&queue_0a, &temp[0], TX_NO_WAIT);
257 
258     if (status != TX_SUCCESS)
259     {
260 
261         /* Queue error.  */
262         printf("ERROR #13a\n");
263         test_control_return(1);
264     }
265 
266     /* Now relinquish again to let the other thread process the message.  */
267     tx_thread_relinquish();
268 
269     /* At this point, the other thread should have placed 2 messages on the queue
270        so we will now send to the front, but without suspension.  */
271     temp[0] =  0xFF00003;
272     status =  tx_queue_front_send(&queue_0a, &temp[0], TX_NO_WAIT);
273 
274     if (status != TX_QUEUE_FULL)
275     {
276 
277         /* Queue error.  */
278         printf("ERROR #14a\n");
279         test_control_return(1);
280     }
281 
282     /* Now, we will now send to the front, but with suspension.  */
283     temp[0] =  0xFF00003;
284     status =  tx_queue_front_send(&queue_0a, &temp[0], TX_WAIT_FOREVER);
285 
286     if (status != TX_SUCCESS)
287     {
288 
289         /* Queue error.  */
290         printf("ERROR #15a\n");
291         test_control_return(1);
292     }
293 
294     /* Now resume thread 2a to get another thread suspended on the queue.  */
295     tx_thread_resume(&thread_2a);
296 
297     temp[0] =  0xFF00004;
298     status =  tx_queue_front_send(&queue_0a, &temp[0], TX_WAIT_FOREVER);
299 
300     /* When we get back, the other thread has received all the messages and
301        verified they are in order AND relinquished.  */
302     if ((status != TX_SUCCESS) || (thread_1a_counter != 1))
303     {
304 
305         /* Queue error.  */
306         printf("ERROR #16a\n");
307         test_control_return(1);
308     }
309 
310 
311     /* Perform the multiword queue front send test.  */
312 
313 
314     /* Increment thread 0 counter.  */
315     thread_0_counter =  1;
316 
317     /* Reset the source message.  */
318     source_message[0] =  0x12345678;
319     source_message[1] =  0;
320 
321     /* Place something on queue 0.  */
322     status =  tx_queue_send(&queue_0, &source_message[0], TX_NO_WAIT);
323 
324     if (status != TX_SUCCESS)
325     {
326 
327         /* Queue error.  */
328         printf("ERROR #7\n");
329         test_control_return(1);
330     }
331 
332     /* Place a new message on the front of the queue.  */
333     temp[0] =  0xF000001;
334     status =  tx_queue_front_send(&queue_0, &temp[0], TX_NO_WAIT);
335 
336     if (status != TX_SUCCESS)
337     {
338 
339         /* Queue error.  */
340         printf("ERROR #8\n");
341         test_control_return(1);
342     }
343 
344     /* Attempt to receive something from queue 0.  */
345     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
346 
347     /* Should be successful and dest_message should equal source. */
348     if ((status != TX_SUCCESS) || (dest_message[0] != temp[0]))
349     {
350 
351         /* Queue error.  */
352         printf("ERROR #9\n");
353         test_control_return(1);
354     }
355 
356     /* Attempt to receive something from queue 0.  */
357     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
358 
359     /* Should be successful and dest_message should equal source. */
360     if ((status != TX_SUCCESS) || (dest_message[0] != source_message[0]))
361     {
362 
363         /* Queue error.  */
364         printf("ERROR #10\n");
365         test_control_return(1);
366     }
367 
368     /* Attempt to receive another message from the queue.  */
369     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
370 
371     /* Should be an error.  */
372     if (status != TX_QUEUE_EMPTY)
373     {
374 
375         /* Queue error.  */
376         printf("ERROR #11\n");
377         test_control_return(1);
378     }
379 
380     /* At this point the queue is empty. Resume another thread to
381        suspend on an empty queue.  */
382     tx_thread_resume(&thread_1);
383 
384     /* Relinquish to get this thread suspended on the empty queue.  */
385     tx_thread_relinquish();
386 
387     /* Resume thread 2 to get another thread suspended on the empty queue.  */
388     tx_thread_resume(&thread_2);
389 
390     /* Now send something to the front of the queue, which will resume
391        the first waiting thread.  */
392     temp[0] =  0xFF00002;
393     status =  tx_queue_front_send(&queue_0, &temp[0], TX_NO_WAIT);
394 
395     if (status != TX_SUCCESS)
396     {
397 
398         /* Queue error.  */
399         printf("ERROR #12\n");
400         test_control_return(1);
401     }
402 
403     /* Now send something to the front of the queue, which will resume
404        the second waiting thread.  */
405     temp[0] =  0xFF00002;
406     status =  tx_queue_front_send(&queue_0, &temp[0], TX_NO_WAIT);
407 
408     if (status != TX_SUCCESS)
409     {
410 
411         /* Queue error.  */
412         printf("ERROR #13\n");
413         test_control_return(1);
414     }
415 
416     /* Now relinquish again to let the other thread process the message.  */
417     tx_thread_relinquish();
418 
419     /* At this point, the other thread should have placed 2 messages on the queue
420        so we will now send to the front, but without suspension.  */
421     temp[0] =  0xFF00003;
422     status =  tx_queue_front_send(&queue_0, &temp[0], TX_NO_WAIT);
423 
424     if (status != TX_QUEUE_FULL)
425     {
426 
427         /* Queue error.  */
428         printf("ERROR #14\n");
429         test_control_return(1);
430     }
431 
432     /* Now, we will now send to the front, but with suspension.  */
433     temp[0] =  0xFF00003;
434     status =  tx_queue_front_send(&queue_0, &temp[0], TX_WAIT_FOREVER);
435 
436     if (status != TX_SUCCESS)
437     {
438 
439         /* Queue error.  */
440         printf("ERROR #15\n");
441         test_control_return(1);
442     }
443 
444     /* Now resume thread 2 to get another thread suspended on the queue.  */
445     tx_thread_resume(&thread_2);
446 
447     temp[0] =  0xFF00004;
448     status =  tx_queue_front_send(&queue_0, &temp[0], TX_WAIT_FOREVER);
449 
450     /* When we get back, the other thread has received all the messages and
451        verified they are in order AND relinquished.  */
452     if ((status != TX_SUCCESS) || (thread_1_counter != 1))
453     {
454 
455         /* Queue error.  */
456         printf("ERROR #16\n");
457         test_control_return(1);
458     }
459     else
460     {
461 
462         /* Successful test.  */
463         printf("SUCCESS!\n");
464         test_control_return(0);
465     }
466 }
467 
468 
thread_1_entry(ULONG thread_input)469 static void    thread_1_entry(ULONG thread_input)
470 {
471 
472 UINT    status;
473 ULONG   source_message[2] = {0xEE000001, 0};
474 ULONG   dest_message[2];
475 
476 
477     /* First, suspend on an empty queue.  */
478     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_WAIT_FOREVER);
479 
480     /* Determine if the message is good.  */
481     if ((status != TX_SUCCESS) || (dest_message[0] != 0xFF00002))
482         return;
483 
484     /* Now fill the queue with two messages.   */
485     status =  tx_queue_send(&queue_0, &source_message[0], TX_WAIT_FOREVER);
486     source_message[0]++;
487     status +=  tx_queue_front_send(&queue_0, &source_message[0], TX_WAIT_FOREVER);
488 
489     if (status != TX_SUCCESS)
490         return;
491 
492     /* Now let thread 0 send to the front of the queue with suspension.  */
493     tx_thread_relinquish();
494 
495     /* Attempt to receive three messages from the queue.  */
496     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
497 
498     /* Should be an error.  */
499     if ((status != TX_SUCCESS) || (dest_message[0] != 0xFF00003))
500         return;
501 
502     /* Attempt to receive three messages from the queue.  */
503     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
504 
505     /* Should be an error.  */
506     if ((status != TX_SUCCESS) || (dest_message[0] != 0xEE000002))
507         return;
508 
509     /* Attempt to receive three messages from the queue.  */
510     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
511 
512     /* Should be an error.  */
513     if ((status != TX_SUCCESS) || (dest_message[0] != 0xEE000001))
514         return;
515 
516     /* At this point, we are going to fill up the queue again.  */
517     source_message[0]++;
518     status =  tx_queue_send(&queue_0, &source_message[0], TX_WAIT_FOREVER);
519     source_message[0]++;
520     status +=  tx_queue_front_send(&queue_0, &source_message[0], TX_WAIT_FOREVER);
521 
522     if (status != TX_SUCCESS)
523         return;
524 
525     /* Now let thread 0 send to the front of the queue with suspension.  */
526     tx_thread_relinquish();
527 
528     /* Attempt to receive four messages from the queue.  */
529     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
530 
531     /* Should be an error.  */
532     if ((status != TX_SUCCESS) || (dest_message[0] != 0xFF00004))
533         return;
534 
535     /* Attempt to receive four messages from the queue.  */
536     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
537 
538     /* Should be an error.  */
539     if ((status != TX_SUCCESS) || (dest_message[0] != 0xDD000001))
540         return;
541 
542     /* Attempt to receive four messages from the queue.  */
543     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
544 
545     /* Should be an error.  */
546     if ((status != TX_SUCCESS) || (dest_message[0] != 0xEE000004))
547         return;
548 
549     /* Attempt to receive three messages from the queue.  */
550     status =  tx_queue_receive(&queue_0, &dest_message[0], TX_NO_WAIT);
551 
552     /* Should be an error.  */
553     if ((status != TX_SUCCESS) || (dest_message[0] != 0xEE000003))
554         return;
555 
556     /* Increment this threads counter.  */
557     thread_1_counter++;
558 }
559 
560 
thread_2_entry(ULONG thread_input)561 static void    thread_2_entry(ULONG thread_input)
562 {
563 
564 ULONG   source_message[2] = {0xDD000001, 0};
565 ULONG   destination_message[2];
566 
567 
568     /* Receive message.  */
569     tx_queue_receive(&queue_0, &destination_message[0], TX_WAIT_FOREVER);
570 
571     /* Self suspend.  */
572     tx_thread_suspend(&thread_2);
573 
574     /* Send another message to the front of the queue.  */
575     tx_queue_front_send(&queue_0, &source_message[0], TX_WAIT_FOREVER);
576 }
577 
578 
thread_1a_entry(ULONG thread_input)579 static void    thread_1a_entry(ULONG thread_input)
580 {
581 
582 UINT    status;
583 ULONG   source_message[2] = {0xEE000001, 0};
584 ULONG   dest_message[2];
585 
586 
587     /* First, suspend on an empty queue.  */
588     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_WAIT_FOREVER);
589 
590     /* Determine if the message is good.  */
591     if ((status != TX_SUCCESS) || (dest_message[0] != 0xFF00002))
592         return;
593 
594     /* Now fill the queue with two messages.   */
595     status =  tx_queue_send(&queue_0a, &source_message[0], TX_WAIT_FOREVER);
596     source_message[0]++;
597     status +=  tx_queue_front_send(&queue_0a, &source_message[0], TX_WAIT_FOREVER);
598 
599     if (status != TX_SUCCESS)
600         return;
601 
602     /* Now let thread 0 send to the front of the queue with suspension.  */
603     tx_thread_relinquish();
604 
605     /* Attempt to receive three messages from the queue.  */
606     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
607 
608     /* Should be an error.  */
609     if ((status != TX_SUCCESS) || (dest_message[0] != 0xFF00003))
610         return;
611 
612     /* Attempt to receive three messages from the queue.  */
613     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
614 
615     /* Should be an error.  */
616     if ((status != TX_SUCCESS) || (dest_message[0] != 0xEE000002))
617         return;
618 
619     /* Attempt to receive three messages from the queue.  */
620     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
621 
622     /* Should be an error.  */
623     if ((status != TX_SUCCESS) || (dest_message[0] != 0xEE000001))
624         return;
625 
626     /* At this point, we are going to fill up the queue again.  */
627     source_message[0]++;
628     status =  tx_queue_send(&queue_0a, &source_message[0], TX_WAIT_FOREVER);
629     source_message[0]++;
630     status +=  tx_queue_front_send(&queue_0a, &source_message[0], TX_WAIT_FOREVER);
631 
632     if (status != TX_SUCCESS)
633         return;
634 
635     /* Now let thread 0 send to the front of the queue with suspension.  */
636     tx_thread_relinquish();
637 
638     /* Attempt to receive four messages from the queue.  */
639     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
640 
641     /* Should be an error.  */
642     if ((status != TX_SUCCESS) || (dest_message[0] != 0xFF00004))
643         return;
644 
645     /* Attempt to receive four messages from the queue.  */
646     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
647 
648     /* Should be an error.  */
649     if ((status != TX_SUCCESS) || (dest_message[0] != 0xDD000001))
650         return;
651 
652     /* Attempt to receive four messages from the queue.  */
653     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
654 
655     /* Should be an error.  */
656     if ((status != TX_SUCCESS) || (dest_message[0] != 0xEE000004))
657         return;
658 
659     /* Attempt to receive three messages from the queue.  */
660     status =  tx_queue_receive(&queue_0a, &dest_message[0], TX_NO_WAIT);
661 
662     /* Should be an error.  */
663     if ((status != TX_SUCCESS) || (dest_message[0] != 0xEE000003))
664         return;
665 
666     /* Increment this threads counter.  */
667     thread_1a_counter++;
668 }
669 
670 
thread_2a_entry(ULONG thread_input)671 static void    thread_2a_entry(ULONG thread_input)
672 {
673 
674 ULONG   source_message[2] = {0xDD000001, 0};
675 ULONG   destination_message[2];
676 
677 
678     /* Receive message.  */
679     tx_queue_receive(&queue_0a, &destination_message[0], TX_WAIT_FOREVER);
680 
681     /* Self suspend.  */
682     tx_thread_suspend(&thread_2a);
683 
684     /* Send another message to the front of the queue.  */
685     tx_queue_front_send(&queue_0a, &source_message[0], TX_WAIT_FOREVER);
686 }
687