|
0
|
1 //========================================================================== |
|
|
2 // |
|
2
|
3 // sched/sched.cxx |
|
0
|
4 // |
|
2
|
5 // Scheduler class implementations |
|
0
|
6 // |
|
|
7 //========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
2
|
32 // Author(s): nickg |
|
|
33 // Contributors: nickg |
|
|
34 // Date: 1997-09-15 |
|
|
35 // Purpose: Scheduler class implementation |
|
|
36 // Description: This file contains the definitions of the scheduler class |
|
0
|
37 // member functions that are common to all scheduler |
|
|
38 // implementations. |
|
|
39 // |
|
|
40 //####DESCRIPTIONEND#### |
|
|
41 // |
|
|
42 //========================================================================== |
|
|
43 |
|
|
44 #include <pkgconf/kernel.h> |
|
|
45 |
|
|
46 #include <cyg/kernel/ktypes.h> // base kernel types |
|
|
47 #include <cyg/infra/cyg_trac.h> // tracing macros |
|
|
48 #include <cyg/infra/cyg_ass.h> // assertion macros |
|
|
49 #include <cyg/kernel/instrmnt.h> // instrumentation |
|
|
50 |
|
|
51 #include <cyg/kernel/sched.hxx> // our header |
|
|
52 |
|
|
53 #include <cyg/kernel/thread.hxx> // thread classes |
|
|
54 #include <cyg/kernel/intr.hxx> // Interrupt interface |
|
|
55 |
|
|
56 #include <cyg/hal/hal_arch.h> // Architecture specific definitions |
|
|
57 |
|
|
58 #include <cyg/kernel/thread.inl> // thread inlines |
|
|
59 |
|
|
60 //------------------------------------------------------------------------- |
|
|
61 // Some local tracing control - a default. |
|
|
62 #ifdef CYGDBG_USE_TRACING |
|
|
63 # if !defined( CYGDBG_INFRA_DEBUG_TRACE_ASSERT_SIMPLE ) && \ |
|
|
64 !defined( CYGDBG_INFRA_DEBUG_TRACE_ASSERT_FANCY ) |
|
|
65 // ie. not a tracing implementation that takes a long time to output |
|
|
66 |
|
|
67 # ifndef CYGDBG_KERNEL_TRACE_UNLOCK_INNER |
|
|
68 # define CYGDBG_KERNEL_TRACE_UNLOCK_INNER |
|
|
69 # endif // control not already defined |
|
|
70 |
|
|
71 # endif // trace implementation not ..._SIMPLE && not ..._FANCY |
|
|
72 #endif // CYGDBG_USE_TRACING |
|
|
73 |
|
|
74 // ------------------------------------------------------------------------- |
|
|
75 // Static Cyg_Scheduler class members |
|
|
76 |
|
|
77 // We start with sched_lock at 1 so that any kernel code we |
|
|
78 // call during initialization will not try to reschedule. |
|
|
79 |
|
|
80 volatile cyg_ucount32 Cyg_Scheduler_Base::sched_lock = 1; |
|
|
81 |
|
|
82 Cyg_Thread *Cyg_Scheduler_Base::current_thread = NULL; |
|
|
83 |
|
|
84 cyg_bool Cyg_Scheduler_Base::need_reschedule = false; |
|
|
85 |
|
|
86 Cyg_Scheduler Cyg_Scheduler::scheduler CYG_INIT_PRIORITY( SCHEDULER ); |
|
|
87 |
|
|
88 cyg_ucount32 Cyg_Scheduler_Base::thread_switches = 0; |
|
|
89 |
|
|
90 // ------------------------------------------------------------------------- |
|
|
91 // Scheduler unlock function. |
|
|
92 // This is only called when the lock is to be decremented to zero and there |
|
|
93 // is the potential for real work to be done. Other cases are handled in |
|
|
94 // Cyg_Scheduler::unlock() which is an inline. |
|
|
95 |
|
|
96 void Cyg_Scheduler::unlock_inner() |
|
|
97 { |
|
|
98 #ifdef CYGDBG_KERNEL_TRACE_UNLOCK_INNER |
|
|
99 CYG_REPORT_FUNCTION(); |
|
|
100 #endif |
|
|
101 |
|
|
102 do { |
|
|
103 |
|
|
104 CYG_PRECONDITION( sched_lock == 1 , "sched_lock not 1" ); |
|
|
105 |
|
|
106 #ifdef CYGIMP_KERNEL_INTERRUPTS_DSRS |
|
|
107 |
|
|
108 // Call any pending DSRs. Do this here to ensure that any |
|
|
109 // threads that get awakened are properly scheduled. |
|
|
110 |
|
|
111 if( Cyg_Interrupt::DSRs_pending() ) |
|
|
112 Cyg_Interrupt::call_pending_DSRs(); |
|
|
113 #endif |
|
|
114 |
|
|
115 Cyg_Thread *current = current_thread; |
|
|
116 |
|
|
117 CYG_ASSERTCLASS( current, "Bad current thread" ); |
|
|
118 |
|
|
119 // If the current thread is going to sleep, or someone |
|
|
120 // wants a reschedule, choose another thread to run |
|
|
121 |
|
|
122 if( current->state != Cyg_Thread::RUNNING || need_reschedule ) { |
|
|
123 |
|
|
124 CYG_INSTRUMENT_SCHED(RESCHEDULE,0,0); |
|
|
125 |
|
|
126 // Get the next thread to run from scheduler |
|
|
127 Cyg_Thread *next = scheduler.schedule(); |
|
|
128 |
|
|
129 CYG_CHECK_DATA_PTR( next, "Invalid next thread pointer"); |
|
|
130 CYG_ASSERTCLASS( next, "Bad next thread" ); |
|
|
131 |
|
|
132 if( current != next ) |
|
|
133 { |
|
|
134 |
|
|
135 CYG_INSTRUMENT_THREAD(SWITCH,current,next); |
|
|
136 |
|
|
137 // Count this thread switch |
|
|
138 thread_switches++; |
|
|
139 |
|
|
140 // Switch contexts |
|
|
141 HAL_THREAD_SWITCH_CONTEXT( ¤t->stack_ptr, |
|
|
142 &next->stack_ptr ); |
|
|
143 |
|
|
144 // Worry here about possible compiler |
|
|
145 // optimizations across the above call that may try to |
|
|
146 // propogate common subexpresions. We would end up |
|
|
147 // with the expression from one thread in its |
|
|
148 // successor. This is only a worry if we do not save |
|
|
149 // and restore the complete register set. We need a |
|
|
150 // way of marking functions that return into a |
|
|
151 // different context. A temporary fix would be to |
|
|
152 // disable CSE (-fdisable-cse) in the compiler. |
|
|
153 |
|
|
154 // We return here only when the current thread is |
|
|
155 // rescheduled. There is a bit of housekeeping to do |
|
|
156 // here before we are allowed to go on our way. |
|
|
157 |
|
|
158 CYG_CHECK_DATA_PTR( current, "Invalid current thread pointer"); |
|
|
159 CYG_ASSERTCLASS( current, "Bad current thread" ); |
|
|
160 |
|
|
161 current_thread = current; // restore current thread pointer |
|
|
162 |
|
|
163 } |
|
|
164 |
|
|
165 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE |
|
|
166 // Reset the timeslice counter so that this thread gets a full |
|
|
167 // quantum. |
|
|
168 reset_timeslice_count(); |
|
|
169 #endif |
|
|
170 |
|
|
171 need_reschedule = false; // finished rescheduling |
|
|
172 } |
|
|
173 |
|
2
|
174 HAL_REORDER_BARRIER(); // Make sure everything above has happened |
|
|
175 // by this point |
|
0
|
176 sched_lock = 0; // Clear the lock |
|
2
|
177 HAL_REORDER_BARRIER(); |
|
|
178 |
|
0
|
179 #ifdef CYGIMP_KERNEL_INTERRUPTS_DSRS |
|
|
180 |
|
|
181 // Now check whether any DSRs got posted during the thread |
|
|
182 // switch and if so, go around again. Making this test after |
|
|
183 // the lock has been zeroed avoids a race condition in which |
|
|
184 // a DSR could have been posted during a reschedule, but would |
|
|
185 // not be run until the _next_ time we release the sched lock. |
|
|
186 |
|
|
187 if( Cyg_Interrupt::DSRs_pending() ) { |
|
|
188 sched_lock = 1; // reclaim the lock |
|
|
189 continue; // go back to head of loop |
|
|
190 } |
|
|
191 |
|
|
192 #endif |
|
|
193 // Otherwise the lock is zero, we can return. |
|
|
194 |
|
|
195 CYG_POSTCONDITION( sched_lock == 0, "sched_lock not zero" ); |
|
|
196 |
|
|
197 #ifdef CYGDBG_KERNEL_TRACE_UNLOCK_INNER |
|
|
198 CYG_REPORT_RETURN(); |
|
|
199 #endif |
|
|
200 return; |
|
|
201 |
|
|
202 } while( 1 ); |
|
|
203 |
|
|
204 CYG_FAIL( "Should not be executed" ); |
|
|
205 } |
|
|
206 |
|
|
207 // ------------------------------------------------------------------------- |
|
|
208 // Start the scheduler. This is called after the initial threads have been |
|
|
209 // created to start scheduling. |
|
|
210 |
|
|
211 void Cyg_Scheduler::start() |
|
|
212 { |
|
|
213 CYG_REPORT_FUNCTION(); |
|
|
214 |
|
|
215 // Get the first thread to run from scheduler |
|
|
216 register Cyg_Thread *next = scheduler.schedule(); |
|
|
217 |
|
|
218 CYG_ASSERTCLASS( next, "Bad initial thread" ); |
|
|
219 |
|
|
220 need_reschedule = false; // finished rescheduling |
|
|
221 current_thread = next; // restore current thread pointer |
|
|
222 |
|
2
|
223 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK |
|
|
224 // Reference the real time clock. This ensures that at least one |
|
|
225 // reference to the kernel_clock.o object exists, without which |
|
|
226 // the object will not be included while linking. |
|
|
227 CYG_REFERENCE_OBJECT( Cyg_Clock::real_time_clock ); |
|
|
228 #endif |
|
|
229 |
|
0
|
230 // Let the interrupts go |
|
|
231 Cyg_Interrupt::enable_interrupts(); |
|
|
232 |
|
|
233 HAL_THREAD_LOAD_CONTEXT( &next->stack_ptr ); |
|
|
234 |
|
|
235 } |
|
|
236 |
|
|
237 // ------------------------------------------------------------------------- |
|
|
238 // Consistency checker |
|
|
239 |
|
|
240 #ifdef CYGDBG_USE_ASSERTS |
|
|
241 |
|
2
|
242 bool Cyg_Scheduler::check_this( cyg_assert_class_zeal zeal) const |
|
0
|
243 { |
|
|
244 CYG_REPORT_FUNCTION(); |
|
|
245 |
|
|
246 // check that we have a non-NULL pointer first |
|
|
247 if( this == NULL ) return false; |
|
|
248 |
|
|
249 switch( zeal ) |
|
|
250 { |
|
|
251 case cyg_system_test: |
|
|
252 case cyg_extreme: |
|
|
253 case cyg_thorough: |
|
|
254 if( !current_thread->check_this(zeal) ) return false; |
|
|
255 case cyg_quick: |
|
|
256 case cyg_trivial: |
|
|
257 case cyg_none: |
|
|
258 default: |
|
|
259 break; |
|
|
260 }; |
|
|
261 |
|
|
262 return true; |
|
|
263 } |
|
|
264 |
|
|
265 #endif |
|
|
266 |
|
|
267 //========================================================================== |
|
|
268 // SchedThread members |
|
|
269 |
|
|
270 // ------------------------------------------------------------------------- |
|
|
271 // Constructor |
|
|
272 |
|
|
273 Cyg_SchedThread::Cyg_SchedThread(Cyg_Thread *thread, CYG_ADDRWORD sched_info) |
|
|
274 : Cyg_SchedThread_Implementation(sched_info) |
|
|
275 { |
|
|
276 CYG_REPORT_FUNCTION(); |
|
|
277 |
|
|
278 queue = NULL; |
|
|
279 |
|
|
280 if( Cyg_Scheduler::current_thread == NULL ) |
|
|
281 Cyg_Scheduler::current_thread = thread; |
|
|
282 |
|
|
283 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE_SIMPLE |
|
|
284 |
|
|
285 mutex_count = 0; |
|
|
286 priority_inherited = false; |
|
|
287 |
|
|
288 #endif |
|
|
289 |
|
|
290 } |
|
|
291 |
|
|
292 // ------------------------------------------------------------------------- |
|
|
293 // Priority inheritance support. |
|
|
294 |
|
|
295 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE |
|
|
296 |
|
|
297 // ------------------------------------------------------------------------- |
|
|
298 // Inherit the priority of the provided thread if it |
|
|
299 // has a higher priority than ours. |
|
|
300 |
|
|
301 void Cyg_SchedThread::inherit_priority( Cyg_Thread *thread) |
|
|
302 { |
|
|
303 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE_SIMPLE |
|
|
304 |
|
|
305 // A simple implementation of priority inheritance. If the other |
|
|
306 // thread is of higher priority, reset our priority to his. The |
|
|
307 // first time we do this, save our original priority. |
|
|
308 |
|
|
309 Cyg_Thread *self = CYG_CLASSFROMBASE(Cyg_Thread, |
|
|
310 Cyg_SchedThread, |
|
|
311 this); |
|
|
312 |
|
|
313 CYG_ASSERT( mutex_count > 0, "Non-positive mutex count"); |
|
|
314 CYG_ASSERT( self != thread, "Trying to inherit from self!"); |
|
|
315 |
|
|
316 if( thread->get_priority() < priority ) |
|
|
317 { |
|
|
318 cyg_priority mypri = priority; |
|
|
319 |
|
|
320 if( !priority_inherited ) |
|
|
321 { |
|
|
322 // If this is first inheritance, copy the old pri |
|
|
323 // and set inherited flag. We do this after setting the |
|
|
324 // pri since set_priority() is inheritance aware. |
|
|
325 |
|
|
326 self->set_priority( thread->get_priority() ); |
|
|
327 priority_inherited = true, |
|
|
328 original_priority = mypri; |
|
|
329 } |
|
|
330 else |
|
|
331 { |
|
|
332 // Already in inherited state, and new pri is higher |
|
|
333 // than old. Just change the pri. |
|
|
334 |
|
|
335 self->set_priority( thread->get_priority() ); |
|
|
336 } |
|
|
337 } |
|
|
338 |
|
|
339 #endif |
|
|
340 } |
|
|
341 |
|
|
342 // ------------------------------------------------------------------------- |
|
|
343 // Lose a priority inheritance |
|
|
344 |
|
|
345 void Cyg_SchedThread::disinherit_priority() |
|
|
346 { |
|
|
347 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE_SIMPLE |
|
|
348 |
|
|
349 // A simple implementation of priority inheritance. The |
|
|
350 // simplification in this algorithm is that we do not reduce our |
|
|
351 // priority until we have freed all mutexes claimed. Hence we can |
|
|
352 // continue to run at an artificially high priority even when we |
|
|
353 // should not. However, since nested mutexes are rare, the thread |
|
|
354 // we have inherited from is likely to be locking the same mutexes |
|
|
355 // we are, and mutex claim periods should be very short, the |
|
|
356 // performance difference between this and a more complex algorithm |
|
|
357 // should be negligible. The most important advantage of this |
|
|
358 // algorithm is that it is fast and deterministic. |
|
|
359 |
|
|
360 Cyg_Thread *self = CYG_CLASSFROMBASE(Cyg_Thread, |
|
|
361 Cyg_SchedThread, |
|
|
362 this); |
|
|
363 |
|
|
364 CYG_ASSERT( mutex_count >= 0, "Non-positive mutex count"); |
|
|
365 |
|
|
366 if( mutex_count == 0 && priority_inherited ) |
|
|
367 { |
|
|
368 priority_inherited = false; |
|
|
369 |
|
|
370 // Only make an effort if the priority must change |
|
|
371 if( priority < original_priority ) |
|
|
372 self->set_priority( original_priority ); |
|
|
373 |
|
|
374 } |
|
|
375 |
|
|
376 #endif |
|
|
377 } |
|
|
378 |
|
|
379 #endif |
|
|
380 |
|
|
381 // ------------------------------------------------------------------------- |
|
|
382 // EOF sched/sched.cxx |