source: rtems/cpukit/score/include/rtems/score/schedulerimpl.h @ e239760

4.115
Last change on this file since e239760 was e239760, checked in by Sebastian Huber <sebastian.huber@…>, on 04/29/14 at 14:09:35

score: SMP_FATAL_SCHEDULER_WITHOUT_PROCESSORS

Avoid the SMP_FATAL_SCHEDULER_WITHOUT_PROCESSORS fatal error and make it
a run-time error in rtems_scheduler_ident() and _Scheduler_Get_by_id().

  • Property mode set to 100644
File size: 15.4 KB
Line 
1/**
2 * @file
3 *
4 * @brief Inlined Routines Associated with the Manipulation of the Scheduler
5 *
6 * This inline file contains all of the inlined routines associated with
7 * the manipulation of the scheduler.
8 */
9
10/*
11 *  Copyright (C) 2010 Gedare Bloom.
12 *  Copyright (C) 2011 On-Line Applications Research Corporation (OAR).
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#ifndef _RTEMS_SCORE_SCHEDULERIMPL_H
20#define _RTEMS_SCORE_SCHEDULERIMPL_H
21
22#include <rtems/score/scheduler.h>
23#include <rtems/score/cpusetimpl.h>
24#include <rtems/score/smpimpl.h>
25#include <rtems/score/threadimpl.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/**
32 * @addtogroup ScoreScheduler
33 */
34/**@{**/
35
36/**
37 *  @brief Initializes the scheduler to the policy chosen by the user.
38 *
39 *  This routine initializes the scheduler to the policy chosen by the user
40 *  through confdefs, or to the priority scheduler with ready chains by
41 *  default.
42 */
43void _Scheduler_Handler_initialization( void );
44
45RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_by_CPU_index(
46  uint32_t cpu_index
47)
48{
49#if defined(RTEMS_SMP)
50  return _Scheduler_Assignments[ cpu_index ].scheduler;
51#else
52  (void) cpu_index;
53
54  return &_Scheduler_Table[ 0 ];
55#endif
56}
57
58RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_by_CPU(
59  const Per_CPU_Control *cpu
60)
61{
62  uint32_t cpu_index = _Per_CPU_Get_index( cpu );
63
64  return _Scheduler_Get_by_CPU_index( cpu_index );
65}
66
67/**
68 * The preferred method to add a new scheduler is to define the jump table
69 * entries and add a case to the _Scheduler_Initialize routine.
70 *
71 * Generic scheduling implementations that rely on the ready queue only can
72 * be found in the _Scheduler_queue_XXX functions.
73 */
74
75/*
76 * Passing the Scheduler_Control* to these functions allows for multiple
77 * scheduler's to exist simultaneously, which could be useful on an SMP
78 * system.  Then remote Schedulers may be accessible.  How to protect such
79 * accesses remains an open problem.
80 */
81
82/**
83 * @brief Scheduler schedule.
84 *
85 * This kernel routine implements the scheduling decision logic for
86 * the scheduler. It does NOT dispatch.
87 *
88 * @param[in] the_thread The thread which state changed previously.
89 */
90RTEMS_INLINE_ROUTINE void _Scheduler_Schedule(
91  const Scheduler_Control *scheduler,
92  Thread_Control          *the_thread
93)
94{
95  ( *scheduler->Operations.schedule )( scheduler, the_thread );
96}
97
98/**
99 * @brief Scheduler yield with a particular thread.
100 *
101 * This routine is invoked when a thread wishes to voluntarily transfer control
102 * of the processor to another thread.
103 *
104 * @param[in] the_thread The yielding thread.
105 */
106RTEMS_INLINE_ROUTINE void _Scheduler_Yield(
107  const Scheduler_Control *scheduler,
108  Thread_Control          *the_thread
109)
110{
111  ( *scheduler->Operations.yield )( scheduler, the_thread );
112}
113
114/**
115 * @brief Scheduler block.
116 *
117 * This routine removes @a the_thread from the scheduling decision for
118 * the scheduler. The primary task is to remove the thread from the
119 * ready queue.  It performs any necessary schedulering operations
120 * including the selection of a new heir thread.
121 */
122RTEMS_INLINE_ROUTINE void _Scheduler_Block(
123  const Scheduler_Control *scheduler,
124  Thread_Control               *the_thread
125)
126{
127  ( *scheduler->Operations.block )( scheduler, the_thread );
128}
129
130/**
131 * @brief Scheduler unblock.
132 *
133 * This routine adds @a the_thread to the scheduling decision for
134 * the scheduler.  The primary task is to add the thread to the
135 * ready queue per the schedulering policy and update any appropriate
136 * scheduling variables, for example the heir thread.
137 */
138RTEMS_INLINE_ROUTINE void _Scheduler_Unblock(
139  const Scheduler_Control *scheduler,
140  Thread_Control          *the_thread
141)
142{
143  ( *scheduler->Operations.unblock )( scheduler, the_thread );
144}
145
146/**
147 * @brief Scheduler allocate.
148 *
149 * This routine allocates @a the_thread->scheduler
150 */
151RTEMS_INLINE_ROUTINE bool _Scheduler_Allocate(
152  const Scheduler_Control *scheduler,
153  Thread_Control          *the_thread
154)
155{
156  return ( *scheduler->Operations.allocate )( scheduler, the_thread );
157}
158
159/**
160 * @brief Scheduler free.
161 *
162 * This routine frees @a the_thread->scheduler
163 */
164RTEMS_INLINE_ROUTINE void _Scheduler_Free(
165  const Scheduler_Control *scheduler,
166  Thread_Control          *the_thread
167)
168{
169  ( *scheduler->Operations.free )( scheduler, the_thread );
170}
171
172/**
173 * @brief Scheduler update.
174 *
175 * This routine updates @a the_thread->scheduler
176 */
177RTEMS_INLINE_ROUTINE void _Scheduler_Update(
178  const Scheduler_Control *scheduler,
179  Thread_Control          *the_thread
180)
181{
182  ( *scheduler->Operations.update )( scheduler, the_thread );
183}
184
185/**
186 * @brief Scheduler enqueue.
187 *
188 * This routine enqueue @a the_thread->scheduler
189 */
190RTEMS_INLINE_ROUTINE void _Scheduler_Enqueue(
191  const Scheduler_Control *scheduler,
192  Thread_Control          *the_thread
193)
194{
195  ( *scheduler->Operations.enqueue )( scheduler, the_thread );
196}
197
198/**
199 * @brief Scheduler enqueue first.
200 *
201 * This routine enqueue_first @a the_thread->scheduler
202 */
203RTEMS_INLINE_ROUTINE void _Scheduler_Enqueue_first(
204  const Scheduler_Control *scheduler,
205  Thread_Control          *the_thread
206)
207{
208  ( *scheduler->Operations.enqueue_first )( scheduler, the_thread );
209}
210
211/**
212 * @brief Scheduler extract.
213 *
214 * This routine extract @a the_thread->scheduler
215 */
216RTEMS_INLINE_ROUTINE void _Scheduler_Extract(
217  const Scheduler_Control *scheduler,
218  Thread_Control          *the_thread
219)
220{
221  ( *scheduler->Operations.extract )( scheduler, the_thread );
222}
223
224/**
225 * @brief Scheduler priority compare.
226 *
227 * This routine compares two priorities.
228 */
229RTEMS_INLINE_ROUTINE int _Scheduler_Priority_compare(
230  const Scheduler_Control *scheduler,
231  Priority_Control         p1,
232  Priority_Control         p2
233)
234{
235  return ( *scheduler->Operations.priority_compare )( p1, p2 );
236}
237
238/**
239 * @brief Scheduler release job.
240 *
241 * This routine is called when a new period of task is issued.
242 */
243RTEMS_INLINE_ROUTINE void _Scheduler_Release_job(
244  const Scheduler_Control *scheduler,
245  Thread_Control          *the_thread,
246  uint32_t                 length
247)
248{
249  ( *scheduler->Operations.release_job )( scheduler, the_thread, length );
250}
251
252/**
253 * @brief Scheduler method invoked at each clock tick.
254 *
255 * This method is invoked at each clock tick to allow the scheduler
256 * implementation to perform any activities required.  For the
257 * scheduler which support standard RTEMS features, this includes
258 * time-slicing management.
259 */
260RTEMS_INLINE_ROUTINE void _Scheduler_Tick( void )
261{
262  uint32_t cpu_count = _SMP_Get_processor_count();
263  uint32_t cpu_index;
264
265  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
266    const Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
267    const Scheduler_Control *scheduler = _Scheduler_Get_by_CPU( cpu );
268
269    if ( scheduler != NULL ) {
270      ( *scheduler->Operations.tick )( scheduler, cpu->executing );
271    }
272  }
273}
274
275/**
276 * @brief Starts the idle thread for a particular processor.
277 *
278 * @param[in,out] the_thread The idle thread for the processor.
279 * @parma[in,out] processor The processor for the idle thread.
280 *
281 * @see _Thread_Create_idle().
282 */
283RTEMS_INLINE_ROUTINE void _Scheduler_Start_idle(
284  const Scheduler_Control *scheduler,
285  Thread_Control          *the_thread,
286  Per_CPU_Control         *cpu
287)
288{
289  ( *scheduler->Operations.start_idle )( scheduler, the_thread, cpu );
290}
291
292#if defined(RTEMS_SMP)
293RTEMS_INLINE_ROUTINE const Scheduler_Assignment *_Scheduler_Get_assignment(
294  uint32_t cpu_index
295)
296{
297  return &_Scheduler_Assignments[ cpu_index ];
298}
299
300RTEMS_INLINE_ROUTINE bool _Scheduler_Is_mandatory_processor(
301  const Scheduler_Assignment *assignment
302)
303{
304  return (assignment->attributes & SCHEDULER_ASSIGN_PROCESSOR_MANDATORY) != 0;
305}
306
307RTEMS_INLINE_ROUTINE bool _Scheduler_Should_start_processor(
308  const Scheduler_Assignment *assignment
309)
310{
311  return assignment->scheduler != NULL;
312}
313#endif /* defined(RTEMS_SMP) */
314
315RTEMS_INLINE_ROUTINE bool _Scheduler_Has_processor_ownership(
316  const Scheduler_Control *scheduler,
317  uint32_t cpu_index
318)
319{
320#if defined(RTEMS_SMP)
321  const Scheduler_Assignment *assignment =
322    _Scheduler_Get_assignment( cpu_index );
323
324  return assignment->scheduler == scheduler;
325#else
326  (void) scheduler;
327  (void) cpu_index;
328
329  return true;
330#endif
331}
332
333#if defined(__RTEMS_HAVE_SYS_CPUSET_H__)
334
335RTEMS_INLINE_ROUTINE void _Scheduler_Get_processor_set(
336  const Scheduler_Control *scheduler,
337  size_t                   cpusetsize,
338  cpu_set_t               *cpuset
339)
340{
341  uint32_t cpu_count = _SMP_Get_processor_count();
342  uint32_t cpu_index;
343
344  CPU_ZERO_S( cpusetsize, cpuset );
345
346  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
347#if defined(RTEMS_SMP)
348    if ( _Scheduler_Has_processor_ownership( scheduler, cpu_index ) ) {
349      CPU_SET_S( (int) cpu_index, cpusetsize, cpuset );
350    }
351#else
352    (void) scheduler;
353
354    CPU_SET_S( (int) cpu_index, cpusetsize, cpuset );
355#endif
356  }
357}
358
359RTEMS_INLINE_ROUTINE bool _Scheduler_default_Get_affinity_body(
360  const Scheduler_Control *scheduler,
361  Thread_Control          *the_thread,
362  size_t                   cpusetsize,
363  cpu_set_t               *cpuset
364)
365{
366  (void) the_thread;
367
368  _Scheduler_Get_processor_set( scheduler, cpusetsize, cpuset );
369
370  return true;
371}
372
373bool _Scheduler_Get_affinity(
374  const Scheduler_Control *scheduler,
375  Thread_Control          *the_thread,
376  size_t                   cpusetsize,
377  cpu_set_t               *cpuset
378);
379
380RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get(
381  Thread_Control *the_thread
382)
383{
384#if defined(RTEMS_SMP)
385  return the_thread->scheduler;
386#else
387  (void) the_thread;
388
389  return &_Scheduler_Table[ 0 ];
390#endif
391}
392
393RTEMS_INLINE_ROUTINE bool _Scheduler_Set(
394  const Scheduler_Control *scheduler,
395  Thread_Control          *the_thread
396)
397{
398  bool ok;
399
400  if ( _States_Is_dormant( the_thread->current_state ) ) {
401#if defined(RTEMS_SMP)
402    _Scheduler_Free( _Scheduler_Get( the_thread ), the_thread );
403    the_thread->scheduler = scheduler;
404    _Scheduler_Allocate( scheduler, the_thread );
405    _Scheduler_Update( scheduler, the_thread );
406#else
407    (void) scheduler;
408#endif
409
410    ok = true;
411  } else {
412    ok = false;
413  }
414
415  return ok;
416}
417
418RTEMS_INLINE_ROUTINE bool _Scheduler_default_Set_affinity_body(
419  const Scheduler_Control *scheduler,
420  Thread_Control          *the_thread,
421  size_t                   cpusetsize,
422  const cpu_set_t         *cpuset
423)
424{
425  size_t   cpu_max   = _CPU_set_Maximum_CPU_count( cpusetsize );
426  uint32_t cpu_count = _SMP_Get_processor_count();
427  uint32_t cpu_index;
428  bool     ok = true;
429
430  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
431#if defined(RTEMS_SMP)
432    const Scheduler_Control *scheduler_of_cpu =
433      _Scheduler_Get_by_CPU_index( cpu_index );
434
435    ok = ok
436      && ( ( CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset )
437          && scheduler == scheduler_of_cpu )
438        || ( !CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset )
439          && scheduler != scheduler_of_cpu ) );
440#else
441    (void) scheduler;
442
443    ok = ok && CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset );
444#endif
445  }
446
447  for ( ; cpu_index < cpu_max ; ++cpu_index ) {
448    ok = ok && !CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset );
449  }
450
451  if ( ok ) {
452    ok = _Scheduler_Set( scheduler, the_thread );
453  }
454
455  return ok;
456}
457
458bool _Scheduler_Set_affinity(
459  Thread_Control          *the_thread,
460  size_t                   cpusetsize,
461  const cpu_set_t         *cpuset
462);
463
464#endif /* defined(__RTEMS_HAVE_SYS_CPUSET_H__) */
465
466RTEMS_INLINE_ROUTINE void _Scheduler_Update_heir(
467  Thread_Control *heir,
468  bool force_dispatch
469)
470{
471  Thread_Control *executing = _Thread_Executing;
472
473  _Thread_Heir = heir;
474
475  if ( executing != heir && ( force_dispatch || executing->is_preemptible ) )
476    _Thread_Dispatch_necessary = true;
477}
478
479RTEMS_INLINE_ROUTINE void _Scheduler_Generic_block(
480  const Scheduler_Control *scheduler,
481  Thread_Control          *the_thread,
482  void                  ( *extract )(
483                             const Scheduler_Control *,
484                             Thread_Control * ),
485  void                  ( *schedule )(
486                             const Scheduler_Control *,
487                             Thread_Control *,
488                             bool )
489)
490{
491  ( *extract )( scheduler, the_thread );
492
493  /* TODO: flash critical section? */
494
495  if ( _Thread_Is_executing( the_thread ) || _Thread_Is_heir( the_thread ) ) {
496    ( *schedule )( scheduler, the_thread, true );
497  }
498}
499
500/**
501 * @brief Returns true if @p1 encodes a lower priority than @a p2 in the
502 * intuitive sense of priority.
503 */
504RTEMS_INLINE_ROUTINE bool _Scheduler_Is_priority_lower_than(
505  const Scheduler_Control *scheduler,
506  Priority_Control         p1,
507  Priority_Control         p2
508)
509{
510  return _Scheduler_Priority_compare( scheduler, p1,  p2 ) < 0;
511}
512
513/**
514 * @brief Returns true if @p1 encodes a higher priority than @a p2 in the
515 * intuitive sense of priority.
516 */
517RTEMS_INLINE_ROUTINE bool _Scheduler_Is_priority_higher_than(
518  const Scheduler_Control *scheduler,
519  Priority_Control         p1,
520  Priority_Control         p2
521)
522{
523  return _Scheduler_Priority_compare( scheduler, p1,  p2 ) > 0;
524}
525
526/**
527 * @brief Returns the priority encoding @a p1 or @a p2 with the higher priority
528 * in the intuitive sense of priority.
529 */
530RTEMS_INLINE_ROUTINE Priority_Control _Scheduler_Highest_priority_of_two(
531  const Scheduler_Control *scheduler,
532  Priority_Control         p1,
533  Priority_Control         p2
534)
535{
536  return _Scheduler_Is_priority_higher_than( scheduler, p1, p2 ) ? p1 : p2;
537}
538
539/**
540 * @brief Sets the thread priority to @a priority if it is higher than the
541 * current priority of the thread in the intuitive sense of priority.
542 */
543RTEMS_INLINE_ROUTINE void _Scheduler_Set_priority_if_higher(
544  const Scheduler_Control *scheduler,
545  Thread_Control          *the_thread,
546  Priority_Control         priority
547)
548{
549  Priority_Control current = the_thread->current_priority;
550
551  if ( _Scheduler_Is_priority_higher_than( scheduler, priority, current ) ) {
552    _Thread_Set_priority( the_thread, priority );
553  }
554}
555
556/**
557 * @brief Changes the thread priority to @a priority if it is higher than the
558 * current priority of the thread in the intuitive sense of priority.
559 */
560RTEMS_INLINE_ROUTINE void _Scheduler_Change_priority_if_higher(
561  const Scheduler_Control *scheduler,
562  Thread_Control          *the_thread,
563  Priority_Control         priority,
564  bool                     prepend_it
565)
566{
567  Priority_Control current = the_thread->current_priority;
568
569  if ( _Scheduler_Is_priority_higher_than( scheduler, priority, current ) ) {
570    _Thread_Change_priority( the_thread, priority, prepend_it );
571  }
572}
573
574RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_processor_count(
575  const Scheduler_Control *scheduler
576)
577{
578#if defined(RTEMS_SMP)
579  return scheduler->context->processor_count;
580#else
581  (void) scheduler;
582
583  return 1;
584#endif
585}
586
587RTEMS_INLINE_ROUTINE Objects_Id _Scheduler_Build_id( uint32_t scheduler_index )
588{
589  return _Objects_Build_id(
590    OBJECTS_FAKE_OBJECTS_API,
591    OBJECTS_FAKE_OBJECTS_SCHEDULERS,
592    _Objects_Local_node,
593    scheduler_index + 1
594  );
595}
596
597RTEMS_INLINE_ROUTINE bool _Scheduler_Get_by_id(
598  Objects_Id                id,
599  const Scheduler_Control **scheduler_p
600)
601{
602  uint32_t minimum_id = _Scheduler_Build_id( 0 );
603  uint32_t index = id - minimum_id;
604  const Scheduler_Control *scheduler = &_Scheduler_Table[ index ];
605
606  *scheduler_p = scheduler;
607
608  return index < _Scheduler_Count
609    && _Scheduler_Get_processor_count( scheduler ) > 0;
610}
611
612RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_index(
613  const Scheduler_Control *scheduler
614)
615{
616  return (uint32_t) (scheduler - &_Scheduler_Table[ 0 ]);
617}
618
619/** @} */
620
621#ifdef __cplusplus
622}
623#endif
624
625#endif
626/* end of include file */
Note: See TracBrowser for help on using the repository browser.