1 #ifndef CYGONCE_KERNEL_LOTTERY_HXX
2 #define CYGONCE_KERNEL_LOTTERY_HXX
4 //==========================================================================
8 // Lottery scheduler class declarations
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 lottery scheduler implementation
50 // Description: The classes defined here are used as base classes
51 // by the common classes that define schedulers and thread
52 // things. A lottery scheduler provides each thread with a
53 // share of the processor based on the number of tickets that
55 // Usage: Included according to configuration by
56 // <cyg/kernel/sched.hxx>
58 //####DESCRIPTIONEND####
60 //==========================================================================
62 #include <cyg/kernel/ktypes.h>
64 // -------------------------------------------------------------------------
65 // Customize the scheduler
67 #define CYGIMP_THREAD_PRIORITY 1 // Threads have changable priorities
69 #define CYG_THREAD_MIN_PRIORITY 1
70 #define CYG_THREAD_MAX_PRIORITY 0x7FFFFFFF
72 // set default scheduling info value for thread constructors.
73 #define CYG_SCHED_DEFAULT_INFO CYG_THREAD_MAX_PRIORITY
75 #error Lottery Scheduler not yet complete, do not use!!!
77 // -------------------------------------------------------------------------
78 // Thread queue implementation.
79 // This class provides the (scheduler specific) implementation of the
80 // thread queue class.
82 class Cyg_ThreadQueue_Implementation
84 friend class Cyg_Scheduler_Implementation;
85 friend class Cyg_SchedThread_Implementation;
91 // API used by Cyg_ThreadQueue
93 // Add thread to queue
94 void enqueue(Cyg_Thread *thread);
96 // return first thread on queue
97 Cyg_Thread *highpri();
99 // remove first thread on queue
100 Cyg_Thread *dequeue();
102 // remove specified thread from queue
103 void remove(Cyg_Thread *thread);
105 // test if queue is empty
108 void rotate(); // Rotate the queue
111 inline cyg_bool Cyg_ThreadQueue_Implementation::empty()
113 return queue == NULL;
116 // -------------------------------------------------------------------------
117 // This class contains the implementation details of the scheduler, and
118 // provides a standard API for accessing it.
120 class Cyg_Scheduler_Implementation
121 : public Cyg_Scheduler_Base
123 friend class Cyg_ThreadQueue_Implementation;
124 friend class Cyg_SchedThread_Implementation;
126 // All runnable threads are kept on a single run queue
128 Cyg_ThreadQueue_Implementation run_queue;
130 cyg_uint32 rand_seed;
132 cyg_int32 total_tickets;
136 Cyg_Scheduler_Implementation(); // Constructor
138 // The following functions provide the scheduler implementation
139 // interface to the Cyg_Scheduler class. These are protected
140 // so that only the scheduler can call them.
142 // choose a new thread
143 Cyg_Thread *schedule();
145 // make thread schedulable
146 void add_thread(Cyg_Thread *thread);
148 // make thread un-schedulable
149 void rem_thread(Cyg_Thread *thread);
151 // register thread with scheduler
152 void register_thread(Cyg_Thread *thread);
155 void deregister_thread(Cyg_Thread *thread);
157 // Test the given priority for uniqueness
158 cyg_bool unique( cyg_priority priority);
160 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE
162 // If timeslicing is enbled, define a scheduler
163 // entry point to do timeslicing. This will be
164 // called from the RTC DSR.
168 static cyg_count32 timeslice_count;
173 static void reset_timeslice_count();
180 // -------------------------------------------------------------------------
181 // Cyg_Scheduler_Implementation inlines
183 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE
185 inline void Cyg_Scheduler_Implementation::reset_timeslice_count()
187 timeslice_count = CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS;
192 // -------------------------------------------------------------------------
193 // Scheduler thread implementation.
194 // This class provides the implementation of the scheduler specific parts
197 class Cyg_SchedThread_Implementation
199 friend class Cyg_Scheduler_Implementation;
200 friend class Cyg_ThreadQueue_Implementation;
202 Cyg_Thread *next; // next thread in queue
203 Cyg_Thread *prev; // previous thread in queue
205 void insert( Cyg_Thread *thread ); // Insert thread in front of this
207 void remove(); // remove this from queue
211 cyg_priority priority; // current thread priority == tickets held
213 cyg_priority compensation_tickets; // sleep compensation
215 Cyg_SchedThread_Implementation(CYG_ADDRWORD sched_info);
217 void yield(); // Yield CPU to next thread
221 // -------------------------------------------------------------------------
222 #endif // ifndef CYGONCE_KERNEL_LOTTERY_HXX