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_trace.h"
29 #include "tx_thread.h"
30 #include "tx_semaphore.h"
31
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _tx_semaphore_ceiling_put PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* William E. Lamie, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function puts an instance into the specified counting */
46 /* semaphore up to the specified semaphore ceiling. */
47 /* */
48 /* INPUT */
49 /* */
50 /* semaphore_ptr Pointer to semaphore */
51 /* ceiling Maximum value of semaphore */
52 /* */
53 /* OUTPUT */
54 /* */
55 /* status Completion status */
56 /* */
57 /* CALLS */
58 /* */
59 /* _tx_thread_system_resume Resume thread service */
60 /* _tx_thread_system_ni_resume Non-interruptable resume */
61 /* thread */
62 /* */
63 /* CALLED BY */
64 /* */
65 /* Application Code */
66 /* */
67 /* RELEASE HISTORY */
68 /* */
69 /* DATE NAME DESCRIPTION */
70 /* */
71 /* 05-19-2020 William E. Lamie Initial Version 6.0 */
72 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
73 /* resulting in version 6.1 */
74 /* */
75 /**************************************************************************/
_tx_semaphore_ceiling_put(TX_SEMAPHORE * semaphore_ptr,ULONG ceiling)76 UINT _tx_semaphore_ceiling_put(TX_SEMAPHORE *semaphore_ptr, ULONG ceiling)
77 {
78
79 TX_INTERRUPT_SAVE_AREA
80
81 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
82 VOID (*semaphore_put_notify)(struct TX_SEMAPHORE_STRUCT *notify_semaphore_ptr);
83 #endif
84
85 TX_THREAD *thread_ptr;
86 UINT suspended_count;
87 TX_THREAD *next_thread;
88 TX_THREAD *previous_thread;
89 UINT status;
90
91
92 /* Default the status to TX_SUCCESS. */
93 status = TX_SUCCESS;
94
95 /* Disable interrupts to put an instance back to the semaphore. */
96 TX_DISABLE
97
98 #ifdef TX_SEMAPHORE_ENABLE_PERFORMANCE_INFO
99
100 /* Increment the total semaphore put counter. */
101 _tx_semaphore_performance_put_count++;
102
103 /* Increment the number of puts on this semaphore. */
104 semaphore_ptr -> tx_semaphore_performance_put_count++;
105 #endif
106
107 /* If trace is enabled, insert this event into the trace buffer. */
108 TX_TRACE_IN_LINE_INSERT(TX_TRACE_SEMAPHORE_CEILING_PUT, semaphore_ptr, semaphore_ptr -> tx_semaphore_count, semaphore_ptr -> tx_semaphore_suspended_count, ceiling, TX_TRACE_SEMAPHORE_EVENTS)
109
110 /* Log this kernel call. */
111 TX_EL_SEMAPHORE_CEILING_PUT_INSERT
112
113 /* Pickup the number of suspended threads. */
114 suspended_count = semaphore_ptr -> tx_semaphore_suspended_count;
115
116 /* Determine if there are any threads suspended on the semaphore. */
117 if (suspended_count == TX_NO_SUSPENSIONS)
118 {
119
120 /* Determine if the ceiling has been exceeded. */
121 if (semaphore_ptr -> tx_semaphore_count >= ceiling)
122 {
123
124 /* Restore interrupts. */
125 TX_RESTORE
126
127 /* Return an error. */
128 status = TX_CEILING_EXCEEDED;
129 }
130 else
131 {
132
133 /* Increment the semaphore count. */
134 semaphore_ptr -> tx_semaphore_count++;
135
136 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
137
138 /* Pickup the application notify function. */
139 semaphore_put_notify = semaphore_ptr -> tx_semaphore_put_notify;
140 #endif
141
142 /* Restore interrupts. */
143 TX_RESTORE
144
145 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
146
147 /* Determine if notification is required. */
148 if (semaphore_put_notify != TX_NULL)
149 {
150
151 /* Yes, call the appropriate notify callback function. */
152 (semaphore_put_notify)(semaphore_ptr);
153 }
154 #endif
155
156 /* Return successful completion status. */
157 status = TX_SUCCESS;
158 }
159 }
160 else
161 {
162
163 /* Remove the suspended thread from the list. */
164
165 /* Pickup the pointer to the first suspended thread. */
166 thread_ptr = semaphore_ptr -> tx_semaphore_suspension_list;
167
168 /* See if this is the only suspended thread on the list. */
169 suspended_count--;
170 if (suspended_count == TX_NO_SUSPENSIONS)
171 {
172
173 /* Yes, the only suspended thread. */
174
175 /* Update the head pointer. */
176 semaphore_ptr -> tx_semaphore_suspension_list = TX_NULL;
177 }
178 else
179 {
180
181 /* At least one more thread is on the same expiration list. */
182
183 /* Update the list head pointer. */
184 next_thread = thread_ptr -> tx_thread_suspended_next;
185 semaphore_ptr -> tx_semaphore_suspension_list = next_thread;
186
187 /* Update the links of the adjacent threads. */
188 previous_thread = thread_ptr -> tx_thread_suspended_previous;
189 next_thread -> tx_thread_suspended_previous = previous_thread;
190 previous_thread -> tx_thread_suspended_next = next_thread;
191 }
192
193 /* Decrement the suspension count. */
194 semaphore_ptr -> tx_semaphore_suspended_count = suspended_count;
195
196 /* Prepare for resumption of the first thread. */
197
198 /* Clear cleanup routine to avoid timeout. */
199 thread_ptr -> tx_thread_suspend_cleanup = TX_NULL;
200
201 /* Put return status into the thread control block. */
202 thread_ptr -> tx_thread_suspend_status = TX_SUCCESS;
203
204 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
205
206 /* Pickup the application notify function. */
207 semaphore_put_notify = semaphore_ptr -> tx_semaphore_put_notify;
208 #endif
209
210 #ifdef TX_NOT_INTERRUPTABLE
211
212 /* Resume the thread! */
213 _tx_thread_system_ni_resume(thread_ptr);
214
215 /* Restore interrupts. */
216 TX_RESTORE
217 #else
218
219 /* Temporarily disable preemption. */
220 _tx_thread_preempt_disable++;
221
222 /* Restore interrupts. */
223 TX_RESTORE
224
225 /* Resume thread. */
226 _tx_thread_system_resume(thread_ptr);
227 #endif
228
229 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
230
231 /* Determine if notification is required. */
232 if (semaphore_put_notify != TX_NULL)
233 {
234
235 /* Yes, call the appropriate notify callback function. */
236 (semaphore_put_notify)(semaphore_ptr);
237 }
238 #endif
239 }
240
241 /* Return successful completion. */
242 return(status);
243 }
244
245