]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/include/sched.hxx
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / kernel / v2_0 / include / sched.hxx
1 #ifndef CYGONCE_KERNEL_SCHED_HXX
2 #define CYGONCE_KERNEL_SCHED_HXX
3
4 //==========================================================================
5 //
6 //      sched.hxx
7 //
8 //      Scheduler class declaration(s)
9 //
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.
15 //
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.
19 //
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
23 // for more details.
24 //
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.
28 //
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.
35 //
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.
38 //
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####
45 //
46 // Author(s):   nickg
47 // Contributors:        nickg
48 // Date:        1997-09-09
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>
53 //
54 //####DESCRIPTIONEND####
55 //
56 //==========================================================================
57
58 #include <cyg/kernel/ktypes.h>
59 #include <cyg/infra/cyg_ass.h>         // assertion macros
60
61 #include <cyg/kernel/smp.hxx>          // SMP support
62
63 // -------------------------------------------------------------------------
64 // Miscellaneous types
65
66 #ifdef CYGSEM_KERNEL_SCHED_ASR_SUPPORT
67
68 typedef void Cyg_ASR( CYG_ADDRWORD data );      // ASR type signature
69
70 #endif
71
72 __externC void cyg_scheduler_set_need_reschedule();
73
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.
79
80 class Cyg_Scheduler_Base
81      : public Cyg_Scheduler_SchedLock
82 {
83     friend class Cyg_HardwareThread;
84     friend class Cyg_SchedThread;
85     
86 protected:
87     // The following variables are implicit in the API, but are
88     // not publically visible.
89
90     // Current running thread    
91     static Cyg_Thread * volatile current_thread[CYGNUM_KERNEL_CPU_MAX]
92                                                 CYGBLD_ANNOTATE_VARIABLE_SCHED; 
93
94     // Set when reschedule needed
95     static volatile cyg_bool     need_reschedule[CYGNUM_KERNEL_CPU_MAX]
96                                                  CYGBLD_ANNOTATE_VARIABLE_SCHED; 
97
98     // Count of number of thread switches
99     static volatile cyg_ucount32 thread_switches[CYGNUM_KERNEL_CPU_MAX]
100                                                  CYGBLD_ANNOTATE_VARIABLE_SCHED; 
101
102 public:
103
104     // return a pointer to the current thread
105     static Cyg_Thread *get_current_thread();
106
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);
110     
111     // Set need_reschedule flag
112     static void set_need_reschedule();
113     static void set_need_reschedule(Cyg_Thread *thread);
114
115     // Get need_reschedule flag
116     static cyg_bool get_need_reschedule();
117
118     // Return current value of lock
119     static cyg_ucount32 get_sched_lock();
120
121     // Clear need_reschedule flag
122     static void clear_need_reschedule();
123     
124     // Return current number of thread switches
125     static cyg_ucount32 get_thread_switches();
126     
127 };
128
129 // -------------------------------------------------------------------------
130 // Include the scheduler implementation header
131
132 #include CYGPRI_KERNEL_SCHED_IMPL_HXX
133
134 // Do some checking that we have a consistent universe.
135
136 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL
137 # ifndef CYGIMP_THREAD_PRIORITY
138 #  error Priority inversion protocols will not work without priorities!!!
139 # endif
140 #endif
141
142 // -------------------------------------------------------------------------
143 // Scheduler class. This is the public scheduler interface seen by the
144 // rest of the kernel.
145
146 class Cyg_Scheduler
147     : public Cyg_Scheduler_Implementation
148 {
149     friend class Cyg_Thread;
150     
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.
154     
155     static void             unlock_inner(cyg_uint32 new_lock = 0);
156     
157 public:
158
159     CYGDBG_DEFINE_CHECK_THIS
160
161     // The following API functions are common to all scheduler
162     // implementations.
163
164     // claim the preemption lock
165     static void             lock();         
166
167     // release the preemption lock and possibly reschedule
168     static void             unlock();
169
170     // release and reclaim the lock atomically, keeping the old
171     // value on restart
172     static void             reschedule();
173
174     // decrement the lock but also look for a reschedule opportunity
175     static void             unlock_reschedule();
176
177     // release the preemption lock without rescheduling
178     static void             unlock_simple();
179
180     // perform thread startup housekeeping
181     void thread_entry( Cyg_Thread *thread );
182     
183     // Start execution of the scheduler
184     static void start() CYGBLD_ATTRIB_NORET;
185
186     // Start execution of the scheduler on the current CPU
187     static void start_cpu() CYGBLD_ATTRIB_NORET;    
188     
189     // The only  scheduler instance should be this one...
190     static Cyg_Scheduler scheduler CYGBLD_ANNOTATE_VARIABLE_SCHED;
191
192 };
193
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.
199
200 class Cyg_SchedThread
201     : public Cyg_SchedThread_Implementation
202 {
203     friend class Cyg_ThreadQueue_Implementation;
204     friend class Cyg_Scheduler_Implementation;
205     friend class Cyg_Scheduler;
206     
207     Cyg_ThreadQueue     *queue;
208
209
210 public:
211
212     Cyg_SchedThread(Cyg_Thread *thread, CYG_ADDRWORD sched_info);
213
214     // Return current queue pointer
215
216     Cyg_ThreadQueue     *get_current_queue();
217     
218     // Remove this thread from current queue
219     void remove();
220
221 #ifdef CYGSEM_KERNEL_SCHED_ASR_SUPPORT
222
223     // 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.
228
229 private:
230
231     volatile cyg_ucount32       asr_inhibit;    // If > 0, blocks calls to ASRs
232
233     volatile cyg_bool           asr_pending;    // If true, this thread's ASR should be called.
234
235 #ifdef CYGSEM_KERNEL_SCHED_ASR_GLOBAL
236     static
237 #endif    
238     Cyg_ASR             *asr;            // ASR function
239 #ifdef CYGSEM_KERNEL_SCHED_ASR_DATA_GLOBAL
240     static
241 #endif    
242     CYG_ADDRWORD        asr_data;       // ASR data pointer
243
244     // Default ASR function
245     static void         asr_default(CYG_ADDRWORD data);
246
247 public:
248
249     // Public interface to ASR mechanism
250
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; }
255
256     // Set and get pending flag. The flag is only cleared when the
257     // ASR is called.
258     inline void set_asr_pending() { asr_pending = true; }
259     inline cyg_bool get_asr_pending() { return asr_pending; }
260
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);
264
265     // Clear the ASR function back to the default.
266     void clear_asr();
267
268 #else
269
270 public:
271     
272     // Even when we do not have ASRs enabled, we keep these functions
273     // available. This avoids excessive ifdefs in the rest of the
274     // kernel code.
275     inline void set_asr_inhibit() { }
276     inline void clear_asr_inhibit() { }
277     
278 #endif    
279     
280 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL
281
282 private:
283
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
287     // wakeup.
288     
289     cyg_count32         mutex_count;
290
291 protected:
292     // These are implementation functions that are common to all protocols.
293         
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 );
297
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);
301
302     // Lose priority inheritance
303     void clear_inherited_priority();
304     
305 public:    
306     // Count and uncount the number of mutexes held by
307     // this thread.
308     void count_mutex() { mutex_count++; };
309     void uncount_mutex() { mutex_count--; };
310
311 #if defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE)
312     
313 protected:    
314
315     // The simple priority inversion protocols simply needs
316     // somewhere to store the base priority of the current thread.
317     
318     cyg_priority        original_priority;      // our original priority
319
320     cyg_bool            priority_inherited;     // have we inherited?
321
322 #endif
323
324 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
325     
326 public:
327
328     // Inherit the priority of the provided thread if it
329     // has higher priority than this.
330     void inherit_priority( Cyg_Thread *thread);
331
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);
335
336     // Lose priority inheritance
337     void disinherit_priority();
338     
339 #endif
340
341 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
342
343 public:
344
345     // Set the priority of this thread to the given ceiling.
346     void set_priority_ceiling( cyg_priority pri );
347
348     // Clear the ceiling, if necessary.
349     void clear_priority_ceiling();
350     
351 #endif    
352
353 #endif
354     
355 };
356
357 // -------------------------------------------------------------------------
358 // Simple inline accessor functions
359
360 inline Cyg_Thread *Cyg_Scheduler_Base::get_current_thread()
361 {
362     return current_thread[CYG_KERNEL_CPU_THIS()];
363 }
364
365 inline void Cyg_Scheduler_Base::set_current_thread(Cyg_Thread *thread )
366 {
367     current_thread[CYG_KERNEL_CPU_THIS()] = thread;
368 }
369
370 inline void Cyg_Scheduler_Base::set_current_thread(Cyg_Thread *thread,
371                                                    HAL_SMP_CPU_TYPE cpu)
372 {
373     current_thread[cpu] = thread;
374 }
375
376 inline cyg_bool Cyg_Scheduler_Base::get_need_reschedule()
377 {
378     return need_reschedule[CYG_KERNEL_CPU_THIS()];
379 }
380
381 inline void Cyg_Scheduler_Base::set_need_reschedule()
382 {
383     need_reschedule[CYG_KERNEL_CPU_THIS()] = true;
384 }
385
386 inline void Cyg_Scheduler_Base::set_need_reschedule(Cyg_Thread *thread)
387 {
388     need_reschedule[CYG_KERNEL_CPU_THIS()] = true;
389 }
390
391 inline void Cyg_Scheduler_Base::clear_need_reschedule()
392 {
393     need_reschedule[CYG_KERNEL_CPU_THIS()] = false;
394 }
395
396 inline cyg_ucount32 Cyg_Scheduler_Base::get_sched_lock()
397 {
398     return Cyg_Scheduler_SchedLock::get_sched_lock();
399 }
400
401 // Return current number of thread switches
402 inline cyg_ucount32 Cyg_Scheduler_Base::get_thread_switches()
403 {
404     return thread_switches[CYG_KERNEL_CPU_THIS()];
405 }
406
407 // Return current queue pointer
408 inline Cyg_ThreadQueue *Cyg_SchedThread::get_current_queue()
409 {
410     return queue;
411 }
412
413 // -------------------------------------------------------------------------
414 #endif // ifndef __SCHED_HXX__
415 // EOF sched.hxx