|
0
|
1 #ifndef CYGONCE_KERNEL_MLQUEUE_HXX |
|
|
2 #define CYGONCE_KERNEL_MLQUEUE_HXX |
|
|
3 |
|
|
4 //========================================================================== |
|
|
5 // |
|
2
|
6 // mlqueue.hxx |
|
0
|
7 // |
|
2
|
8 // Multi-Level Queue scheduler class declarations |
|
0
|
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 |
|
2
|
28 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
29 // ------------------------------------------- |
|
|
30 // |
|
|
31 //####COPYRIGHTEND#### |
|
|
32 //========================================================================== |
|
|
33 //#####DESCRIPTIONBEGIN#### |
|
|
34 // |
|
2
|
35 // Author(s): nickg |
|
|
36 // Contributors: nickg |
|
|
37 // Date: 1997-09-10 |
|
|
38 // Purpose: Define multilevel queue scheduler implementation |
|
|
39 // Description: The classes defined here are used as base classes |
|
0
|
40 // by the common classes that define schedulers and thread |
|
|
41 // things. The MLQ scheduler in various configurations |
|
|
42 // provides standard FIFO, round-robin and single priority |
|
|
43 // schedulers. |
|
|
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 // The macro CYGNUM_KERNEL_SCHED_PRIORITIES contains the number of priorities |
|
|
55 // supported by the scheduler. |
|
|
56 |
|
|
57 #ifndef CYGNUM_KERNEL_SCHED_PRIORITIES |
|
|
58 #define CYGNUM_KERNEL_SCHED_PRIORITIES 32 // define a default |
|
|
59 #endif |
|
|
60 |
|
|
61 // set bitmap size |
|
|
62 #define CYGNUM_KERNEL_SCHED_BITMAP_SIZE CYGNUM_KERNEL_SCHED_PRIORITIES |
|
|
63 |
|
|
64 // ------------------------------------------------------------------------- |
|
|
65 // The macro CYGNUM_KERNEL_SCHED_BITMAP_SIZE contains the number of bits that the |
|
|
66 // scheduler bitmap should contain. It is derived from the number of prioirity |
|
|
67 // levels defined by the configuration. |
|
|
68 |
|
|
69 #if CYGNUM_KERNEL_SCHED_BITMAP_SIZE <= 8 |
|
|
70 typedef cyg_ucount8 cyg_sched_bitmap; |
|
|
71 #elif CYGNUM_KERNEL_SCHED_BITMAP_SIZE <= 16 |
|
|
72 typedef cyg_ucount16 cyg_sched_bitmap; |
|
|
73 #elif CYGNUM_KERNEL_SCHED_BITMAP_SIZE <= 32 |
|
|
74 typedef cyg_ucount32 cyg_sched_bitmap; |
|
|
75 #else |
|
|
76 #error Bitmaps greater than 32 bits not currently allowed |
|
|
77 #endif |
|
|
78 |
|
|
79 // ------------------------------------------------------------------------- |
|
|
80 // Customize the scheduler |
|
|
81 |
|
|
82 #define CYGIMP_THREAD_PRIORITY 1 // Threads have changable priorities |
|
|
83 #define CYG_SCHED_UNIQUE_PRIORITIES 0 // Threads don't have unique priorities |
|
|
84 |
|
|
85 #define CYG_THREAD_MIN_PRIORITY (CYGNUM_KERNEL_SCHED_PRIORITIES-1) |
|
|
86 #define CYG_THREAD_MAX_PRIORITY 0 |
|
|
87 |
|
|
88 // set default scheduling info value for thread constructors. |
|
|
89 #define CYG_SCHED_DEFAULT_INFO CYG_THREAD_MAX_PRIORITY |
|
|
90 |
|
|
91 // ------------------------------------------------------------------------- |
|
|
92 // Thread queue implementation. |
|
|
93 // This class provides the (scheduler specific) implementation of the |
|
|
94 // thread queue class. |
|
|
95 |
|
|
96 class Cyg_ThreadQueue_Implementation |
|
|
97 { |
|
|
98 friend class Cyg_Scheduler_Implementation; |
|
|
99 friend class Cyg_SchedThread_Implementation; |
|
|
100 |
|
|
101 Cyg_Thread *queue; |
|
|
102 |
|
|
103 protected: |
|
|
104 |
|
|
105 // API used by Cyg_ThreadQueue |
|
|
106 |
|
|
107 Cyg_ThreadQueue_Implementation(); // Constructor |
|
|
108 |
|
|
109 // Add thread to queue |
|
|
110 void enqueue(Cyg_Thread *thread); |
|
|
111 |
|
|
112 // return first thread on queue |
|
|
113 Cyg_Thread *highpri(); |
|
|
114 |
|
|
115 // remove first thread on queue |
|
|
116 Cyg_Thread *dequeue(); |
|
|
117 |
|
|
118 // remove specified thread from queue |
|
|
119 void remove(Cyg_Thread *thread); |
|
|
120 |
|
|
121 // test if queue is empty |
|
|
122 cyg_bool empty(); |
|
|
123 |
|
|
124 void rotate(); // Rotate the queue |
|
2
|
125 |
|
|
126 void to_head(Cyg_Thread *thread); |
|
|
127 |
|
0
|
128 }; |
|
|
129 |
|
|
130 inline cyg_bool Cyg_ThreadQueue_Implementation::empty() |
|
|
131 { |
|
|
132 return queue == NULL; |
|
|
133 } |
|
|
134 |
|
|
135 // ------------------------------------------------------------------------- |
|
|
136 // This class contains the implementation details of the scheduler, and |
|
|
137 // provides a standard API for accessing it. |
|
|
138 |
|
|
139 class Cyg_Scheduler_Implementation |
|
|
140 : public Cyg_Scheduler_Base |
|
|
141 { |
|
|
142 friend class Cyg_ThreadQueue_Implementation; |
|
|
143 friend class Cyg_SchedThread_Implementation; |
|
|
144 friend class Cyg_HardwareThread; |
|
|
145 |
|
|
146 // Mask of which run queues have ready threads |
|
|
147 cyg_sched_bitmap queue_map; |
|
|
148 |
|
|
149 // Each run queue is a double linked circular list of threads. |
|
|
150 // These pointers point to the head element of each list. |
|
|
151 Cyg_ThreadQueue_Implementation run_queue[CYGNUM_KERNEL_SCHED_PRIORITIES]; |
|
|
152 |
|
|
153 protected: |
|
|
154 |
|
|
155 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE |
|
|
156 |
|
|
157 // Timeslice counter. This is decremented on each |
|
|
158 // clock tick, and a timeslice is performed each |
|
|
159 // time it zeroes. |
|
|
160 |
|
|
161 static cyg_ucount32 timeslice_count; |
|
|
162 |
|
|
163 static void reset_timeslice_count(); |
|
|
164 |
|
|
165 #endif |
|
|
166 |
|
|
167 Cyg_Scheduler_Implementation(); // Constructor |
|
|
168 |
|
|
169 // The following functions provide the scheduler implementation |
|
|
170 // interface to the Cyg_Scheduler class. These are protected |
|
|
171 // so that only the scheduler can call them. |
|
|
172 |
|
|
173 // choose a new thread |
|
|
174 Cyg_Thread *schedule(); |
|
|
175 |
|
|
176 // make thread schedulable |
|
|
177 void add_thread(Cyg_Thread *thread); |
|
|
178 |
|
|
179 // make thread un-schedulable |
|
|
180 void rem_thread(Cyg_Thread *thread); |
|
|
181 |
|
|
182 // register thread with scheduler |
|
|
183 void register_thread(Cyg_Thread *thread); |
|
|
184 |
|
|
185 // deregister thread |
|
|
186 void deregister_thread(Cyg_Thread *thread); |
|
|
187 |
|
|
188 // Test the given priority for uniqueness |
|
|
189 cyg_bool unique( cyg_priority priority); |
|
|
190 |
|
|
191 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE |
|
|
192 |
|
|
193 // If timeslicing is enbled, define a scheduler |
|
|
194 // entry point to do timeslicing. This will be |
|
|
195 // called from the RTC DSR. |
|
|
196 public: |
|
|
197 void timeslice(); |
|
|
198 |
|
|
199 #endif |
|
|
200 |
|
|
201 }; |
|
|
202 |
|
|
203 // ------------------------------------------------------------------------- |
|
|
204 // Cyg_Scheduler_Implementation inlines |
|
|
205 |
|
|
206 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE |
|
|
207 |
|
|
208 inline void Cyg_Scheduler_Implementation::reset_timeslice_count() |
|
|
209 { |
|
|
210 timeslice_count = CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS; |
|
|
211 } |
|
|
212 |
|
|
213 #endif |
|
|
214 |
|
|
215 // ------------------------------------------------------------------------- |
|
|
216 // Scheduler thread implementation. |
|
|
217 // This class provides the implementation of the scheduler specific parts |
|
|
218 // of each thread. |
|
|
219 |
|
|
220 class Cyg_SchedThread_Implementation |
|
|
221 { |
|
|
222 friend class Cyg_Scheduler_Implementation; |
|
|
223 friend class Cyg_ThreadQueue_Implementation; |
|
|
224 |
|
|
225 Cyg_Thread *next; // next thread in queue |
|
|
226 Cyg_Thread *prev; // previous thread in queue |
|
|
227 |
|
|
228 void insert( Cyg_Thread *thread ); // Insert thread in front of this |
|
|
229 |
|
|
230 void remove(); // remove this from queue |
|
|
231 |
|
|
232 protected: |
|
|
233 |
|
|
234 cyg_priority priority; // current thread priority |
|
|
235 |
|
|
236 Cyg_SchedThread_Implementation(CYG_ADDRWORD sched_info); |
|
|
237 |
|
|
238 void yield(); // Yield CPU to next thread |
|
|
239 |
|
|
240 static void rotate_queue( cyg_priority pri ); |
|
|
241 // Rotate that run queue |
|
2
|
242 |
|
|
243 void to_queue_head( void ); // Move this thread to the head |
|
|
244 // of its queue (not necessarily |
|
|
245 // a scheduler queue) |
|
0
|
246 }; |
|
|
247 |
|
|
248 // ------------------------------------------------------------------------- |
|
|
249 #endif // ifndef CYGONCE_KERNEL_MLQUEUE_HXX |
|
|
250 // EOF mlqueue.hxx |