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 // Start execution of the scheduler
181 static void start() CYGBLD_ATTRIB_NORET;
183 // Start execution of the scheduler on the current CPU
184 static void start_cpu() CYGBLD_ATTRIB_NORET;
186 // The only scheduler instance should be this one...
187 static Cyg_Scheduler scheduler CYGBLD_ANNOTATE_VARIABLE_SCHED;
191 // -------------------------------------------------------------------------
192 // This class encapsulates the scheduling abstractions in a thread.
193 // Cyg_SchedThread is included as a base class of Cyg_Thread. The actual
194 // implementation of the abstractions is in Cyg_SchedThread_Implementation
195 // so this class has little to do.
197 class Cyg_SchedThread
198 : public Cyg_SchedThread_Implementation
200 friend class Cyg_ThreadQueue_Implementation;
201 friend class Cyg_Scheduler_Implementation;
202 friend class Cyg_Scheduler;
204 Cyg_ThreadQueue *queue;
209 Cyg_SchedThread(Cyg_Thread *thread, CYG_ADDRWORD sched_info);
211 // Return current queue pointer
213 Cyg_ThreadQueue *get_current_queue();
215 // Remove this thread from current queue
218 #ifdef CYGSEM_KERNEL_SCHED_ASR_SUPPORT
221 // An ASR is an Asynchronous Service Routine. When set pending it
222 // is called when the thread exits the scheduler. ASRs are mainly
223 // used by compatibility subsystems, such as POSIX, to implement
224 // such things as thread cancellation and signal delivery.
228 volatile cyg_ucount32 asr_inhibit; // If > 0, blocks calls to ASRs
230 volatile cyg_bool asr_pending; // If true, this thread's ASR should be called.
232 #ifdef CYGSEM_KERNEL_SCHED_ASR_GLOBAL
235 Cyg_ASR *asr; // ASR function
236 #ifdef CYGSEM_KERNEL_SCHED_ASR_DATA_GLOBAL
239 CYG_ADDRWORD asr_data; // ASR data pointer
241 // Default ASR function
242 static void asr_default(CYG_ADDRWORD data);
246 // Public interface to ASR mechanism
248 // Set, clear and get inhibit flag.
249 inline void set_asr_inhibit() { asr_inhibit++; }
250 inline void clear_asr_inhibit() { asr_inhibit--; }
251 inline cyg_ucount32 get_asr_inhibit() { return asr_inhibit; }
253 // Set and get pending flag. The flag is only cleared when the
255 inline void set_asr_pending() { asr_pending = true; }
256 inline cyg_bool get_asr_pending() { return asr_pending; }
258 // Set a new ASR, returning the old one.
259 void set_asr( Cyg_ASR *new_asr, CYG_ADDRWORD new_data,
260 Cyg_ASR **old_asr, CYG_ADDRWORD *old_data);
262 // Clear the ASR function back to the default.
269 // Even when we do not have ASRs enabled, we keep these functions
270 // available. This avoids excessive ifdefs in the rest of the
272 inline void set_asr_inhibit() { }
273 inline void clear_asr_inhibit() { }
277 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL
281 // For all priority inversion protocols we need to keep track of how
282 // many mutexes we have locked, including one which we are waiting to
283 // lock, because we can inherit priority while sleeping just prior to
286 cyg_count32 mutex_count;
289 // These are implementation functions that are common to all protocols.
291 // Inherit the given priority. If thread is non-NULL the priority is
292 // being inherited from it, otherwise it has come from the mutex.
293 void set_inherited_priority( cyg_priority pri, Cyg_Thread *thread = 0 );
295 // Relay the priority of the ex-owner thread or from the queue if it
296 // has a higher priority than ours.
297 void relay_inherited_priority( Cyg_Thread *ex_owner, Cyg_ThreadQueue *pqueue);
299 // Lose priority inheritance
300 void clear_inherited_priority();
303 // Count and uncount the number of mutexes held by
305 void count_mutex() { mutex_count++; };
306 void uncount_mutex() { mutex_count--; };
308 #if defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE)
312 // The simple priority inversion protocols simply needs
313 // somewhere to store the base priority of the current thread.
315 cyg_priority original_priority; // our original priority
317 cyg_bool priority_inherited; // have we inherited?
321 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
325 // Inherit the priority of the provided thread if it
326 // has higher priority than this.
327 void inherit_priority( Cyg_Thread *thread);
329 // Relay the priority of the ex-owner thread or from the queue if it
330 // has a higher priority than ours.
331 void relay_priority( Cyg_Thread *ex_owner, Cyg_ThreadQueue *pqueue);
333 // Lose priority inheritance
334 void disinherit_priority();
338 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
342 // Set the priority of this thread to the given ceiling.
343 void set_priority_ceiling( cyg_priority pri );
345 // Clear the ceiling, if necessary.
346 void clear_priority_ceiling();
354 // -------------------------------------------------------------------------
355 // Simple inline accessor functions
357 inline Cyg_Thread *Cyg_Scheduler_Base::get_current_thread()
359 return current_thread[CYG_KERNEL_CPU_THIS()];
362 inline void Cyg_Scheduler_Base::set_current_thread(Cyg_Thread *thread )
364 current_thread[CYG_KERNEL_CPU_THIS()] = thread;
367 inline void Cyg_Scheduler_Base::set_current_thread(Cyg_Thread *thread,
368 HAL_SMP_CPU_TYPE cpu)
370 current_thread[cpu] = thread;
373 inline cyg_bool Cyg_Scheduler_Base::get_need_reschedule()
375 return need_reschedule[CYG_KERNEL_CPU_THIS()];
378 inline void Cyg_Scheduler_Base::set_need_reschedule()
380 need_reschedule[CYG_KERNEL_CPU_THIS()] = true;
383 inline void Cyg_Scheduler_Base::set_need_reschedule(Cyg_Thread *thread)
385 need_reschedule[CYG_KERNEL_CPU_THIS()] = true;
388 inline void Cyg_Scheduler_Base::clear_need_reschedule()
390 need_reschedule[CYG_KERNEL_CPU_THIS()] = false;
393 inline cyg_ucount32 Cyg_Scheduler_Base::get_sched_lock()
395 return Cyg_Scheduler_SchedLock::get_sched_lock();
398 // Return current number of thread switches
399 inline cyg_ucount32 Cyg_Scheduler_Base::get_thread_switches()
401 return thread_switches[CYG_KERNEL_CPU_THIS()];
404 // Return current queue pointer
405 inline Cyg_ThreadQueue *Cyg_SchedThread::get_current_queue()
410 // -------------------------------------------------------------------------
411 #endif // ifndef __SCHED_HXX__