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 /**   Synchronization Processing Test                                     */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 /**************************************************************************/
22 /*                                                                        */
23 /*  FUNCTION                                               RELEASE        */
24 /*                                                                        */
25 /*    tm_synchronization_processing_test                  PORTABLE C      */
26 /*                                                           6.1.7        */
27 /*  AUTHOR                                                                */
28 /*                                                                        */
29 /*    William E. Lamie, Microsoft Corporation                             */
30 /*                                                                        */
31 /*  DESCRIPTION                                                           */
32 /*                                                                        */
33 /*    This file defines the Semaphore get/put processing test.            */
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_synchronization_processing_counter;
47 
48 /* Define the test thread prototypes.  */
49 
50 void tm_synchronization_processing_thread_0_entry(void *p1, void *p2, void *p3);
51 
52 /* Define the reporting function prototype.  */
53 
54 void tm_synchronization_processing_thread_report(void);
55 
56 /* Define the initialization prototype.  */
57 
58 void tm_synchronization_processing_initialize(void);
59 
60 /* Define main entry point.  */
61 
main(void)62 int main(void)
63 {
64 
65 	/* Initialize the test.  */
66 	tm_initialize(tm_synchronization_processing_initialize);
67 
68 	return 0;
69 }
70 
71 /* Define the synchronization processing test initialization.  */
72 
tm_synchronization_processing_initialize(void)73 void tm_synchronization_processing_initialize(void)
74 {
75 
76 	/* Create thread 0 at priority 10.  */
77 	tm_thread_create(0, 10, tm_synchronization_processing_thread_0_entry);
78 
79 	/* Resume thread 0.  */
80 	tm_thread_resume(0);
81 
82 	/* Create a semaphore for the test.  */
83 	tm_semaphore_create(0);
84 
85 	tm_synchronization_processing_thread_report();
86 }
87 
88 /* Define the synchronization processing thread.  */
tm_synchronization_processing_thread_0_entry(void * p1,void * p2,void * p3)89 void tm_synchronization_processing_thread_0_entry(void *p1, void *p2, void *p3)
90 {
91 
92 	int status;
93 
94 	while (1) {
95 
96 		/* Get the semaphore.  */
97 		tm_semaphore_get(0);
98 
99 		/* Release the semaphore.  */
100 		status = tm_semaphore_put(0);
101 
102 		/* Check for semaphore put error.  */
103 		if (status != TM_SUCCESS) {
104 			break;
105 		}
106 
107 		/* Increment the number of semaphore get/puts.  */
108 		tm_synchronization_processing_counter++;
109 	}
110 }
111 
112 /* Define the synchronization test reporting function.  */
tm_synchronization_processing_thread_report(void)113 void tm_synchronization_processing_thread_report(void)
114 {
115 
116 	unsigned long last_counter;
117 	unsigned long relative_time;
118 
119 	/* Initialize the last counter.  */
120 	last_counter = 0;
121 
122 	/* Initialize the relative time.  */
123 	relative_time = 0;
124 
125 	while (1) {
126 
127 		/* Sleep to allow the test to run.  */
128 		tm_thread_sleep(TM_TEST_DURATION);
129 
130 		/* Increment the relative time.  */
131 		relative_time = relative_time + TM_TEST_DURATION;
132 
133 		/* Print results to the stdio window.  */
134 		printf("**** Thread-Metric Synchronization Processing Test **** Relative Time: "
135 		       "%lu\n",
136 		       relative_time);
137 
138 		/* See if there are any errors.  */
139 		if (tm_synchronization_processing_counter == last_counter) {
140 
141 			printf("ERROR: Invalid counter value(s). Error getting/putting "
142 			       "semaphore!\n");
143 		}
144 
145 		/* Show the time period total.  */
146 		printf("Time Period Total:  %lu\n\n",
147 		       tm_synchronization_processing_counter - last_counter);
148 
149 		/* Save the last counter.  */
150 		last_counter = tm_synchronization_processing_counter;
151 	}
152 }
153