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 /**                                                                       */
15 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Semaphore                                                           */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define TX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "tx_api.h"
28 #include "tx_semaphore.h"
29 #ifdef TX_SEMAPHORE_ENABLE_PERFORMANCE_INFO
30 #include "tx_trace.h"
31 #endif
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _tx_semaphore_performance_info_get                  PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function retrieves performance information from the specified  */
47 /*    semaphore.                                                          */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    semaphore_ptr                     Pointer to semaphore control block*/
52 /*    puts                              Destination for the number of     */
53 /*                                        puts on to this semaphore       */
54 /*    gets                              Destination for the number of     */
55 /*                                        gets on this semaphore          */
56 /*    suspensions                       Destination for the number of     */
57 /*                                        suspensions on this semaphore   */
58 /*    timeouts                          Destination for number of timeouts*/
59 /*                                        on this semaphore               */
60 /*                                                                        */
61 /*  OUTPUT                                                                */
62 /*                                                                        */
63 /*    status                            Completion status                 */
64 /*                                                                        */
65 /*  CALLS                                                                 */
66 /*                                                                        */
67 /*    None                                                                */
68 /*                                                                        */
69 /*  CALLED BY                                                             */
70 /*                                                                        */
71 /*    Application Code                                                    */
72 /*                                                                        */
73 /*  RELEASE HISTORY                                                       */
74 /*                                                                        */
75 /*    DATE              NAME                      DESCRIPTION             */
76 /*                                                                        */
77 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
78 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
79 /*                                            resulting in version 6.1    */
80 /*                                                                        */
81 /**************************************************************************/
_tx_semaphore_performance_info_get(TX_SEMAPHORE * semaphore_ptr,ULONG * puts,ULONG * gets,ULONG * suspensions,ULONG * timeouts)82 UINT  _tx_semaphore_performance_info_get(TX_SEMAPHORE *semaphore_ptr, ULONG *puts, ULONG *gets,
83                     ULONG *suspensions, ULONG *timeouts)
84 {
85 
86 #ifdef TX_SEMAPHORE_ENABLE_PERFORMANCE_INFO
87 
88 TX_INTERRUPT_SAVE_AREA
89 UINT                    status;
90 
91 
92     /* Determine if this is a legal request.  */
93     if (semaphore_ptr == TX_NULL)
94     {
95 
96         /* Semaphore pointer is illegal, return error.  */
97         status =  TX_PTR_ERROR;
98     }
99 
100     /* Determine if the semaphore ID is invalid.  */
101     else if (semaphore_ptr -> tx_semaphore_id != TX_SEMAPHORE_ID)
102     {
103 
104         /* Semaphore pointer is illegal, return error.  */
105         status =  TX_PTR_ERROR;
106     }
107     else
108     {
109 
110         /* Disable interrupts.  */
111         TX_DISABLE
112 
113         /* If trace is enabled, insert this event into the trace buffer.  */
114         TX_TRACE_IN_LINE_INSERT(TX_TRACE_SEMAPHORE_PERFORMANCE_INFO_GET, semaphore_ptr, 0, 0, 0, TX_TRACE_SEMAPHORE_EVENTS)
115 
116         /* Log this kernel call.  */
117         TX_EL_SEMAPHORE_PERFORMANCE_INFO_GET_INSERT
118 
119         /* Retrieve all the pertinent information and return it in the supplied
120            destinations.  */
121 
122         /* Retrieve the number of puts on this semaphore.  */
123         if (puts != TX_NULL)
124         {
125 
126             *puts =  semaphore_ptr -> tx_semaphore_performance_put_count;
127         }
128 
129         /* Retrieve the number of gets on this semaphore.  */
130         if (gets != TX_NULL)
131         {
132 
133             *gets =  semaphore_ptr -> tx_semaphore_performance_get_count;
134         }
135 
136         /* Retrieve the number of suspensions on this semaphore.  */
137         if (suspensions != TX_NULL)
138         {
139 
140             *suspensions =  semaphore_ptr -> tx_semaphore_performance_suspension_count;
141         }
142 
143         /* Retrieve the number of timeouts on this semaphore.  */
144         if (timeouts != TX_NULL)
145         {
146 
147             *timeouts =  semaphore_ptr -> tx_semaphore_performance_timeout_count;
148         }
149 
150         /* Restore interrupts.  */
151         TX_RESTORE
152 
153         /* Return successful completion.  */
154         status =  TX_SUCCESS;
155     }
156 #else
157 UINT                    status;
158 
159 
160     /* Access input arguments just for the sake of lint, MISRA, etc.  */
161     if (semaphore_ptr != TX_NULL)
162     {
163 
164         /* Not enabled, return error.  */
165         status =  TX_FEATURE_NOT_ENABLED;
166     }
167     else if (puts != TX_NULL)
168     {
169 
170         /* Not enabled, return error.  */
171         status =  TX_FEATURE_NOT_ENABLED;
172     }
173     else if (gets != TX_NULL)
174     {
175 
176         /* Not enabled, return error.  */
177         status =  TX_FEATURE_NOT_ENABLED;
178     }
179     else if (suspensions != TX_NULL)
180     {
181 
182         /* Not enabled, return error.  */
183         status =  TX_FEATURE_NOT_ENABLED;
184     }
185     else if (timeouts != TX_NULL)
186     {
187 
188         /* Not enabled, return error.  */
189         status =  TX_FEATURE_NOT_ENABLED;
190     }
191     else
192     {
193 
194         /* Not enabled, return error.  */
195         status =  TX_FEATURE_NOT_ENABLED;
196     }
197 #endif
198 
199     /* Return completion status.  */
200     return(status);
201 }
202 
203