1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Inlined Routines Associated with the Manipulation of the |
---|
5 | * Priority-Based Scheduling Structures |
---|
6 | * |
---|
7 | * This inline file contains all of the inlined routines associated with |
---|
8 | * the manipulation of the priority-based scheduling structures. |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * Copyright (C) 2010 Gedare Bloom. |
---|
13 | * Copyright (C) 2011 On-Line Applications Research Corporation (OAR). |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.com/license/LICENSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifndef _RTEMS_SCORE_SCHEDULERPRIORITY_H |
---|
21 | # error "Never use <rtems/score/schedulerpriority.inl> directly; include <rtems/score/schedulerpriority.h> instead." |
---|
22 | #endif |
---|
23 | |
---|
24 | #ifndef _RTEMS_SCORE_SCHEDULERPRIORITY_INL |
---|
25 | #define _RTEMS_SCORE_SCHEDULERPRIORITY_INL |
---|
26 | |
---|
27 | #include <rtems/score/wkspace.h> |
---|
28 | |
---|
29 | /** |
---|
30 | * @addtogroup ScoreScheduler |
---|
31 | * |
---|
32 | * @{ |
---|
33 | */ |
---|
34 | |
---|
35 | /** |
---|
36 | * @brief Ready queue initialization. |
---|
37 | * |
---|
38 | * This routine initializes @a the_ready_queue for priority-based scheduling. |
---|
39 | */ |
---|
40 | RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_initialize(void) |
---|
41 | { |
---|
42 | size_t index; |
---|
43 | Chain_Control *ready_queues; |
---|
44 | |
---|
45 | /* allocate ready queue structures */ |
---|
46 | _Scheduler.information = _Workspace_Allocate_or_fatal_error( |
---|
47 | ((size_t) PRIORITY_MAXIMUM + 1) * sizeof(Chain_Control) |
---|
48 | ); |
---|
49 | |
---|
50 | /* initialize ready queue structures */ |
---|
51 | ready_queues = (Chain_Control *) _Scheduler.information; |
---|
52 | for( index=0; index <= PRIORITY_MAXIMUM; index++) |
---|
53 | _Chain_Initialize_empty( &ready_queues[index] ); |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * @brief Put a thread to the ready queue. |
---|
58 | * |
---|
59 | * This routine puts @a the_thread on to the priority-based ready queue. |
---|
60 | * |
---|
61 | * @param[in] the_thread is a pointer to the thread |
---|
62 | */ |
---|
63 | RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_enqueue( |
---|
64 | Thread_Control *the_thread |
---|
65 | ) |
---|
66 | { |
---|
67 | Scheduler_priority_Per_thread *sched_info; |
---|
68 | Chain_Control *ready; |
---|
69 | |
---|
70 | sched_info = (Scheduler_priority_Per_thread *) the_thread->scheduler_info; |
---|
71 | ready = sched_info->ready_chain; |
---|
72 | |
---|
73 | _Priority_bit_map_Add( &sched_info->Priority_map ); |
---|
74 | |
---|
75 | _Chain_Append_unprotected( ready, &the_thread->Object.Node ); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * @brief Put a thread to the head of the ready queue. |
---|
80 | * |
---|
81 | * This routine puts @a the_thread to the head of the ready queue. |
---|
82 | * For priority-based ready queues, the thread will be the first thread |
---|
83 | * at its priority level. |
---|
84 | * |
---|
85 | * @param[in] the_thread is a pointer to the thread. |
---|
86 | */ |
---|
87 | RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_enqueue_first( |
---|
88 | Thread_Control *the_thread |
---|
89 | ) |
---|
90 | { |
---|
91 | Scheduler_priority_Per_thread *sched_info; |
---|
92 | |
---|
93 | sched_info = (Scheduler_priority_Per_thread *) the_thread->scheduler_info; |
---|
94 | |
---|
95 | _Priority_bit_map_Add( &sched_info->Priority_map ); |
---|
96 | |
---|
97 | _Chain_Prepend_unprotected( |
---|
98 | sched_info->ready_chain, |
---|
99 | &the_thread->Object.Node |
---|
100 | ); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * @brief Remove a specific thread from the ready queue. |
---|
105 | * |
---|
106 | * This routine removes a specific thread from the specified |
---|
107 | * priority-based ready queue. |
---|
108 | * |
---|
109 | * @param[in] the_thread is a pointer to the thread. |
---|
110 | */ |
---|
111 | RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_extract( |
---|
112 | Thread_Control *the_thread |
---|
113 | ) |
---|
114 | { |
---|
115 | Scheduler_priority_Per_thread *sched_info; |
---|
116 | Chain_Control *ready; |
---|
117 | |
---|
118 | sched_info = (Scheduler_priority_Per_thread *) the_thread->scheduler_info; |
---|
119 | ready = sched_info->ready_chain; |
---|
120 | |
---|
121 | if ( _Chain_Has_only_one_node( ready ) ) { |
---|
122 | _Chain_Initialize_empty( ready ); |
---|
123 | _Priority_bit_map_Remove( &sched_info->Priority_map ); |
---|
124 | } else { |
---|
125 | _Chain_Extract_unprotected( &the_thread->Object.Node ); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief Return a pointer to the first thread. |
---|
131 | * |
---|
132 | * This routines returns a pointer to the first thread on @a the_ready_queue. |
---|
133 | * |
---|
134 | * @param[in] the_ready_queue - pointer to thread queue |
---|
135 | * |
---|
136 | * @return This method returns the first thread or NULL |
---|
137 | */ |
---|
138 | RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_priority_Ready_queue_first( |
---|
139 | Chain_Control *the_ready_queue |
---|
140 | ) |
---|
141 | { |
---|
142 | Priority_Control index = _Priority_bit_map_Get_highest(); |
---|
143 | |
---|
144 | if ( !_Chain_Is_empty( &the_ready_queue[ index ] ) ) |
---|
145 | return (Thread_Control *) _Chain_First( &the_ready_queue[ index ] ); |
---|
146 | |
---|
147 | return NULL; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * @brief Requeue a thread on the ready queue. |
---|
152 | * |
---|
153 | * This routine is invoked when a thread changes priority and should be |
---|
154 | * moved to a different position on the ready queue. |
---|
155 | * |
---|
156 | * @param[in] the_thread is a pointer to the thread |
---|
157 | */ |
---|
158 | RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_requeue( |
---|
159 | Thread_Control *the_thread |
---|
160 | ) |
---|
161 | { |
---|
162 | Scheduler_priority_Per_thread *sched_info; |
---|
163 | |
---|
164 | sched_info = (Scheduler_priority_Per_thread *) the_thread->scheduler_info; |
---|
165 | |
---|
166 | if ( !_Chain_Has_only_one_node( sched_info->ready_chain ) ) { |
---|
167 | _Chain_Extract_unprotected( &the_thread->Object.Node ); |
---|
168 | |
---|
169 | _Chain_Append_unprotected( |
---|
170 | sched_info->ready_chain, |
---|
171 | &the_thread->Object.Node |
---|
172 | ); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * @brief Scheduling decision logic. |
---|
178 | * |
---|
179 | * This kernel routine implements scheduling decision logic |
---|
180 | * for priority-based scheduling. |
---|
181 | */ |
---|
182 | RTEMS_INLINE_ROUTINE void _Scheduler_priority_Schedule_body(void) |
---|
183 | { |
---|
184 | _Thread_Heir = _Scheduler_priority_Ready_queue_first( |
---|
185 | (Chain_Control *) _Scheduler.information |
---|
186 | ); |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * @brief Priority comparison. |
---|
191 | * |
---|
192 | * This routine implements priority comparison for priority-based |
---|
193 | * scheduling. |
---|
194 | * |
---|
195 | * @return >0 for higher priority, 0 for equal and <0 for lower priority. |
---|
196 | */ |
---|
197 | RTEMS_INLINE_ROUTINE int _Scheduler_priority_Priority_compare_body( |
---|
198 | Priority_Control p1, |
---|
199 | Priority_Control p2 |
---|
200 | ) |
---|
201 | { |
---|
202 | /* High priority in priority scheduler is represented by low numbers. */ |
---|
203 | return ( p2 - p1 ); |
---|
204 | } |
---|
205 | |
---|
206 | /** @} */ |
---|
207 | |
---|
208 | #endif |
---|
209 | /* end of include file */ |
---|