1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** Thread-Metric Component                                               */
16 /**                                                                       */
17 /**   Synchronization Processing Test                                     */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /**************************************************************************/
23 /*                                                                        */
24 /*  FUNCTION                                               RELEASE        */
25 /*                                                                        */
26 /*    tm_synchronization_processing_test                  PORTABLE C      */
27 /*                                                           6.1.7        */
28 /*  AUTHOR                                                                */
29 /*                                                                        */
30 /*    William E. Lamie, Microsoft Corporation                             */
31 /*                                                                        */
32 /*  DESCRIPTION                                                           */
33 /*                                                                        */
34 /*    This file defines the Semaphore get/put processing test.            */
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_synchronization_processing_counter;
49 
50 
51 /* Define the test thread prototypes.  */
52 
53 void            tm_synchronization_processing_thread_0_entry(void);
54 
55 
56 /* Define the reporting thread prototype.  */
57 
58 void            tm_synchronization_processing_thread_report(void);
59 
60 
61 /* Define the initialization prototype.  */
62 
63 void            tm_synchronization_processing_initialize(void);
64 
65 
66 /* Define main entry point.  */
67 
tm_main()68 void tm_main()
69 {
70 
71     /* Initialize the test.  */
72     tm_initialize(tm_synchronization_processing_initialize);
73 }
74 
75 
76 /* Define the synchronization processing test initialization.  */
77 
tm_synchronization_processing_initialize(void)78 void  tm_synchronization_processing_initialize(void)
79 {
80 
81     /* Create thread 0 at priority 10.  */
82     tm_thread_create(0, 10, tm_synchronization_processing_thread_0_entry);
83 
84     /* Resume thread 0.  */
85     tm_thread_resume(0);
86 
87     /* Create a semaphore for the test.  */
88     tm_semaphore_create(0);
89 
90     /* Create the reporting thread. It will preempt the other
91        threads and print out the test results.  */
92     tm_thread_create(5, 2, tm_synchronization_processing_thread_report);
93     tm_thread_resume(5);
94 }
95 
96 
97 /* Define the synchronization processing thread.  */
tm_synchronization_processing_thread_0_entry(void)98 void  tm_synchronization_processing_thread_0_entry(void)
99 {
100 
101 int status;
102 
103     while(1)
104     {
105 
106         /* Get the semaphore.  */
107         tm_semaphore_get(0);
108 
109         /* Release the semaphore.  */
110         status = tm_semaphore_put(0);
111 
112         /* Check for semaphore put error.  */
113         if (status != TM_SUCCESS)
114             break;
115 
116         /* Increment the number of semaphore get/puts.  */
117         tm_synchronization_processing_counter++;
118     }
119 }
120 
121 
122 /* Define the synchronization test reporting thread.  */
tm_synchronization_processing_thread_report(void)123 void  tm_synchronization_processing_thread_report(void)
124 {
125 
126 unsigned long   last_counter;
127 unsigned long   relative_time;
128 
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 
139         /* Sleep to allow the test to run.  */
140         tm_thread_sleep(TM_TEST_DURATION);
141 
142         /* Increment the relative time.  */
143         relative_time =  relative_time + TM_TEST_DURATION;
144 
145         /* Print results to the stdio window.  */
146         printf("**** Thread-Metric Synchronization Processing Test **** Relative Time: %lu\n", relative_time);
147 
148         /* See if there are any errors.  */
149         if (tm_synchronization_processing_counter == last_counter)
150         {
151 
152             printf("ERROR: Invalid counter value(s). Error getting/putting semaphore!\n");
153         }
154 
155         /* Show the time period total.  */
156         printf("Time Period Total:  %lu\n\n", tm_synchronization_processing_counter - last_counter);
157 
158         /* Save the last counter.  */
159         last_counter =  tm_synchronization_processing_counter;
160     }
161 }
162