1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// sched/bitmap.cxx
|
4 |
|
|
//
|
5 |
|
|
// Bitmap scheduler class implementation
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2010 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): nickg
|
43 |
|
|
// Contributors: nickg
|
44 |
|
|
// Date: 1997-09-16
|
45 |
|
|
// Purpose: Bitmap scheduler class implementation
|
46 |
|
|
// Description: This file contains the implementations of
|
47 |
|
|
// Cyg_Scheduler_Implementation and Cyg_SchedThread_Implementation.
|
48 |
|
|
//
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//==========================================================================
|
53 |
|
|
|
54 |
|
|
#include <pkgconf/kernel.h>
|
55 |
|
|
|
56 |
|
|
#include <cyg/kernel/ktypes.h> // base kernel types
|
57 |
|
|
#include <cyg/infra/cyg_trac.h> // tracing macros
|
58 |
|
|
#include <cyg/infra/cyg_ass.h> // assertion macros
|
59 |
|
|
|
60 |
|
|
#include <cyg/kernel/sched.hxx> // our header
|
61 |
|
|
|
62 |
|
|
#include <cyg/hal/hal_arch.h> // Architecture specific definitions
|
63 |
|
|
|
64 |
|
|
#include <cyg/kernel/thread.inl> // thread inlines
|
65 |
|
|
#include <cyg/kernel/sched.inl> // scheduler inlines
|
66 |
|
|
|
67 |
|
|
#ifdef CYGSEM_KERNEL_SCHED_BITMAP
|
68 |
|
|
|
69 |
|
|
//==========================================================================
|
70 |
|
|
// Cyg_Scheduler_Implementation class members
|
71 |
|
|
|
72 |
|
|
// -------------------------------------------------------------------------
|
73 |
|
|
// Constructor.
|
74 |
|
|
|
75 |
|
|
Cyg_Scheduler_Implementation::Cyg_Scheduler_Implementation()
|
76 |
|
|
{
|
77 |
|
|
CYG_REPORT_FUNCTION();
|
78 |
|
|
|
79 |
|
|
// At present we cannot init run_queue here because the absence of
|
80 |
|
|
// ordering of static constructors means that we could do this
|
81 |
|
|
// after the static idle thread has been created. (Guess how I
|
82 |
|
|
// found this out!)
|
83 |
|
|
// run_queue = 0;
|
84 |
|
|
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
// -------------------------------------------------------------------------
|
88 |
|
|
// Choose the best thread to run next
|
89 |
|
|
|
90 |
|
|
Cyg_Thread *Cyg_Scheduler_Implementation::schedule()
|
91 |
|
|
{
|
92 |
|
|
CYG_REPORT_FUNCTION();
|
93 |
|
|
|
94 |
|
|
// The run queue may _never_ be empty, there is always
|
95 |
|
|
// an idle thread at the lowest priority.
|
96 |
|
|
|
97 |
|
|
CYG_ASSERT(run_queue != 0, "Run queue empty");
|
98 |
|
|
|
99 |
|
|
cyg_uint32 index;
|
100 |
|
|
|
101 |
|
|
HAL_LSBIT_INDEX(index, run_queue);
|
102 |
|
|
|
103 |
|
|
CYG_INSTRUMENT_BITMAP(SCHEDULE, thread_table[index], index);
|
104 |
|
|
|
105 |
|
|
return thread_table[index];
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
// -------------------------------------------------------------------------
|
109 |
|
|
|
110 |
|
|
void Cyg_Scheduler_Implementation::add_thread(Cyg_Thread *thread)
|
111 |
|
|
{
|
112 |
|
|
CYG_REPORT_FUNCTION();
|
113 |
|
|
|
114 |
|
|
CYG_INSTRUMENT_BITMAP(ADD, thread, thread->priority);
|
115 |
|
|
|
116 |
|
|
CYG_ASSERT((CYG_THREAD_MIN_PRIORITY >= thread->priority)
|
117 |
|
|
&& (CYG_THREAD_MAX_PRIORITY <= thread->priority),
|
118 |
|
|
"Priority out of range!");
|
119 |
|
|
|
120 |
|
|
CYG_ASSERT( thread_table[thread->priority] == NULL ||
|
121 |
|
|
thread_table[thread->priority] == thread,
|
122 |
|
|
"Duplicate thread priorities" );
|
123 |
|
|
|
124 |
|
|
CYG_ASSERT( (run_queue & (1<<thread->priority)) == 0,
|
125 |
|
|
"Run queue bit already set" );
|
126 |
|
|
|
127 |
|
|
// If the thread is on some other queue, remove it
|
128 |
|
|
// here.
|
129 |
|
|
if( thread->queue != NULL )
|
130 |
|
|
{
|
131 |
|
|
thread->queue->remove(thread);
|
132 |
|
|
thread->queue = NULL;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
run_queue |= 1<<thread->priority;
|
136 |
|
|
|
137 |
|
|
// If the new thread is higher priority than the
|
138 |
|
|
// current thread, request a reschedule.
|
139 |
|
|
|
140 |
|
|
if( thread->priority < Cyg_Scheduler::get_current_thread()->priority )
|
141 |
|
|
set_need_reschedule();
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
// -------------------------------------------------------------------------
|
145 |
|
|
|
146 |
|
|
void Cyg_Scheduler_Implementation::rem_thread(Cyg_Thread *thread)
|
147 |
|
|
{
|
148 |
|
|
CYG_REPORT_FUNCTION();
|
149 |
|
|
|
150 |
|
|
CYG_INSTRUMENT_BITMAP(REM, thread, thread->priority);
|
151 |
|
|
|
152 |
|
|
CYG_ASSERT( thread_table[thread->priority] == thread,
|
153 |
|
|
"Invalid thread priority" );
|
154 |
|
|
|
155 |
|
|
CYG_ASSERT( (run_queue & (1<<thread->priority)) != 0,
|
156 |
|
|
"Run queue bit not set" );
|
157 |
|
|
|
158 |
|
|
run_queue &= ~(1<<thread->priority);
|
159 |
|
|
|
160 |
|
|
if( thread == Cyg_Scheduler::get_current_thread() )
|
161 |
|
|
set_need_reschedule();
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
// -------------------------------------------------------------------------
|
165 |
|
|
// Set up initial idle thread
|
166 |
|
|
|
167 |
|
|
void Cyg_Scheduler_Implementation::set_idle_thread( Cyg_Thread *thread, HAL_SMP_CPU_TYPE cpu )
|
168 |
|
|
{
|
169 |
|
|
CYG_REPORT_FUNCTION();
|
170 |
|
|
|
171 |
|
|
// Make the thread the current thread for this CPU.
|
172 |
|
|
|
173 |
|
|
current_thread[cpu] = thread;
|
174 |
|
|
|
175 |
|
|
// This will insert the thread in the run queues and make it
|
176 |
|
|
// available to execute.
|
177 |
|
|
thread->resume();
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
// -------------------------------------------------------------------------
|
181 |
|
|
// register thread with scheduler
|
182 |
|
|
|
183 |
|
|
void Cyg_Scheduler_Implementation::register_thread(Cyg_Thread *thread)
|
184 |
|
|
{
|
185 |
|
|
CYG_REPORT_FUNCTION();
|
186 |
|
|
|
187 |
|
|
thread_table[thread->priority] = thread;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
// -------------------------------------------------------------------------
|
191 |
|
|
|
192 |
|
|
// deregister thread
|
193 |
|
|
void Cyg_Scheduler_Implementation::deregister_thread(Cyg_Thread *thread)
|
194 |
|
|
{
|
195 |
|
|
CYG_REPORT_FUNCTION();
|
196 |
|
|
|
197 |
|
|
thread_table[thread->priority] = NULL;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
// -------------------------------------------------------------------------
|
201 |
|
|
// Test the given priority for uniqueness
|
202 |
|
|
|
203 |
|
|
cyg_bool Cyg_Scheduler_Implementation::unique( cyg_priority priority)
|
204 |
|
|
{
|
205 |
|
|
CYG_REPORT_FUNCTION();
|
206 |
|
|
|
207 |
|
|
return thread_table[priority] == NULL;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
//==========================================================================
|
212 |
|
|
// Cyg_Cyg_SchedThread_Implementation class members
|
213 |
|
|
|
214 |
|
|
Cyg_SchedThread_Implementation::Cyg_SchedThread_Implementation
|
215 |
|
|
(
|
216 |
|
|
CYG_ADDRWORD sched_info
|
217 |
|
|
)
|
218 |
|
|
{
|
219 |
|
|
CYG_REPORT_FUNCTION();
|
220 |
|
|
|
221 |
|
|
#if 1
|
222 |
|
|
// Assign this thread's priority to the supplied sched_info
|
223 |
|
|
// or the next highest priority available.
|
224 |
|
|
|
225 |
|
|
priority = cyg_priority(sched_info);
|
226 |
|
|
|
227 |
|
|
while( !Cyg_Scheduler::scheduler.unique(priority) )
|
228 |
|
|
priority++;
|
229 |
|
|
|
230 |
|
|
#else
|
231 |
|
|
// Assign initial priorities to threads in descending order of
|
232 |
|
|
// creation.
|
233 |
|
|
|
234 |
|
|
static cyg_priority init_priority = 0;
|
235 |
|
|
|
236 |
|
|
priority = init_priority++;
|
237 |
|
|
#endif
|
238 |
|
|
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
// -------------------------------------------------------------------------
|
242 |
|
|
|
243 |
|
|
void Cyg_SchedThread_Implementation::yield()
|
244 |
|
|
{
|
245 |
|
|
CYG_REPORT_FUNCTION();
|
246 |
|
|
|
247 |
|
|
// We cannot yield in this scheduler
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
//==========================================================================
|
251 |
|
|
// Cyg_ThreadQueue_Implementation class members
|
252 |
|
|
|
253 |
|
|
Cyg_ThreadQueue_Implementation::Cyg_ThreadQueue_Implementation()
|
254 |
|
|
{
|
255 |
|
|
CYG_REPORT_FUNCTION();
|
256 |
|
|
|
257 |
|
|
wait_queue = 0; // empty queue
|
258 |
|
|
|
259 |
|
|
CYG_REPORT_RETURN();
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
void Cyg_ThreadQueue_Implementation::enqueue(Cyg_Thread *thread)
|
264 |
|
|
{
|
265 |
|
|
CYG_REPORT_FUNCTION();
|
266 |
|
|
|
267 |
|
|
CYG_INSTRUMENT_BITMAP(ENQUEUE, this, thread);
|
268 |
|
|
|
269 |
|
|
wait_queue |= 1<<thread->priority;
|
270 |
|
|
thread->queue = CYG_CLASSFROMBASE(Cyg_ThreadQueue,
|
271 |
|
|
Cyg_ThreadQueue_Implementation,
|
272 |
|
|
this);
|
273 |
|
|
}
|
274 |
|
|
|
275 |
|
|
// -------------------------------------------------------------------------
|
276 |
|
|
|
277 |
|
|
Cyg_Thread *Cyg_ThreadQueue_Implementation::dequeue()
|
278 |
|
|
{
|
279 |
|
|
CYG_REPORT_FUNCTION();
|
280 |
|
|
|
281 |
|
|
// Isolate ls bit in run_queue.
|
282 |
|
|
cyg_sched_bitmap next_thread = wait_queue & -wait_queue;
|
283 |
|
|
|
284 |
|
|
if( next_thread == 0 )
|
285 |
|
|
{
|
286 |
|
|
CYG_INSTRUMENT_BITMAP(DEQUEUE, this, NULL);
|
287 |
|
|
return NULL;
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
wait_queue &= ~next_thread;
|
291 |
|
|
|
292 |
|
|
cyg_uint32 index;
|
293 |
|
|
|
294 |
|
|
HAL_LSBIT_INDEX(index, next_thread);
|
295 |
|
|
|
296 |
|
|
Cyg_Thread *thread = Cyg_Scheduler::scheduler.thread_table[index];
|
297 |
|
|
|
298 |
|
|
CYG_INSTRUMENT_BITMAP(DEQUEUE, this, thread);
|
299 |
|
|
|
300 |
|
|
thread->queue = NULL;
|
301 |
|
|
|
302 |
|
|
return thread;
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
// -------------------------------------------------------------------------
|
306 |
|
|
|
307 |
|
|
Cyg_Thread *Cyg_ThreadQueue_Implementation::highpri()
|
308 |
|
|
{
|
309 |
|
|
CYG_REPORT_FUNCTION();
|
310 |
|
|
|
311 |
|
|
// Isolate ls bit in run_queue.
|
312 |
|
|
cyg_sched_bitmap next_thread = wait_queue & -wait_queue;
|
313 |
|
|
|
314 |
|
|
if( next_thread == 0 ) return NULL;
|
315 |
|
|
|
316 |
|
|
cyg_uint32 index;
|
317 |
|
|
|
318 |
|
|
HAL_LSBIT_INDEX(index, next_thread);
|
319 |
|
|
|
320 |
|
|
return Cyg_Scheduler::scheduler.thread_table[index];
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
// -------------------------------------------------------------------------
|
324 |
|
|
|
325 |
|
|
void Cyg_ThreadQueue_Implementation::remove(Cyg_Thread *thread)
|
326 |
|
|
{
|
327 |
|
|
CYG_REPORT_FUNCTION();
|
328 |
|
|
|
329 |
|
|
CYG_INSTRUMENT_BITMAP(REMOVE, this, thread);
|
330 |
|
|
|
331 |
|
|
wait_queue &= ~(1<<thread->priority);
|
332 |
|
|
thread->queue = NULL;
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
#endif
|
336 |
|
|
|
337 |
|
|
// -------------------------------------------------------------------------
|
338 |
|
|
// EOF sched/bitmap.cxx
|