1 |
1254 |
phoenix |
#ifndef CYGONCE_KERNEL_LOTTERY_HXX
|
2 |
|
|
#define CYGONCE_KERNEL_LOTTERY_HXX
|
3 |
|
|
|
4 |
|
|
//==========================================================================
|
5 |
|
|
//
|
6 |
|
|
// lottery.hxx
|
7 |
|
|
//
|
8 |
|
|
// Lottery scheduler class declarations
|
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-10
|
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
|
54 |
|
|
// it owns.
|
55 |
|
|
// Usage: Included according to configuration by
|
56 |
|
|
//
|
57 |
|
|
//
|
58 |
|
|
//####DESCRIPTIONEND####
|
59 |
|
|
//
|
60 |
|
|
//==========================================================================
|
61 |
|
|
|
62 |
|
|
#include
|
63 |
|
|
|
64 |
|
|
// -------------------------------------------------------------------------
|
65 |
|
|
// Customize the scheduler
|
66 |
|
|
|
67 |
|
|
#define CYGIMP_THREAD_PRIORITY 1 // Threads have changable priorities
|
68 |
|
|
|
69 |
|
|
#define CYG_THREAD_MIN_PRIORITY 1
|
70 |
|
|
#define CYG_THREAD_MAX_PRIORITY 0x7FFFFFFF
|
71 |
|
|
|
72 |
|
|
// set default scheduling info value for thread constructors.
|
73 |
|
|
#define CYG_SCHED_DEFAULT_INFO CYG_THREAD_MAX_PRIORITY
|
74 |
|
|
|
75 |
|
|
#error Lottery Scheduler not yet complete, do not use!!!
|
76 |
|
|
|
77 |
|
|
// -------------------------------------------------------------------------
|
78 |
|
|
// Thread queue implementation.
|
79 |
|
|
// This class provides the (scheduler specific) implementation of the
|
80 |
|
|
// thread queue class.
|
81 |
|
|
|
82 |
|
|
class Cyg_ThreadQueue_Implementation
|
83 |
|
|
{
|
84 |
|
|
friend class Cyg_Scheduler_Implementation;
|
85 |
|
|
friend class Cyg_SchedThread_Implementation;
|
86 |
|
|
|
87 |
|
|
Cyg_Thread *queue;
|
88 |
|
|
|
89 |
|
|
protected:
|
90 |
|
|
|
91 |
|
|
// API used by Cyg_ThreadQueue
|
92 |
|
|
|
93 |
|
|
// Add thread to queue
|
94 |
|
|
void enqueue(Cyg_Thread *thread);
|
95 |
|
|
|
96 |
|
|
// return first thread on queue
|
97 |
|
|
Cyg_Thread *highpri();
|
98 |
|
|
|
99 |
|
|
// remove first thread on queue
|
100 |
|
|
Cyg_Thread *dequeue();
|
101 |
|
|
|
102 |
|
|
// remove specified thread from queue
|
103 |
|
|
void remove(Cyg_Thread *thread);
|
104 |
|
|
|
105 |
|
|
// test if queue is empty
|
106 |
|
|
cyg_bool empty();
|
107 |
|
|
|
108 |
|
|
void rotate(); // Rotate the queue
|
109 |
|
|
};
|
110 |
|
|
|
111 |
|
|
inline cyg_bool Cyg_ThreadQueue_Implementation::empty()
|
112 |
|
|
{
|
113 |
|
|
return queue == NULL;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
// -------------------------------------------------------------------------
|
117 |
|
|
// This class contains the implementation details of the scheduler, and
|
118 |
|
|
// provides a standard API for accessing it.
|
119 |
|
|
|
120 |
|
|
class Cyg_Scheduler_Implementation
|
121 |
|
|
: public Cyg_Scheduler_Base
|
122 |
|
|
{
|
123 |
|
|
friend class Cyg_ThreadQueue_Implementation;
|
124 |
|
|
friend class Cyg_SchedThread_Implementation;
|
125 |
|
|
|
126 |
|
|
// All runnable threads are kept on a single run queue
|
127 |
|
|
// in MRU order.
|
128 |
|
|
Cyg_ThreadQueue_Implementation run_queue;
|
129 |
|
|
|
130 |
|
|
cyg_uint32 rand_seed;
|
131 |
|
|
|
132 |
|
|
cyg_int32 total_tickets;
|
133 |
|
|
|
134 |
|
|
protected:
|
135 |
|
|
|
136 |
|
|
Cyg_Scheduler_Implementation(); // Constructor
|
137 |
|
|
|
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.
|
141 |
|
|
|
142 |
|
|
// choose a new thread
|
143 |
|
|
Cyg_Thread *schedule();
|
144 |
|
|
|
145 |
|
|
// make thread schedulable
|
146 |
|
|
void add_thread(Cyg_Thread *thread);
|
147 |
|
|
|
148 |
|
|
// make thread un-schedulable
|
149 |
|
|
void rem_thread(Cyg_Thread *thread);
|
150 |
|
|
|
151 |
|
|
// register thread with scheduler
|
152 |
|
|
void register_thread(Cyg_Thread *thread);
|
153 |
|
|
|
154 |
|
|
// deregister thread
|
155 |
|
|
void deregister_thread(Cyg_Thread *thread);
|
156 |
|
|
|
157 |
|
|
// Test the given priority for uniqueness
|
158 |
|
|
cyg_bool unique( cyg_priority priority);
|
159 |
|
|
|
160 |
|
|
#ifdef CYGSEM_KERNEL_SCHED_TIMESLICE
|
161 |
|
|
|
162 |
|
|
// If timeslicing is enbled, define a scheduler
|
163 |
|
|
// entry point to do timeslicing. This will be
|
164 |
|
|
// called from the RTC DSR.
|
165 |
|
|
|
166 |
|
|
protected:
|
167 |
|
|
|
168 |
|
|
static cyg_count32 timeslice_count;
|
169 |
|
|
|
170 |
|
|
public:
|
171 |
|
|
void timeslice();
|
172 |
|
|
|
173 |
|
|
static void reset_timeslice_count();
|
174 |
|
|
|
175 |
|
|
#endif
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
};
|
179 |
|
|
|
180 |
|
|
// -------------------------------------------------------------------------
|
181 |
|
|
// Cyg_Scheduler_Implementation inlines
|
182 |
|
|
|
183 |
|
|
#ifdef CYGSEM_KERNEL_SCHED_TIMESLICE
|
184 |
|
|
|
185 |
|
|
inline void Cyg_Scheduler_Implementation::reset_timeslice_count()
|
186 |
|
|
{
|
187 |
|
|
timeslice_count = CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
#endif
|
191 |
|
|
|
192 |
|
|
// -------------------------------------------------------------------------
|
193 |
|
|
// Scheduler thread implementation.
|
194 |
|
|
// This class provides the implementation of the scheduler specific parts
|
195 |
|
|
// of each thread.
|
196 |
|
|
|
197 |
|
|
class Cyg_SchedThread_Implementation
|
198 |
|
|
{
|
199 |
|
|
friend class Cyg_Scheduler_Implementation;
|
200 |
|
|
friend class Cyg_ThreadQueue_Implementation;
|
201 |
|
|
|
202 |
|
|
Cyg_Thread *next; // next thread in queue
|
203 |
|
|
Cyg_Thread *prev; // previous thread in queue
|
204 |
|
|
|
205 |
|
|
void insert( Cyg_Thread *thread ); // Insert thread in front of this
|
206 |
|
|
|
207 |
|
|
void remove(); // remove this from queue
|
208 |
|
|
|
209 |
|
|
protected:
|
210 |
|
|
|
211 |
|
|
cyg_priority priority; // current thread priority == tickets held
|
212 |
|
|
|
213 |
|
|
cyg_priority compensation_tickets; // sleep compensation
|
214 |
|
|
|
215 |
|
|
Cyg_SchedThread_Implementation(CYG_ADDRWORD sched_info);
|
216 |
|
|
|
217 |
|
|
void yield(); // Yield CPU to next thread
|
218 |
|
|
|
219 |
|
|
};
|
220 |
|
|
|
221 |
|
|
// -------------------------------------------------------------------------
|
222 |
|
|
#endif // ifndef CYGONCE_KERNEL_LOTTERY_HXX
|
223 |
|
|
// EOF lottery.hxx
|