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 
45 /* Define the counters used in the demo application...  */
46 
47 unsigned long   tm_synchronization_processing_counter;
48 
49 
50 /* Define the test thread prototypes.  */
51 
52 void            tm_synchronization_processing_thread_0_entry(void);
53 
54 
55 /* Define the reporting thread prototype.  */
56 
57 void            tm_synchronization_processing_thread_report(void);
58 
59 
60 /* Define the initialization prototype.  */
61 
62 void            tm_synchronization_processing_initialize(void);
63 
64 
65 /* Define main entry point.  */
66 
tm_main()67 void tm_main()
68 {
69 
70     /* Initialize the test.  */
71     tm_initialize(tm_synchronization_processing_initialize);
72 }
73 
74 
75 /* Define the synchronization processing test initialization.  */
76 
tm_synchronization_processing_initialize(void)77 void  tm_synchronization_processing_initialize(void)
78 {
79 
80     /* Create thread 0 at priority 10.  */
81     tm_thread_create(0, 10, tm_synchronization_processing_thread_0_entry);
82 
83     /* Resume thread 0.  */
84     tm_thread_resume(0);
85 
86     /* Create a semaphore for the test.  */
87     tm_semaphore_create(0);
88 
89     /* Create the reporting thread. It will preempt the other
90        threads and print out the test results.  */
91     tm_thread_create(5, 2, tm_synchronization_processing_thread_report);
92     tm_thread_resume(5);
93 }
94 
95 
96 /* Define the synchronization processing thread.  */
tm_synchronization_processing_thread_0_entry(void)97 void  tm_synchronization_processing_thread_0_entry(void)
98 {
99 
100 int status;
101 
102     while(1)
103     {
104 
105         /* Get the semaphore.  */
106         tm_semaphore_get(0);
107 
108         /* Release the semaphore.  */
109         status = tm_semaphore_put(0);
110 
111         /* Check for semaphore put error.  */
112         if (status != TM_SUCCESS)
113             break;
114 
115         /* Increment the number of semaphore get/puts.  */
116         tm_synchronization_processing_counter++;
117     }
118 }
119 
120 
121 /* Define the synchronization test reporting thread.  */
tm_synchronization_processing_thread_report(void)122 void  tm_synchronization_processing_thread_report(void)
123 {
124 
125 unsigned long   last_counter;
126 unsigned long   relative_time;
127 
128 
129     /* Initialize the last counter.  */
130     last_counter =  0;
131 
132     /* Initialize the relative time.  */
133     relative_time =  0;
134 
135     while(1)
136     {
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 Synchronization Processing Test **** Relative Time: %lu\n", relative_time);
146 
147         /* See if there are any errors.  */
148         if (tm_synchronization_processing_counter == last_counter)
149         {
150 
151             printf("ERROR: Invalid counter value(s). Error getting/putting semaphore!\n");
152         }
153 
154         /* Show the time period total.  */
155         printf("Time Period Total:  %lu\n\n", tm_synchronization_processing_counter - last_counter);
156 
157         /* Save the last counter.  */
158         last_counter =  tm_synchronization_processing_counter;
159     }
160 }
161