1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Changes the Priority of a Thread |
---|
5 | * |
---|
6 | * @ingroup ScoreThread |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2011. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #if HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <rtems/score/threadimpl.h> |
---|
23 | #include <rtems/score/schedulerimpl.h> |
---|
24 | #include <rtems/score/threadqimpl.h> |
---|
25 | |
---|
26 | /** |
---|
27 | * @brief Invoked when a thread changes priority and is blocked. |
---|
28 | * |
---|
29 | * This routine is invoked when a thread changes priority and is |
---|
30 | * blocked on a thread queue. If the queue is priority ordered, |
---|
31 | * the_thread is removed from the_thread_queue and reinserted using |
---|
32 | * its new priority. This method has no impact on the state of the_thread |
---|
33 | * or of any timeouts associated with this blocking. |
---|
34 | * |
---|
35 | * @param[in] the_thread_queue pointer to a threadq header |
---|
36 | * @param[in] the_thread pointer to a thread control block |
---|
37 | */ |
---|
38 | static void _Thread_queue_Requeue( |
---|
39 | Thread_queue_Control *the_thread_queue, |
---|
40 | Thread_Control *the_thread |
---|
41 | ) |
---|
42 | { |
---|
43 | /* |
---|
44 | * Just in case the thread really wasn't blocked on a thread queue |
---|
45 | * when we get here. |
---|
46 | */ |
---|
47 | if ( !the_thread_queue ) |
---|
48 | return; |
---|
49 | |
---|
50 | /* |
---|
51 | * If queueing by FIFO, there is nothing to do. This only applies to |
---|
52 | * priority blocking discipline. |
---|
53 | */ |
---|
54 | if ( the_thread_queue->discipline == THREAD_QUEUE_DISCIPLINE_PRIORITY ) { |
---|
55 | Thread_queue_Control *tq = the_thread_queue; |
---|
56 | ISR_Level level; |
---|
57 | ISR_Level level_ignored; |
---|
58 | |
---|
59 | _ISR_Disable( level ); |
---|
60 | if ( _States_Is_waiting_on_thread_queue( the_thread->current_state ) ) { |
---|
61 | _Thread_queue_Enter_critical_section( tq ); |
---|
62 | _Thread_queue_Extract_priority_helper( |
---|
63 | the_thread, |
---|
64 | the_thread->Wait.return_code, |
---|
65 | true |
---|
66 | ); |
---|
67 | (void) _Thread_queue_Enqueue_priority( tq, the_thread, &level_ignored ); |
---|
68 | } |
---|
69 | _ISR_Enable( level ); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | void _Thread_Change_priority( |
---|
74 | Thread_Control *the_thread, |
---|
75 | Priority_Control new_priority, |
---|
76 | bool prepend_it |
---|
77 | ) |
---|
78 | { |
---|
79 | /* |
---|
80 | * Do not bother recomputing all the priority related information if |
---|
81 | * we are not REALLY changing priority. |
---|
82 | */ |
---|
83 | if ( the_thread->current_priority != new_priority ) { |
---|
84 | ISR_Level level; |
---|
85 | const Scheduler_Control *scheduler; |
---|
86 | |
---|
87 | _ISR_Disable( level ); |
---|
88 | |
---|
89 | scheduler = _Scheduler_Get( the_thread ); |
---|
90 | the_thread->current_priority = new_priority; |
---|
91 | |
---|
92 | if ( _States_Is_ready( the_thread->current_state ) ) { |
---|
93 | _Scheduler_Change_priority( |
---|
94 | scheduler, |
---|
95 | the_thread, |
---|
96 | new_priority, |
---|
97 | prepend_it |
---|
98 | ); |
---|
99 | |
---|
100 | _ISR_Flash( level ); |
---|
101 | |
---|
102 | /* |
---|
103 | * We altered the set of thread priorities. So let's figure out |
---|
104 | * who is the heir and if we need to switch to them. |
---|
105 | */ |
---|
106 | scheduler = _Scheduler_Get( the_thread ); |
---|
107 | _Scheduler_Schedule( scheduler, the_thread ); |
---|
108 | } else { |
---|
109 | _Scheduler_Update( scheduler, the_thread ); |
---|
110 | } |
---|
111 | _ISR_Enable( level ); |
---|
112 | |
---|
113 | _Thread_queue_Requeue( the_thread->Wait.queue, the_thread ); |
---|
114 | } |
---|
115 | } |
---|