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