summaryrefslogtreecommitdiffstats
path: root/cpukit/score/inline/rtems/score/schedulerpriority.inl
blob: b35682fa67fce21c291d17d19ebf0af169cc5837 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/** 
 *  @file  rtems/score/schedulerpriority.inl
 *
 *  This inline file contains all of the inlined routines associated with
 *  the manipulation of the priority-based scheduling structures.
 */

/*
 *  Copyright (C) 2010 Gedare Bloom.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#ifndef _RTEMS_SCORE_SCHEDULERPRIORITY_H
# error "Never use <rtems/score/schedulerpriority.inl> directly; include <rtems/score/schedulerpriority.h> instead."
#endif

#ifndef _RTEMS_SCORE_SCHEDULERPRIORITY_INL
#define _RTEMS_SCORE_SCHEDULERPRIORITY_INL

/**
 *  @addtogroup ScoreScheduler
 * @{
 */

/** @brief  Scheduler priority Ready queue initialize
 *
 *  This routine initializes @a the_ready_queue for priority-based scheduling.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_initialize(
  Scheduler_Control         *the_scheduler
) {
  size_t index;

  /* allocate ready queue structures */
  the_scheduler->Ready_queues.priority = (Chain_Control *) 
    _Workspace_Allocate_or_fatal_error(
      ((size_t) PRIORITY_MAXIMUM + 1) * sizeof(Chain_Control)
    );

  /* initialize ready queue structures */
  for( index=0; index <= PRIORITY_MAXIMUM; index++)
    _Chain_Initialize_empty( &the_scheduler->Ready_queues.priority[index] );
}

/* 
 *  _Scheduler_priority_Ready_queue_enqueue
 *
 *  This routine puts @a the_thread on to the priority-based ready queue.
 *  
 *  Input parameters:
 *    the_thread  - pointer to thread
 *
 *  Output parameters: NONE
 *
 *  INTERRUPT LATENCY:
 */

RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_enqueue(
  Thread_Control                  *the_thread
)
{
  _Priority_bit_map_Add( &the_thread->scheduler.priority->Priority_map );
  
  _Chain_Append_unprotected( the_thread->scheduler.priority->ready_chain, 
      &the_thread->Object.Node );
}

/*
 *  _Scheduler_priority_Ready_queue_Enqueue_first
 *
 *  This routine puts @a the_thread to the head of the ready queue. 
 *  For priority-based ready queues, the thread will be the first thread
 *  at its priority level.
 *  
 *  Input parameters:
 *    the_thread      - pointer to thread
 *
 *  Output parameters: NONE
 *
 *  INTERRUPT LATENCY:
 */

RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_enqueue_first(
  Thread_Control                   *the_thread
)
{
  _Priority_bit_map_Add( &the_thread->scheduler.priority->Priority_map );

  _Chain_Prepend_unprotected( the_thread->scheduler.priority->ready_chain, 
      &the_thread->Object.Node );
}

/*
 *  _Scheduler_priority_Ready_queue_extract
 *
 *  This routine removes a specific thread from the specified 
 *  priority-based ready queue.
 *
 *  Input parameters:
 *    the_thread       - pointer to a thread control block
 *
 *  Output parameters: NONE
 *
 *  INTERRUPT LATENCY: NONE
 */

RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_extract(
  Thread_Control        *the_thread
)
{
  Chain_Control         *ready  = the_thread->scheduler.priority->ready_chain;

  if ( _Chain_Has_only_one_node( ready ) ) {
    _Chain_Initialize_empty( ready );
    _Priority_bit_map_Remove( &the_thread->scheduler.priority->Priority_map );
  } else
    _Chain_Extract_unprotected( &the_thread->Object.Node );
}

/*
 *  _Scheduler_priority_Ready_queue_first
 *
 *  This routines returns a pointer to the first thread on @a the_ready_queue.
 *
 *  Input parameters:
 *    the_ready_queue - pointer to thread queue
 *
 *  Output parameters:
 *    returns - first thread or NULL
 */

RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_priority_Ready_queue_first(
  Chain_Control       *the_ready_queue
)
{
  Priority_Control index = _Priority_bit_map_Get_highest();

  if ( !_Chain_Is_empty( &the_ready_queue[ index ] ) )
    return (Thread_Control *) _Chain_First( &the_ready_queue[ index ] );

  return NULL;
}

/*
 *  _Scheduler_priority_Ready_queue_requeue
 *
 *  This routine is invoked when a thread changes priority and should be
 *  moved to a different position on the ready queue.
 *
 *  Input parameters:
 *    the_thread        - pointer to a thread control block
 *
 *  Output parameters: NONE
 *
 *  INTERRUPT LATENCY: NONE
 */

RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_requeue(
  Thread_Control            *the_thread
)
{
  if ( !_Chain_Has_only_one_node(
        the_thread->scheduler.priority->ready_chain
        ) ) {
    _Chain_Extract_unprotected( &the_thread->Object.Node );

    _Chain_Append_unprotected( the_thread->scheduler.priority->ready_chain, 
      &the_thread->Object.Node );
  }
}

/*
 * _Scheduler_priority_Schedule_body
 *
 * This kernel routine implements scheduling decision logic for priority-based
 * scheduling.  
 *
 * Input parameters:
 *   the_scheduler  - pointer to scheduler control
 *   the_thread     - pointer to thread control block
 *
 * Output parameters:  NONE
 *
 *  INTERRUPT LATENCY:
 */

RTEMS_INLINE_ROUTINE void _Scheduler_priority_Schedule_body(
  Scheduler_Control     *the_scheduler
)
{
  _Thread_Heir = _Scheduler_priority_Ready_queue_first(
      the_scheduler->Ready_queues.priority
  );
}

/*
 * _Scheduler_priority_Block_body
 *
 * This kernel routine removes the_thread from scheduling decisions based 
 * on simple queue extraction.
 *
 * Input parameters:
 *   the_scheduler  - pointer to scheduler control
 *   the_thread     - pointer to thread control block
 *
 * Output parameters:  NONE
 *
 *  INTERRUPT LATENCY:
 */

RTEMS_INLINE_ROUTINE void _Scheduler_priority_Block_body(
  Scheduler_Control *the_scheduler,
  Thread_Control   *the_thread
)
{
  _Scheduler_priority_Ready_queue_extract(the_thread);

  /* TODO: flash critical section */

  if ( _Thread_Is_heir( the_thread ) )
     _Scheduler_priority_Schedule_body(the_scheduler);

  if ( _Thread_Is_executing( the_thread ) )
    _Thread_Dispatch_necessary = true;

  return;
}

/*
 *  _Scheduler_priority_Unblock_body
 *
 *  This kernel routine readies the requested thread according to the queuing 
 *  discipline. A new heir thread may be selected.
 *
 *  Input parameters:
 *    the_scheduler - pointer to scheduler control
 *    the_thread    - pointer to thread control block
 *
 *  Output parameters:  NONE
 *
 *  NOTE:  This routine uses the "blocking" heir selection mechanism.
 *         This ensures the correct heir after a thread restart.
 *
 *  INTERRUPT LATENCY:
 */

RTEMS_INLINE_ROUTINE void _Scheduler_priority_Unblock_body (
  Scheduler_Control       *the_scheduler __attribute__((unused)),
  Thread_Control          *the_thread
)
{
  _Scheduler_priority_Ready_queue_enqueue(
      the_thread
  );

  /* TODO: flash critical section */

  /*
   *  If the thread that was unblocked is more important than the heir,
   *  then we have a new heir.  This may or may not result in a
   *  context switch.
   *
   *  Normal case:
   *    If the current thread is preemptible, then we need to do
   *    a context switch.
   *  Pseudo-ISR case:
   *    Even if the thread isn't preemptible, if the new heir is
   *    a pseudo-ISR system task, we need to do a context switch.
   */
  if ( the_thread->current_priority < _Thread_Heir->current_priority ) {
    _Thread_Heir = the_thread;
    if ( _Thread_Executing->is_preemptible ||
        the_thread->current_priority == 0 )
      _Thread_Dispatch_necessary = true;
  }
}

/**@}*/

#endif
/* end of include file */