1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** ThreadX Component */
/** */
/** Thread */
/** */
/**************************************************************************/
/**************************************************************************/
#define TX_SOURCE_CODE
#define TX_THREAD_SMP_SOURCE_CODE
/* Include necessary system files. */
#include "tx_api.h"
#include "tx_trace.h"
#include "tx_thread.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _tx_thread_priority_change PORTABLE SMP */
/* 6.1 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function changes the priority of the specified thread. It */
/* also returns the old priority and handles preemption if the calling */
/* thread is currently executing and the priority change results in a */
/* higher priority thread ready for execution. */
/* */
/* Note: the preemption-threshold is automatically changed to the new */
/* priority. */
/* */
/* INPUT */
/* */
/* thread_ptr Pointer to thread to suspend */
/* new_priority New thread priority */
/* old_priority Old thread priority */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* _tx_thread_smp_rebalance_execute_list Rebalance the execution list */
/* _tx_thread_smp_simple_priority_change Change priority */
/* _tx_thread_system_resume Resume thread */
/* _tx_thread_system_ni_resume Non-interruptable resume */
/* _tx_thread_system_suspend Suspend thread */
/* _tx_thread_system_ni_suspend Non-interruptable suspend */
/* _tx_thread_system_preempt_check Check for preemption */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/* RELEASE HISTORY */
/* */
/* DATE NAME DESCRIPTION */
/* */
/* 09-30-2020 William E. Lamie Initial Version 6.1 */
/* */
/**************************************************************************/
UINT _tx_thread_priority_change(TX_THREAD *thread_ptr, UINT new_priority, UINT *old_priority)
{
TX_INTERRUPT_SAVE_AREA
TX_THREAD *execute_ptr;
UINT core_index;
UINT original_priority;
UINT lowest_priority;
TX_THREAD *original_pt_thread;
#ifndef TX_DISABLE_PREEMPTION_THRESHOLD
TX_THREAD *new_pt_thread;
UINT priority;
ULONG priority_bit;
#if TX_MAX_PRIORITIES > 32
UINT map_index;
#endif
#endif
UINT status;
/* Default status to not done. */
status = TX_NOT_DONE;
/* Lockout interrupts while the thread is being suspended. */
TX_DISABLE
/* If trace is enabled, insert this event into the trace buffer. */
TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_PRIORITY_CHANGE, thread_ptr, new_priority, thread_ptr -> tx_thread_priority, thread_ptr -> tx_thread_state, TX_TRACE_THREAD_EVENTS)
/* Log this kernel call. */
TX_EL_THREAD_PRIORITY_CHANGE_INSERT
/* Save the previous priority. */
*old_priority = thread_ptr -> tx_thread_user_priority;
/* Determine if the new priority is the same as the last requested priority. */
if ((thread_ptr -> tx_thread_user_priority == new_priority) &&
(thread_ptr -> tx_thread_user_preempt_threshold == new_priority))
{
/* Nothing to do at this point, simply return. */
/* Restore interrupts. */
TX_RESTORE
/* Done, return success. */
status = TX_SUCCESS;
}
/* Determine if the inherit priority is in effect and there is no preemption-threshold in force. */
else if ((thread_ptr -> tx_thread_inherit_priority < new_priority) &&
(thread_ptr -> tx_thread_user_preempt_threshold == thread_ptr -> tx_thread_user_priority))
{
/* In this case, simply setup the user priority and preemption-threshold and return. */
/* Setup the new priority for this thread. */
thread_ptr -> tx_thread_user_priority = new_priority;
thread_ptr -> tx_thread_user_preempt_threshold = new_priority;
/* Restore interrupts. */
TX_RESTORE
/* Done, return success. */
status = TX_SUCCESS;
}
else
{
/* Default the execute pointer to NULL. */
execute_ptr = TX_NULL;
/* Determine if this thread is currently ready. */
if (thread_ptr -> tx_thread_state != TX_READY)
{
/* Setup the user priority and threshold in the thread's control
block. */
thread_ptr -> tx_thread_user_priority = new_priority;
thread_ptr -> tx_thread_user_preempt_threshold = new_priority;
/* Determine if the actual thread priority should be setup, which is the
case if the new priority is higher than the priority inheritance. */
if (new_priority < thread_ptr -> tx_thread_inherit_priority)
{
/* Change thread priority to the new user's priority. */
thread_ptr -> tx_thread_priority = new_priority;
thread_ptr -> tx_thread_preempt_threshold = new_priority;
}
else
{
/* Change thread priority to the priority inheritance. */
thread_ptr -> tx_thread_priority = thread_ptr -> tx_thread_inherit_priority;
thread_ptr -> tx_thread_preempt_threshold = thread_ptr -> tx_thread_inherit_priority;
}
/* Restore interrupts. */
TX_RESTORE
/* Done, return success. */
status = TX_SUCCESS;
}
else
{
/* Pickup the core index. */
core_index = thread_ptr -> tx_thread_smp_core_mapped;
/* Save the original priority. */
original_priority = thread_ptr -> tx_thread_priority;
/* Determine if this thread is the currently scheduled thread. */
if (thread_ptr == _tx_thread_execute_ptr[core_index])
{
/* Yes, this thread is scheduled. */
/* Remember this thread as the currently executing thread. */
execute_ptr = thread_ptr;
/* Determine if the thread is being set to a higher-priority and it does't have
preemption-threshold set. */
if ((new_priority < thread_ptr -> tx_thread_priority) &&
(thread_ptr -> tx_thread_user_priority == thread_ptr -> tx_thread_user_preempt_threshold))
{
/* Simple case, remove the thread from the current priority list and place in
the higher priority list. */
_tx_thread_smp_simple_priority_change(thread_ptr, new_priority);
/* Setup the new priority for this thread. */
thread_ptr -> tx_thread_user_priority = new_priority;
thread_ptr -> tx_thread_user_preempt_threshold = new_priority;
/* Restore interrupts. */
TX_RESTORE
/* Return a successful completion. */
status = TX_SUCCESS;
}
}
else
{
/* Thread is not currently executing, so it can just be moved to the lower priority in the list. */
/* Determine if the thread is being set to a lower-priority and it does't have
preemption-threshold set. */
if ((new_priority > thread_ptr -> tx_thread_priority) &&
(thread_ptr -> tx_thread_user_priority == thread_ptr -> tx_thread_user_preempt_threshold))
{
/* Simple case, remove the thread from the current priority list and place in
the lower priority list. */
_tx_thread_smp_simple_priority_change(thread_ptr, new_priority);
/* Setup the new priority for this thread. */
thread_ptr -> tx_thread_user_priority = new_priority;
thread_ptr -> tx_thread_user_preempt_threshold = new_priority;
/* Restore interrupts. */
TX_RESTORE
/* Return a successful completion. */
status = TX_SUCCESS;
}
}
/* Determine if we are done. */
if (status == TX_NOT_DONE)
{
/* Yes, more to do. */
/* Default the status to success. */
status = TX_SUCCESS;
/* Save the original preemption-threshold thread. */
original_pt_thread = _tx_thread_preemption__threshold_scheduled;
#ifdef TX_NOT_INTERRUPTABLE
/* Increment the preempt disable flag. */
_tx_thread_preempt_disable++;
/* Set the state to priority change. */
thread_ptr -> tx_thread_state = TX_PRIORITY_CHANGE;
/* Call actual non-interruptable thread suspension routine. */
_tx_thread_system_ni_suspend(thread_ptr, ((ULONG) 0));
/* At this point, the preempt disable flag is still set, so we still have
protection against all preemption. */
/* Setup the new priority for this thread. */
thread_ptr -> tx_thread_user_priority = new_priority;
thread_ptr -> tx_thread_user_preempt_threshold = new_priority;
/* Determine if the actual thread priority should be setup, which is the
case if the new priority is higher than the priority inheritance. */
if (new_priority < thread_ptr -> tx_thread_inherit_priority)
{
/* Change thread priority to the new user's priority. */
thread_ptr -> tx_thread_priority = new_priority;
thread_ptr -> tx_thread_preempt_threshold = new_priority;
}
else
{
/* Change thread priority to the priority inheritance. */
thread_ptr -> tx_thread_priority = thread_ptr -> tx_thread_inherit_priority;
thread_ptr -> tx_thread_preempt_threshold = thread_ptr -> tx_thread_inherit_priority;
}
/* Resume the thread with the new priority. */
_tx_thread_system_ni_resume(thread_ptr);
/* Decrement the preempt disable flag. */
_tx_thread_preempt_disable--;
#else
/* Increment the preempt disable flag. */
_tx_thread_preempt_disable = _tx_thread_preempt_disable + ((UINT) 3);
/* Set the state to priority change. */
thread_ptr -> tx_thread_state = TX_PRIORITY_CHANGE;
/* Set the suspending flag. */
thread_ptr -> tx_thread_suspending = TX_TRUE;
/* Setup the timeout period. */
thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks = ((ULONG) 0);
/* Restore interrupts. */
TX_RESTORE
/* The thread is ready and must first be removed from the list. Call the
system suspend function to accomplish this. */
_tx_thread_system_suspend(thread_ptr);
/* Lockout interrupts again. */
TX_DISABLE
/* At this point, the preempt disable flag is still set, so we still have
protection against all preemption. */
/* Setup the new priority for this thread. */
thread_ptr -> tx_thread_user_priority = new_priority;
thread_ptr -> tx_thread_user_preempt_threshold = new_priority;
/* Determine if the actual thread priority should be setup, which is the
case if the new priority is higher than the priority inheritance. */
if (new_priority < thread_ptr -> tx_thread_inherit_priority)
{
/* Change thread priority to the new user's priority. */
thread_ptr -> tx_thread_priority = new_priority;
thread_ptr -> tx_thread_preempt_threshold = new_priority;
}
else
{
/* Change thread priority to the priority inheritance. */
thread_ptr -> tx_thread_priority = thread_ptr -> tx_thread_inherit_priority;
thread_ptr -> tx_thread_preempt_threshold = thread_ptr -> tx_thread_inherit_priority;
}
/* Restore interrupts. */
TX_RESTORE
/* Resume the thread with the new priority. */
_tx_thread_system_resume(thread_ptr);
#endif
/* Optional processing extension. */
TX_THREAD_PRIORITY_CHANGE_EXTENSION
#ifndef TX_NOT_INTERRUPTABLE
/* Disable interrupts. */
TX_DISABLE
/* Decrement the preemption-threshold flag. */
_tx_thread_preempt_disable--;
#endif
/* Determine if the thread was previously executing. */
if (thread_ptr == execute_ptr)
{
/* Make sure the thread is still ready. */
if (thread_ptr -> tx_thread_state == TX_READY)
{
#ifndef TX_DISABLE_PREEMPTION_THRESHOLD
/* Determine if preemption-threshold is in force at the new priority level. */
if (_tx_thread_preemption_threshold_list[thread_ptr -> tx_thread_priority] == TX_NULL)
{
/* Ensure that this thread is placed at the front of the priority list. */
_tx_thread_priority_list[thread_ptr -> tx_thread_priority] = thread_ptr;
}
#else
/* Ensure that this thread is placed at the front of the priority list. */
_tx_thread_priority_list[thread_ptr -> tx_thread_priority] = thread_ptr;
#endif
}
}
/* Pickup the core index. */
core_index = thread_ptr -> tx_thread_smp_core_mapped;
#ifndef TX_THREAD_SMP_DYNAMIC_CORE_MAX
/* Pickup the next thread to execute. */
if (core_index < ((UINT) TX_THREAD_SMP_MAX_CORES))
#else
/* Pickup the next thread to execute. */
if (core_index < _tx_thread_smp_max_cores)
#endif
{
/* Determine if this thread is not the next thread to execute. */
if (thread_ptr != _tx_thread_execute_ptr[core_index])
{
/* Now determine if this thread was previously executing thread. */
if (thread_ptr == execute_ptr)
{
/* Determine if we moved to a lower priority. If so, move the thread to the front of the priority list. */
if (original_priority < new_priority)
{
/* Make sure the thread is still ready. */
if (thread_ptr -> tx_thread_state == TX_READY)
{
/* Determine the lowest priority scheduled thread. */
lowest_priority = _tx_thread_smp_lowest_priority_get();
/* Determine if this thread has a higher or same priority as the lowest priority
in the list. */
if (thread_ptr -> tx_thread_priority <= lowest_priority)
{
/* Yes, we need to rebalance to make it possible for this thread to execute. */
/* Determine if the thread with preemption-threshold thread has changed... and is
not the scheduled thread. */
if ((original_pt_thread != _tx_thread_preemption__threshold_scheduled) &&
(original_pt_thread != thread_ptr))
{
/* Yes, preemption-threshold has changed. Determine if it can or should
be reversed. */
#ifndef TX_DISABLE_PREEMPTION_THRESHOLD
/* Pickup the preemption-threshold thread. */
new_pt_thread = _tx_thread_preemption__threshold_scheduled;
#endif
/* Restore the original preemption-threshold thread. */
_tx_thread_preemption__threshold_scheduled = original_pt_thread;
#ifndef TX_DISABLE_PREEMPTION_THRESHOLD
/* Determine if there is a new preemption-threshold thread to reverse. */
if (new_pt_thread != TX_NULL)
{
/* Clear the information associated with the new preemption-threshold thread. */
/* Pickup the priority. */
priority = new_pt_thread -> tx_thread_priority;
/* Clear the preempted list entry. */
_tx_thread_preemption_threshold_list[priority] = TX_NULL;
#if TX_MAX_PRIORITIES > 32
/* Calculate the bit map array index. */
map_index = new_priority/((UINT) 32);
#endif
/* Ensure that this thread's priority is clear in the preempt map. */
TX_MOD32_BIT_SET(priority, priority_bit)
_tx_thread_preempted_maps[MAP_INDEX] = _tx_thread_preempted_maps[MAP_INDEX] & (~(priority_bit));
#if TX_MAX_PRIORITIES > 32
/* Determine if there are any other bits set in this preempt map. */
if (_tx_thread_preempted_maps[MAP_INDEX] == ((ULONG) 0))
{
/* No, clear the active bit to signify this preempted map has nothing set. */
TX_DIV32_BIT_SET(priority, priority_bit)
_tx_thread_preempted_map_active = _tx_thread_preempted_map_active & (~(priority_bit));
}
#endif
}
#endif
}
/* Pickup the index. */
core_index = TX_SMP_CORE_ID;
/* Call the rebalance routine. This routine maps cores and ready threads. */
_tx_thread_smp_rebalance_execute_list(core_index);
}
}
}
}
}
}
/* Restore interrupts. */
TX_RESTORE
/* Check for preemption. */
_tx_thread_system_preempt_check();
}
}
}
/* Return completion status. */
return(status);
}
|