|
0
|
1 #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 //####COPYRIGHTBEGIN#### |
|
|
12 // |
|
|
13 // ------------------------------------------- |
|
|
14 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
15 // Version 1.0 (the "License"); you may not use this file except in |
|
|
16 // compliance with the License. You may obtain a copy of the License at |
|
|
17 // http://sourceware.cygnus.com/ecos |
|
|
18 // |
|
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
21 // License for the specific language governing rights and limitations under |
|
|
22 // the License. |
|
|
23 // |
|
|
24 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
25 // September 30, 1998. |
|
|
26 // |
|
|
27 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
28 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
29 // ------------------------------------------- |
|
|
30 // |
|
|
31 //####COPYRIGHTEND#### |
|
|
32 //========================================================================== |
|
|
33 //#####DESCRIPTIONBEGIN#### |
|
|
34 // |
|
|
35 // Author(s): nickg |
|
|
36 // Contributors: nickg |
|
|
37 // Date: 1997-09-10 |
|
|
38 // Purpose: Define lottery scheduler implementation |
|
|
39 // Description: The classes defined here are used as base classes |
|
|
40 // by the common classes that define schedulers and thread |
|
|
41 // things. A lottery scheduler provides each thread with a |
|
|
42 // share of the processor based on the number of tickets that |
|
|
43 // it owns. |
|
|
44 // Usage: Included according to configuration by |
|
|
45 // <cyg/kernel/sched.hxx> |
|
|
46 // |
|
|
47 //####DESCRIPTIONEND#### |
|
|
48 // |
|
|
49 //========================================================================== |
|
|
50 |
|
|
51 #include <cyg/kernel/ktypes.h> |
|
|
52 |
|
|
53 // ------------------------------------------------------------------------- |
|
|
54 // Customize the scheduler |
|
|
55 |
|
|
56 #define CYGIMP_THREAD_PRIORITY 1 // Threads have changable priorities |
|
|
57 |
|
|
58 #define CYG_THREAD_MIN_PRIORITY 1 |
|
|
59 #define CYG_THREAD_MAX_PRIORITY 0x7FFFFFFF |
|
|
60 |
|
|
61 // set default scheduling info value for thread constructors. |
|
|
62 #define CYG_SCHED_DEFAULT_INFO CYG_THREAD_MAX_PRIORITY |
|
|
63 |
|
|
64 #error Lottery Scheduler not yet complete, do not use!!! |
|
|
65 |
|
|
66 // ------------------------------------------------------------------------- |
|
|
67 // Thread queue implementation. |
|
|
68 // This class provides the (scheduler specific) implementation of the |
|
|
69 // thread queue class. |
|
|
70 |
|
|
71 class Cyg_ThreadQueue_Implementation |
|
|
72 { |
|
|
73 friend class Cyg_Scheduler_Implementation; |
|
|
74 friend class Cyg_SchedThread_Implementation; |
|
|
75 |
|
|
76 Cyg_Thread *queue; |
|
|
77 |
|
|
78 protected: |
|
|
79 |
|
|
80 // API used by Cyg_ThreadQueue |
|
|
81 |
|
|
82 // Add thread to queue |
|
|
83 void enqueue(Cyg_Thread *thread); |
|
|
84 |
|
|
85 // return first thread on queue |
|
|
86 Cyg_Thread *highpri(); |
|
|
87 |
|
|
88 // remove first thread on queue |
|
|
89 Cyg_Thread *dequeue(); |
|
|
90 |
|
|
91 // remove specified thread from queue |
|
|
92 void remove(Cyg_Thread *thread); |
|
|
93 |
|
|
94 // test if queue is empty |
|
|
95 cyg_bool empty(); |
|
|
96 |
|
|
97 void rotate(); // Rotate the queue |
|
|
98 }; |
|
|
99 |
|
|
100 inline cyg_bool Cyg_ThreadQueue_Implementation::empty() |
|
|
101 { |
|
|
102 return queue == NULL; |
|
|
103 } |
|
|
104 |
|
|
105 // ------------------------------------------------------------------------- |
|
|
106 // This class contains the implementation details of the scheduler, and |
|
|
107 // provides a standard API for accessing it. |
|
|
108 |
|
|
109 class Cyg_Scheduler_Implementation |
|
|
110 : public Cyg_Scheduler_Base |
|
|
111 { |
|
|
112 friend class Cyg_ThreadQueue_Implementation; |
|
|
113 friend class Cyg_SchedThread_Implementation; |
|
|
114 |
|
|
115 // All runnable threads are kept on a single run queue |
|
|
116 // in MRU order. |
|
|
117 Cyg_ThreadQueue_Implementation run_queue; |
|
|
118 |
|
|
119 cyg_uint32 rand_seed; |
|
|
120 |
|
|
121 cyg_int32 total_tickets; |
|
|
122 |
|
|
123 protected: |
|
|
124 |
|
|
125 Cyg_Scheduler_Implementation(); // Constructor |
|
|
126 |
|
|
127 // The following functions provide the scheduler implementation |
|
|
128 // interface to the Cyg_Scheduler class. These are protected |
|
|
129 // so that only the scheduler can call them. |
|
|
130 |
|
|
131 // choose a new thread |
|
|
132 Cyg_Thread *schedule(); |
|
|
133 |
|
|
134 // make thread schedulable |
|
|
135 void add_thread(Cyg_Thread *thread); |
|
|
136 |
|
|
137 // make thread un-schedulable |
|
|
138 void rem_thread(Cyg_Thread *thread); |
|
|
139 |
|
|
140 // register thread with scheduler |
|
|
141 void register_thread(Cyg_Thread *thread); |
|
|
142 |
|
|
143 // deregister thread |
|
|
144 void deregister_thread(Cyg_Thread *thread); |
|
|
145 |
|
|
146 // Test the given priority for uniqueness |
|
|
147 cyg_bool unique( cyg_priority priority); |
|
|
148 |
|
|
149 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE |
|
|
150 |
|
|
151 // If timeslicing is enbled, define a scheduler |
|
|
152 // entry point to do timeslicing. This will be |
|
|
153 // called from the RTC DSR. |
|
|
154 |
|
|
155 protected: |
|
|
156 |
|
|
157 static cyg_count32 timeslice_count; |
|
|
158 |
|
|
159 public: |
|
|
160 void timeslice(); |
|
|
161 |
|
|
162 static void reset_timeslice_count(); |
|
|
163 |
|
|
164 #endif |
|
|
165 |
|
|
166 |
|
|
167 }; |
|
|
168 |
|
|
169 // ------------------------------------------------------------------------- |
|
|
170 // Cyg_Scheduler_Implementation inlines |
|
|
171 |
|
|
172 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE |
|
|
173 |
|
|
174 inline void Cyg_Scheduler_Implementation::reset_timeslice_count() |
|
|
175 { |
|
|
176 timeslice_count = CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS; |
|
|
177 } |
|
|
178 |
|
|
179 #endif |
|
|
180 |
|
|
181 // ------------------------------------------------------------------------- |
|
|
182 // Scheduler thread implementation. |
|
|
183 // This class provides the implementation of the scheduler specific parts |
|
|
184 // of each thread. |
|
|
185 |
|
|
186 class Cyg_SchedThread_Implementation |
|
|
187 { |
|
|
188 friend class Cyg_Scheduler_Implementation; |
|
|
189 friend class Cyg_ThreadQueue_Implementation; |
|
|
190 |
|
|
191 Cyg_Thread *next; // next thread in queue |
|
|
192 Cyg_Thread *prev; // previous thread in queue |
|
|
193 |
|
|
194 void insert( Cyg_Thread *thread ); // Insert thread in front of this |
|
|
195 |
|
|
196 void remove(); // remove this from queue |
|
|
197 |
|
|
198 protected: |
|
|
199 |
|
|
200 cyg_priority priority; // current thread priority == tickets held |
|
|
201 |
|
|
202 cyg_priority compensation_tickets; // sleep compensation |
|
|
203 |
|
|
204 Cyg_SchedThread_Implementation(CYG_ADDRWORD sched_info); |
|
|
205 |
|
|
206 void yield(); // Yield CPU to next thread |
|
|
207 |
|
|
208 }; |
|
|
209 |
|
|
210 // ------------------------------------------------------------------------- |
|
|
211 #endif // ifndef CYGONCE_KERNEL_LOTTERY_HXX |
|
|
212 // EOF lottery.hxx |