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 /** Thread */
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
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _tx_thread_preemption_change PORTABLE C */
37 /* 6.1 */
38 /* AUTHOR */
39 /* */
40 /* William E. Lamie, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function processes preemption-threshold change requests. The */
45 /* previous preemption is returned to the caller. If the new request */
46 /* allows a higher priority thread to execute, preemption takes place */
47 /* inside of this function. */
48 /* */
49 /* INPUT */
50 /* */
51 /* thread_ptr Pointer to thread */
52 /* new_threshold New preemption threshold */
53 /* old_threshold Old preemption threshold */
54 /* */
55 /* OUTPUT */
56 /* */
57 /* status Service return status */
58 /* */
59 /* CALLS */
60 /* */
61 /* _tx_thread_system_preempt_check Check for preemption */
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_thread_preemption_change(TX_THREAD * thread_ptr,UINT new_threshold,UINT * old_threshold)76 UINT _tx_thread_preemption_change(TX_THREAD *thread_ptr, UINT new_threshold, UINT *old_threshold)
77 {
78
79 TX_INTERRUPT_SAVE_AREA
80
81 #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
82 ULONG priority_bit;
83 #if TX_MAX_PRIORITIES > 32
84 UINT map_index;
85 #endif
86 #endif
87 UINT status;
88
89
90 /* Default status to success. */
91 status = TX_SUCCESS;
92
93 #ifdef TX_DISABLE_PREEMPTION_THRESHOLD
94
95 /* Only allow 0 (disable all preemption) and returning preemption-threshold to the
96 current thread priority if preemption-threshold is disabled. All other threshold
97 values are converted to 0. */
98 if (thread_ptr -> tx_thread_user_priority != new_threshold)
99 {
100
101 /* Is the new threshold zero? */
102 if (new_threshold != ((UINT) 0))
103 {
104
105 /* Convert the new threshold to disable all preemption, since preemption-threshold is
106 not supported. */
107 new_threshold = ((UINT) 0);
108 }
109 }
110 #endif
111
112 /* Lockout interrupts while the thread is being resumed. */
113 TX_DISABLE
114
115 /* If trace is enabled, insert this event into the trace buffer. */
116 TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_PREEMPTION_CHANGE, thread_ptr, new_threshold, thread_ptr -> tx_thread_preempt_threshold, thread_ptr -> tx_thread_state, TX_TRACE_THREAD_EVENTS)
117
118 /* Log this kernel call. */
119 TX_EL_THREAD_PREEMPTION_CHANGE_INSERT
120
121 /* Determine if the new threshold is greater than the current user priority. */
122 if (new_threshold > thread_ptr -> tx_thread_user_priority)
123 {
124
125 /* Return error. */
126 status = TX_THRESH_ERROR;
127 }
128 else
129 {
130
131 #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
132
133 /* Determine if the new threshold is the same as the priority. */
134 if (thread_ptr -> tx_thread_user_priority == new_threshold)
135 {
136
137 /* Determine if this thread is at the head of the list. */
138 if (_tx_thread_priority_list[thread_ptr -> tx_thread_priority] == thread_ptr)
139 {
140
141 #if TX_MAX_PRIORITIES > 32
142
143 /* Calculate the index into the bit map array. */
144 map_index = (thread_ptr -> tx_thread_priority)/((UINT) 32);
145 #endif
146
147 /* Yes, this thread is at the front of the list. Make sure
148 the preempted bit is cleared for this thread. */
149 TX_MOD32_BIT_SET(thread_ptr -> tx_thread_priority, priority_bit)
150 _tx_thread_preempted_maps[MAP_INDEX] = _tx_thread_preempted_maps[MAP_INDEX] & (~(priority_bit));
151
152 #if TX_MAX_PRIORITIES > 32
153
154 /* Determine if there are any other bits set in this preempt map. */
155 if (_tx_thread_preempted_maps[MAP_INDEX] == ((ULONG) 0))
156 {
157
158 /* No, clear the active bit to signify this preempt map has nothing set. */
159 TX_DIV32_BIT_SET(thread_ptr -> tx_thread_priority, priority_bit)
160 _tx_thread_preempted_map_active = _tx_thread_preempted_map_active & (~(priority_bit));
161 }
162 #endif
163 }
164 }
165 #endif
166
167 /* Return the user's preemption-threshold. */
168 *old_threshold = thread_ptr -> tx_thread_user_preempt_threshold;
169
170 /* Setup the new threshold. */
171 thread_ptr -> tx_thread_user_preempt_threshold = new_threshold;
172
173 /* Determine if the new threshold represents a higher priority than the priority inheritance threshold. */
174 if (new_threshold < thread_ptr -> tx_thread_inherit_priority)
175 {
176
177 /* Update the actual preemption-threshold with the new threshold. */
178 thread_ptr -> tx_thread_preempt_threshold = new_threshold;
179 }
180 else
181 {
182
183 /* Update the actual preemption-threshold with the priority inheritance. */
184 thread_ptr -> tx_thread_preempt_threshold = thread_ptr -> tx_thread_inherit_priority;
185 }
186
187 /* Is the thread priority less than the current highest priority? If not, no preemption is required. */
188 if (_tx_thread_highest_priority < thread_ptr -> tx_thread_priority)
189 {
190
191 /* Is the new thread preemption-threshold less than the current highest priority? If not, no preemption is required. */
192 if (_tx_thread_highest_priority < new_threshold)
193 {
194
195 /* If the current execute pointer is the same at this thread, preemption needs to take place. */
196 if (_tx_thread_execute_ptr == thread_ptr)
197 {
198
199 /* Preemption needs to take place. */
200
201 #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
202
203 /* Determine if this thread has preemption threshold set. */
204 if (thread_ptr -> tx_thread_preempt_threshold != thread_ptr -> tx_thread_priority)
205 {
206
207 #if TX_MAX_PRIORITIES > 32
208
209 /* Calculate the index into the bit map array. */
210 map_index = (thread_ptr -> tx_thread_priority)/((UINT) 32);
211
212 /* Set the active bit to remember that the preempt map has something set. */
213 TX_DIV32_BIT_SET(thread_ptr -> tx_thread_priority, priority_bit)
214 _tx_thread_preempted_map_active = _tx_thread_preempted_map_active | priority_bit;
215 #endif
216
217 /* Remember that this thread was preempted by a thread above the thread's threshold. */
218 TX_MOD32_BIT_SET(thread_ptr -> tx_thread_priority, priority_bit)
219 _tx_thread_preempted_maps[MAP_INDEX] = _tx_thread_preempted_maps[MAP_INDEX] | priority_bit;
220 }
221 #endif
222
223 #ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO
224
225 /* Determine if the caller is an interrupt or from a thread. */
226 if (TX_THREAD_GET_SYSTEM_STATE() == ((ULONG) 0))
227 {
228
229 /* Caller is a thread, so this is a solicited preemption. */
230 _tx_thread_performance_solicited_preemption_count++;
231
232 /* Increment the thread's solicited preemption counter. */
233 thread_ptr -> tx_thread_performance_solicited_preemption_count++;
234 }
235
236 /* Remember the thread that preempted this thread. */
237 thread_ptr -> tx_thread_performance_last_preempting_thread = _tx_thread_priority_list[_tx_thread_highest_priority];
238
239 /* Is the execute pointer different? */
240 if (_tx_thread_performance_execute_log[_tx_thread_performance__execute_log_index] != _tx_thread_execute_ptr)
241 {
242
243 /* Move to next entry. */
244 _tx_thread_performance__execute_log_index++;
245
246 /* Check for wrap condition. */
247 if (_tx_thread_performance__execute_log_index >= TX_THREAD_EXECUTE_LOG_SIZE)
248 {
249
250 /* Set the index to the beginning. */
251 _tx_thread_performance__execute_log_index = ((UINT) 0);
252 }
253
254 /* Log the new execute pointer. */
255 _tx_thread_performance_execute_log[_tx_thread_performance__execute_log_index] = _tx_thread_execute_ptr;
256 }
257 #endif
258
259 /* Setup the highest priority thread to execute. */
260 _tx_thread_execute_ptr = _tx_thread_priority_list[_tx_thread_highest_priority];
261
262 /* Restore interrupts. */
263 TX_RESTORE
264
265 /* Check for preemption. */
266 _tx_thread_system_preempt_check();
267
268 /* Disable interrupts. */
269 TX_DISABLE
270 }
271 }
272 }
273 }
274
275 /* Restore interrupts. */
276 TX_RESTORE
277
278 /* Return completion status. */
279 return(status);
280 }
281
282