1 | /* |
---|
2 | * Thread Handler |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2006. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/score/apiext.h> |
---|
21 | #include <rtems/score/context.h> |
---|
22 | #include <rtems/score/interr.h> |
---|
23 | #include <rtems/score/isr.h> |
---|
24 | #include <rtems/score/object.h> |
---|
25 | #include <rtems/score/priority.h> |
---|
26 | #include <rtems/score/states.h> |
---|
27 | #include <rtems/score/sysstate.h> |
---|
28 | #include <rtems/score/thread.h> |
---|
29 | #include <rtems/score/threadq.h> |
---|
30 | #include <rtems/score/userext.h> |
---|
31 | #include <rtems/score/wkspace.h> |
---|
32 | |
---|
33 | /*PAGE |
---|
34 | * |
---|
35 | * _Thread_Change_priority |
---|
36 | * |
---|
37 | * This kernel routine changes the priority of the thread. The |
---|
38 | * thread chain is adjusted if necessary. |
---|
39 | * |
---|
40 | * Input parameters: |
---|
41 | * the_thread - pointer to thread control block |
---|
42 | * new_priority - ultimate priority |
---|
43 | * prepend_it - true if the thread should be prepended to the chain |
---|
44 | * |
---|
45 | * Output parameters: NONE |
---|
46 | * |
---|
47 | * INTERRUPT LATENCY: |
---|
48 | * ready chain |
---|
49 | * select heir |
---|
50 | */ |
---|
51 | |
---|
52 | void _Thread_Change_priority( |
---|
53 | Thread_Control *the_thread, |
---|
54 | Priority_Control new_priority, |
---|
55 | bool prepend_it |
---|
56 | ) |
---|
57 | { |
---|
58 | ISR_Level level; |
---|
59 | States_Control state, original_state; |
---|
60 | |
---|
61 | /* |
---|
62 | * If this is a case where prepending the task to its priority is |
---|
63 | * potentially desired, then we need to consider whether to do it. |
---|
64 | * This usually occurs when a task lowers its priority implcitly as |
---|
65 | * the result of losing inherited priority. Normal explicit priority |
---|
66 | * change calls (e.g. rtems_task_set_priority) should always do an |
---|
67 | * append not a prepend. |
---|
68 | */ |
---|
69 | /* |
---|
70 | if ( prepend_it && |
---|
71 | _Thread_Is_executing( the_thread ) && |
---|
72 | new_priority >= the_thread->current_priority ) |
---|
73 | prepend_it = true; |
---|
74 | */ |
---|
75 | |
---|
76 | /* |
---|
77 | * Save original state |
---|
78 | */ |
---|
79 | original_state = the_thread->current_state; |
---|
80 | |
---|
81 | /* |
---|
82 | * Set a transient state for the thread so it is pulled off the Ready chains. |
---|
83 | * This will prevent it from being scheduled no matter what happens in an |
---|
84 | * ISR. |
---|
85 | */ |
---|
86 | _Thread_Set_transient( the_thread ); |
---|
87 | |
---|
88 | /* |
---|
89 | * Do not bother recomputing all the priority related information if |
---|
90 | * we are not REALLY changing priority. |
---|
91 | */ |
---|
92 | if ( the_thread->current_priority != new_priority ) |
---|
93 | _Thread_Set_priority( the_thread, new_priority ); |
---|
94 | |
---|
95 | _ISR_Disable( level ); |
---|
96 | |
---|
97 | /* |
---|
98 | * If the thread has more than STATES_TRANSIENT set, then it is blocked, |
---|
99 | * If it is blocked on a thread queue, then we need to requeue it. |
---|
100 | */ |
---|
101 | state = the_thread->current_state; |
---|
102 | if ( state != STATES_TRANSIENT ) { |
---|
103 | /* Only clear the transient state if it wasn't set already */ |
---|
104 | if ( ! _States_Is_transient( original_state ) ) |
---|
105 | the_thread->current_state = _States_Clear( STATES_TRANSIENT, state ); |
---|
106 | _ISR_Enable( level ); |
---|
107 | if ( _States_Is_waiting_on_thread_queue( state ) ) { |
---|
108 | _Thread_queue_Requeue( the_thread->Wait.queue, the_thread ); |
---|
109 | } |
---|
110 | return; |
---|
111 | } |
---|
112 | |
---|
113 | /* Only clear the transient state if it wasn't set already */ |
---|
114 | if ( ! _States_Is_transient( original_state ) ) { |
---|
115 | /* |
---|
116 | * Interrupts are STILL disabled. |
---|
117 | * We now know the thread will be in the READY state when we remove |
---|
118 | * the TRANSIENT state. So we have to place it on the appropriate |
---|
119 | * Ready Queue with interrupts off. |
---|
120 | */ |
---|
121 | the_thread->current_state = _States_Clear( STATES_TRANSIENT, state ); |
---|
122 | |
---|
123 | _Priority_Add_to_bit_map( &the_thread->Priority_map ); |
---|
124 | if ( prepend_it ) |
---|
125 | _Chain_Prepend_unprotected( the_thread->ready, &the_thread->Object.Node ); |
---|
126 | else |
---|
127 | _Chain_Append_unprotected( the_thread->ready, &the_thread->Object.Node ); |
---|
128 | } |
---|
129 | |
---|
130 | _ISR_Flash( level ); |
---|
131 | |
---|
132 | /* |
---|
133 | * We altered the set of thread priorities. So let's figure out |
---|
134 | * who is the heir and if we need to switch to them. |
---|
135 | */ |
---|
136 | _Thread_Calculate_heir(); |
---|
137 | |
---|
138 | if ( !_Thread_Is_executing_also_the_heir() && |
---|
139 | _Thread_Executing->is_preemptible ) |
---|
140 | _Context_Switch_necessary = true; |
---|
141 | _ISR_Enable( level ); |
---|
142 | } |
---|