1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /** Thread-Metric Component                                               */
15 /**                                                                       */
16 /**   Message Processing Test                                             */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 
22 /**************************************************************************/
23 /*                                                                        */
24 /*  FUCTION                                                RELEASE        */
25 /*                                                                        */
26 /*    tm_message_processing_test                          PORTABLE C      */
27 /*                                                           6.1.7        */
28 /*  AUTHOR                                                                */
29 /*                                                                        */
30 /*    William E. Lamie, Microsoft Corporation                             */
31 /*                                                                        */
32 /*  DESCRIPTION                                                           */
33 /*                                                                        */
34 /*    Basic test for message exchange processing.                         */
35 /*                                                                        */
36 /*  RELEASE HISTORY                                                       */
37 /*                                                                        */
38 /*    DATE              NAME                      DESCRIPTION             */
39 /*                                                                        */
40 /*  10-15-2021     William E. Lamie         Initial Version 6.1.7         */
41 /*                                                                        */
42 /**************************************************************************/
43 #include "tm_api.h"
44 
45 
46 /* Define the counters used in the demo application...  */
47 
48 unsigned long   tm_message_processing_counter;
49 unsigned long   tm_message_sent[4];
50 unsigned long   tm_message_received[4];
51 
52 
53 /* Define the test thread prototypes.  */
54 
55 void            tm_message_processing_thread_0_entry(void);
56 
57 
58 /* Define the reporting thread prototype.  */
59 
60 void            tm_message_processing_thread_report(void);
61 
62 
63 /* Define the initialization prototype.  */
64 
65 void            tm_message_processing_initialize(void);
66 
67 
68 /* Define main entry point.  */
69 
tm_main()70 void tm_main()
71 {
72 
73     /* Initialize the test.  */
74     tm_initialize(tm_message_processing_initialize);
75 }
76 
77 
78 /* Define the message processing test initialization.  */
79 
tm_message_processing_initialize(void)80 void  tm_message_processing_initialize(void)
81 {
82 
83     /* Create thread 0 at priority 10.  */
84     tm_thread_create(0, 10, tm_message_processing_thread_0_entry);
85 
86     /* Resume thread 0.  */
87     tm_thread_resume(0);
88 
89     /* Create a queue for the message passing.  */
90     tm_queue_create(0);
91 
92     /* Create the reporting thread. It will preempt the other
93        threads and print out the test results.  */
94     tm_thread_create(5, 2, tm_message_processing_thread_report);
95     tm_thread_resume(5);
96 }
97 
98 
99 /* Define the message processing thread.  */
tm_message_processing_thread_0_entry(void)100 void  tm_message_processing_thread_0_entry(void)
101 {
102 
103     /* Initialize the source message.   */
104     tm_message_sent[0] =  0x11112222;
105     tm_message_sent[1] =  0x33334444;
106     tm_message_sent[2] =  0x55556666;
107     tm_message_sent[3] =  0x77778888;
108 
109     while(1)
110     {
111         /* Send a message to the queue.  */
112         tm_queue_send(0, tm_message_sent);
113 
114         /* Receive a message from the queue.  */
115         tm_queue_receive(0, tm_message_received);
116 
117         /* Check for invalid message.  */
118         if (tm_message_received[3] != tm_message_sent[3])
119             break;
120 
121         /* Increment the last word of the 16-byte message.  */
122         tm_message_sent[3]++;
123 
124         /* Increment the number of messages sent and received.  */
125         tm_message_processing_counter++;
126     }
127 }
128 
129 
130 /* Define the message test reporting thread.  */
tm_message_processing_thread_report(void)131 void  tm_message_processing_thread_report(void)
132 {
133 
134 unsigned long   last_counter;
135 unsigned long   relative_time;
136 
137 
138     /* Initialize the last counter.  */
139     last_counter =  0;
140 
141     /* Initialize the relative time.  */
142     relative_time =  0;
143 
144     while(1)
145     {
146 
147         /* Sleep to allow the test to run.  */
148         tm_thread_sleep(TM_TEST_DURATION);
149 
150         /* Increment the relative time.  */
151         relative_time =  relative_time + TM_TEST_DURATION;
152 
153         /* Print results to the stdio window.  */
154         printf("**** Thread-Metric Message Processing Test **** Relative Time: %lu\n", relative_time);
155 
156         /* See if there are any errors.  */
157         if (tm_message_processing_counter == last_counter)
158         {
159 
160             printf("ERROR: Invalid counter value(s). Error sending/receiving messages!\n");
161         }
162 
163         /* Show the time period total.  */
164         printf("Time Period Total:  %lu\n\n", tm_message_processing_counter - last_counter);
165 
166         /* Save the last counter.  */
167         last_counter =  tm_message_processing_counter;
168     }
169 }
170