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