1 #ifndef CYGONCE_KERNEL_SCHED_HXX
2 #define CYGONCE_KERNEL_SCHED_HXX
4 //==========================================================================
8 // Scheduler class declaration(s)
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
47 // Contributors: nickg
49 // Purpose: Define Scheduler class interfaces
50 // Description: These class definitions supply the internal API
51 // used to scheduler threads.
52 // Usage: #include <cyg/kernel/sched.hxx>
54 //####DESCRIPTIONEND####
56 //==========================================================================
58 #include <cyg/kernel/ktypes.h>
59 #include <cyg/infra/cyg_ass.h> // assertion macros
61 #include <cyg/kernel/smp.hxx> // SMP support
63 // -------------------------------------------------------------------------
64 // Miscellaneous types
66 #ifdef CYGSEM_KERNEL_SCHED_ASR_SUPPORT
68 typedef void Cyg_ASR( CYG_ADDRWORD data ); // ASR type signature
72 __externC void cyg_scheduler_set_need_reschedule();
74 // -------------------------------------------------------------------------
75 // Scheduler base class. This defines stuff that is needed by the
76 // specific scheduler implementation. Each scheduler comprises three
77 // classes: Cyg_Scheduler_Base, Cyg_Scheduler_Implementation which
78 // inherits from it and Cyg_Scheduler which inherits from _it_ in turn.
80 class Cyg_Scheduler_Base
81 : public Cyg_Scheduler_SchedLock
83 friend class Cyg_HardwareThread;
84 friend class Cyg_SchedThread;
87 // The following variables are implicit in the API, but are
88 // not publically visible.
90 // Current running thread
91 static Cyg_Thread * volatile current_thread[CYGNUM_KERNEL_CPU_MAX]
92 CYGBLD_ANNOTATE_VARIABLE_SCHED;
94 // Set when reschedule needed
95 static volatile cyg_bool need_reschedule[CYGNUM_KERNEL_CPU_MAX]
96 CYGBLD_ANNOTATE_VARIABLE_SCHED;
98 // Count of number of thread switches
99 static volatile cyg_ucount32 thread_switches[CYGNUM_KERNEL_CPU_MAX]
100 CYGBLD_ANNOTATE_VARIABLE_SCHED;
104 // return a pointer to the current thread
105 static Cyg_Thread *get_current_thread();
107 // Set current thread pointer
108 static void set_current_thread(Cyg_Thread *thread);
109 static void set_current_thread(Cyg_Thread *thread, HAL_SMP_CPU_TYPE cpu);
111 // Set need_reschedule flag
112 static void set_need_reschedule();
113 static void set_need_reschedule(Cyg_Thread *thread);
115 // Get need_reschedule flag
116 static cyg_bool get_need_reschedule();
118 // Return current value of lock
119 static cyg_ucount32 get_sched_lock();
121 // Clear need_reschedule flag
122 static void clear_need_reschedule();
124 // Return current number of thread switches
125 static cyg_ucount32 get_thread_switches();
129 // -------------------------------------------------------------------------
130 // Include the scheduler implementation header
132 #include CYGPRI_KERNEL_SCHED_IMPL_HXX
134 // Do some checking that we have a consistent universe.
136 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL
137 # ifndef CYGIMP_THREAD_PRIORITY
138 # error Priority inversion protocols will not work without priorities!!!
142 // -------------------------------------------------------------------------
143 // Scheduler class. This is the public scheduler interface seen by the
144 // rest of the kernel.
147 : public Cyg_Scheduler_Implementation
149 friend class Cyg_Thread;
151 // This function is the actual implementation of the unlock
152 // function. The unlock() later is an inline shell that deals
153 // with the common case.
155 static void unlock_inner(cyg_uint32 new_lock = 0);
159 CYGDBG_DEFINE_CHECK_THIS
161 // The following API functions are common to all scheduler
164 // claim the preemption lock
167 // release the preemption lock and possibly reschedule
168 static void unlock();
170 // release and reclaim the lock atomically, keeping the old
172 static void reschedule();
174 // decrement the lock but also look for a reschedule opportunity
175 static void unlock_reschedule();
177 // release the preemption lock without rescheduling
178 static void unlock_simple();
180 // perform thread startup housekeeping
181 void thread_entry( Cyg_Thread *thread );
183 // Start execution of the scheduler
184 static void start() CYGBLD_ATTRIB_NORET;
186 // Start execution of the scheduler on the current CPU
187 static void start_cpu() CYGBLD_ATTRIB_NORET;
189 // The only scheduler instance should be this one...
190 static Cyg_Scheduler scheduler CYGBLD_ANNOTATE_VARIABLE_SCHED;
194 // -------------------------------------------------------------------------
195 // This class encapsulates the scheduling abstractions in a thread.
196 // Cyg_SchedThread is included as a base class of Cyg_Thread. The actual
197 // implementation of the abstractions is in Cyg_SchedThread_Implementation
198 // so this class has little to do.
200 class Cyg_SchedThread
201 : public Cyg_SchedThread_Implementation
203 friend class Cyg_ThreadQueue_Implementation;
204 friend class Cyg_Scheduler_Implementation;
205 friend class Cyg_Scheduler;
207 Cyg_ThreadQueue *queue;
212 Cyg_SchedThread(Cyg_Thread *thread, CYG_ADDRWORD sched_info);
214 // Return current queue pointer
216 Cyg_ThreadQueue *get_current_queue();
218 // Remove this thread from current queue
221 #ifdef CYGSEM_KERNEL_SCHED_ASR_SUPPORT
224 // An ASR is an Asynchronous Service Routine. When set pending it
225 // is called when the thread exits the scheduler. ASRs are mainly
226 // used by compatibility subsystems, such as POSIX, to implement
227 // such things as thread cancellation and signal delivery.
231 volatile cyg_ucount32 asr_inhibit; // If > 0, blocks calls to ASRs
233 volatile cyg_bool asr_pending; // If true, this thread's ASR should be called.
235 #ifdef CYGSEM_KERNEL_SCHED_ASR_GLOBAL
238 Cyg_ASR *asr; // ASR function
239 #ifdef CYGSEM_KERNEL_SCHED_ASR_DATA_GLOBAL
242 CYG_ADDRWORD asr_data; // ASR data pointer
244 // Default ASR function
245 static void asr_default(CYG_ADDRWORD data);
249 // Public interface to ASR mechanism
251 // Set, clear and get inhibit flag.
252 inline void set_asr_inhibit() { asr_inhibit++; }
253 inline void clear_asr_inhibit() { asr_inhibit--; }
254 inline cyg_ucount32 get_asr_inhibit() { return asr_inhibit; }
256 // Set and get pending flag. The flag is only cleared when the
258 inline void set_asr_pending() { asr_pending = true; }
259 inline cyg_bool get_asr_pending() { return asr_pending; }
261 // Set a new ASR, returning the old one.
262 void set_asr( Cyg_ASR *new_asr, CYG_ADDRWORD new_data,
263 Cyg_ASR **old_asr, CYG_ADDRWORD *old_data);
265 // Clear the ASR function back to the default.
272 // Even when we do not have ASRs enabled, we keep these functions
273 // available. This avoids excessive ifdefs in the rest of the
275 inline void set_asr_inhibit() { }
276 inline void clear_asr_inhibit() { }
280 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL
284 // For all priority inversion protocols we need to keep track of how
285 // many mutexes we have locked, including one which we are waiting to
286 // lock, because we can inherit priority while sleeping just prior to
289 cyg_count32 mutex_count;
292 // These are implementation functions that are common to all protocols.
294 // Inherit the given priority. If thread is non-NULL the priority is
295 // being inherited from it, otherwise it has come from the mutex.
296 void set_inherited_priority( cyg_priority pri, Cyg_Thread *thread = 0 );
298 // Relay the priority of the ex-owner thread or from the queue if it
299 // has a higher priority than ours.
300 void relay_inherited_priority( Cyg_Thread *ex_owner, Cyg_ThreadQueue *pqueue);
302 // Lose priority inheritance
303 void clear_inherited_priority();
306 // Count and uncount the number of mutexes held by
308 void count_mutex() { mutex_count++; };
309 void uncount_mutex() { mutex_count--; };
311 #if defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE)
315 // The simple priority inversion protocols simply needs
316 // somewhere to store the base priority of the current thread.
318 cyg_priority original_priority; // our original priority
320 cyg_bool priority_inherited; // have we inherited?
324 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
328 // Inherit the priority of the provided thread if it
329 // has higher priority than this.
330 void inherit_priority( Cyg_Thread *thread);
332 // Relay the priority of the ex-owner thread or from the queue if it
333 // has a higher priority than ours.
334 void relay_priority( Cyg_Thread *ex_owner, Cyg_ThreadQueue *pqueue);
336 // Lose priority inheritance
337 void disinherit_priority();
341 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
345 // Set the priority of this thread to the given ceiling.
346 void set_priority_ceiling( cyg_priority pri );
348 // Clear the ceiling, if necessary.
349 void clear_priority_ceiling();
357 // -------------------------------------------------------------------------
358 // Simple inline accessor functions
360 inline Cyg_Thread *Cyg_Scheduler_Base::get_current_thread()
362 return current_thread[CYG_KERNEL_CPU_THIS()];
365 inline void Cyg_Scheduler_Base::set_current_thread(Cyg_Thread *thread )
367 current_thread[CYG_KERNEL_CPU_THIS()] = thread;
370 inline void Cyg_Scheduler_Base::set_current_thread(Cyg_Thread *thread,
371 HAL_SMP_CPU_TYPE cpu)
373 current_thread[cpu] = thread;
376 inline cyg_bool Cyg_Scheduler_Base::get_need_reschedule()
378 return need_reschedule[CYG_KERNEL_CPU_THIS()];
381 inline void Cyg_Scheduler_Base::set_need_reschedule()
383 need_reschedule[CYG_KERNEL_CPU_THIS()] = true;
386 inline void Cyg_Scheduler_Base::set_need_reschedule(Cyg_Thread *thread)
388 need_reschedule[CYG_KERNEL_CPU_THIS()] = true;
391 inline void Cyg_Scheduler_Base::clear_need_reschedule()
393 need_reschedule[CYG_KERNEL_CPU_THIS()] = false;
396 inline cyg_ucount32 Cyg_Scheduler_Base::get_sched_lock()
398 return Cyg_Scheduler_SchedLock::get_sched_lock();
401 // Return current number of thread switches
402 inline cyg_ucount32 Cyg_Scheduler_Base::get_thread_switches()
404 return thread_switches[CYG_KERNEL_CPU_THIS()];
407 // Return current queue pointer
408 inline Cyg_ThreadQueue *Cyg_SchedThread::get_current_queue()
413 // -------------------------------------------------------------------------
414 #endif // ifndef __SCHED_HXX__