1 | /* |
---|
2 | * Thread Handler / Change Priority |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2011. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/score/isr.h> |
---|
18 | #include <rtems/score/scheduler.h> |
---|
19 | #include <rtems/score/schedulerpriority.h> |
---|
20 | #include <rtems/score/thread.h> |
---|
21 | #include <rtems/score/threadq.h> |
---|
22 | |
---|
23 | void _Thread_Change_priority( |
---|
24 | Thread_Control *the_thread, |
---|
25 | Priority_Control new_priority, |
---|
26 | bool prepend_it |
---|
27 | ) |
---|
28 | { |
---|
29 | ISR_Level level; |
---|
30 | States_Control state, original_state; |
---|
31 | |
---|
32 | /* |
---|
33 | * Save original state |
---|
34 | */ |
---|
35 | original_state = the_thread->current_state; |
---|
36 | |
---|
37 | /* |
---|
38 | * Set a transient state for the thread so it is pulled off the Ready chains. |
---|
39 | * This will prevent it from being scheduled no matter what happens in an |
---|
40 | * ISR. |
---|
41 | */ |
---|
42 | _Thread_Set_transient( the_thread ); |
---|
43 | |
---|
44 | /* |
---|
45 | * Do not bother recomputing all the priority related information if |
---|
46 | * we are not REALLY changing priority. |
---|
47 | */ |
---|
48 | if ( the_thread->current_priority != new_priority ) |
---|
49 | _Thread_Set_priority( the_thread, new_priority ); |
---|
50 | |
---|
51 | _ISR_Disable( level ); |
---|
52 | |
---|
53 | /* |
---|
54 | * If the thread has more than STATES_TRANSIENT set, then it is blocked, |
---|
55 | * If it is blocked on a thread queue, then we need to requeue it. |
---|
56 | */ |
---|
57 | state = the_thread->current_state; |
---|
58 | if ( state != STATES_TRANSIENT ) { |
---|
59 | /* Only clear the transient state if it wasn't set already */ |
---|
60 | if ( ! _States_Is_transient( original_state ) ) |
---|
61 | the_thread->current_state = _States_Clear( STATES_TRANSIENT, state ); |
---|
62 | _ISR_Enable( level ); |
---|
63 | if ( _States_Is_waiting_on_thread_queue( state ) ) { |
---|
64 | _Thread_queue_Requeue( the_thread->Wait.queue, the_thread ); |
---|
65 | } |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | /* Only clear the transient state if it wasn't set already */ |
---|
70 | if ( ! _States_Is_transient( original_state ) ) { |
---|
71 | /* |
---|
72 | * Interrupts are STILL disabled. |
---|
73 | * We now know the thread will be in the READY state when we remove |
---|
74 | * the TRANSIENT state. So we have to place it on the appropriate |
---|
75 | * Ready Queue with interrupts off. |
---|
76 | */ |
---|
77 | the_thread->current_state = _States_Clear( STATES_TRANSIENT, state ); |
---|
78 | |
---|
79 | if ( prepend_it ) |
---|
80 | _Scheduler_Enqueue_first( the_thread ); |
---|
81 | else |
---|
82 | _Scheduler_Enqueue( the_thread ); |
---|
83 | } |
---|
84 | |
---|
85 | _ISR_Flash( level ); |
---|
86 | |
---|
87 | /* |
---|
88 | * We altered the set of thread priorities. So let's figure out |
---|
89 | * who is the heir and if we need to switch to them. |
---|
90 | */ |
---|
91 | _Scheduler_Schedule(); |
---|
92 | |
---|
93 | if ( !_Thread_Is_executing_also_the_heir() && |
---|
94 | _Thread_Executing->is_preemptible ) |
---|
95 | _Thread_Dispatch_necessary = true; |
---|
96 | _ISR_Enable( level ); |
---|
97 | } |
---|