summaryrefslogtreecommitdiffstats
path: root/cpukit/include/rtems/score/schedulerpriorityimpl.h
blob: d8d226d6f19fd453ea05dfece70df01b5a04d7af (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
287
288
289
290
291
292
293
294
295
296
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSScoreSchedulerDPS
 *
 * @brief This header file provides interfaces of the
 *   @ref RTEMSScoreSchedulerDPS which are only used by the implementation.
 */

/*
 *  Copyright (C) 2010 Gedare Bloom.
 *  Copyright (C) 2011 On-Line Applications Research Corporation (OAR).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _RTEMS_SCORE_SCHEDULERPRIORITYIMPL_H
#define _RTEMS_SCORE_SCHEDULERPRIORITYIMPL_H

#include <rtems/score/schedulerpriority.h>
#include <rtems/score/chainimpl.h>
#include <rtems/score/prioritybitmapimpl.h>
#include <rtems/score/schedulerimpl.h>
#include <rtems/score/thread.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @addtogroup RTEMSScoreSchedulerDPS
 *
 * @{
 */

/**
 * @brief Gets the context of the scheduler.
 *
 * @param scheduler The scheduler to get the context of.
 *
 * @return The context of the scheduler.
 */
RTEMS_INLINE_ROUTINE Scheduler_priority_Context *
  _Scheduler_priority_Get_context( const Scheduler_Control *scheduler )
{
  return (Scheduler_priority_Context *) _Scheduler_Get_context( scheduler );
}

/**
 * @brief Gets the scheduler node of the thread.
 *
 * @param the_thread The thread to get the scheduler node of.
 *
 * @return The scheduler node of @a the_thread.
 */
RTEMS_INLINE_ROUTINE Scheduler_priority_Node *_Scheduler_priority_Thread_get_node(
  Thread_Control *the_thread
)
{
  return (Scheduler_priority_Node *) _Thread_Scheduler_get_home_node( the_thread );
}

/**
 * @brief Gets the priority node of the scheduler node.
 *
 * @param node The node to get the priority node of.
 *
 * @return The priority node.
 */
RTEMS_INLINE_ROUTINE Scheduler_priority_Node *_Scheduler_priority_Node_downcast(
  Scheduler_Node *node
)
{
  return (Scheduler_priority_Node *) node;
}

/**
 * @brief Ready queue initialization.
 *
 * This routine initializes @a ready_queues for priority-based scheduling.
 *
 * @param[out] ready_queues The ready queue to initialize.
 * @param maximum_priority The maximum priority in the ready queue.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_initialize(
  Chain_Control    *ready_queues,
  Priority_Control  maximum_priority
)
{
  size_t index;

  for ( index = 0 ; index <= (size_t) maximum_priority ; ++index ) {
    _Chain_Initialize_empty( &ready_queues[ index ] );
  }
}

/**
 * @brief Enqueues a node on the specified ready queue.
 *
 * The node is placed as the last element of its priority group.
 *
 * @param node The node to enqueue.
 * @param[in, out] ready_queue The ready queue.
 * @param[out] bit_map The priority bit map of the scheduler instance.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_enqueue(
  Chain_Node                     *node,
  Scheduler_priority_Ready_queue *ready_queue,
  Priority_bit_map_Control       *bit_map
)
{
  Chain_Control *ready_chain = ready_queue->ready_chain;

  _Chain_Append_unprotected( ready_chain, node );
  _Priority_bit_map_Add( bit_map, &ready_queue->Priority_map );
}

/**
 * @brief Enqueues a node on the specified ready queue as first.
 *
 * The node is placed as the first element of its priority group.
 *
 * @param node The node to enqueue as first.
 * @param[in, out] ready_queue The ready queue.
 * @param[out] bit_map The priority bit map of the scheduler instance.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_enqueue_first(
  Chain_Node                     *node,
  Scheduler_priority_Ready_queue *ready_queue,
  Priority_bit_map_Control       *bit_map
)
{
  Chain_Control *ready_chain = ready_queue->ready_chain;

  _Chain_Prepend_unprotected( ready_chain, node );
  _Priority_bit_map_Add( bit_map, &ready_queue->Priority_map );
}

/**
 * @brief Extracts a node from the specified ready queue.
 *
 * @param node The node to extract.
 * @param[in, out] ready_queue The ready queue.
 * @param[out] bit_map The priority bit map of the scheduler instance.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_extract(
  Chain_Node                     *node,
  Scheduler_priority_Ready_queue *ready_queue,
  Priority_bit_map_Control       *bit_map
)
{
  Chain_Control *ready_chain = ready_queue->ready_chain;

  if ( _Chain_Has_only_one_node( ready_chain ) ) {
    _Chain_Initialize_empty( ready_chain );
    _Chain_Initialize_node( node );
    _Priority_bit_map_Remove( bit_map, &ready_queue->Priority_map );
  } else {
    _Chain_Extract_unprotected( node );
  }
}

/**
 * @brief Extracts a node from the context of the scheduler.
 *
 * @param scheduler The scheduler instance.
 * @param the_thread The thread of which the node will be extracted.
 * @param[in, out] The node which preserves the ready queue.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Extract_body(
  const Scheduler_Control *scheduler,
  Thread_Control          *the_thread,
  Scheduler_Node          *node
)
{
  Scheduler_priority_Context *context;
  Scheduler_priority_Node    *the_node;

  context = _Scheduler_priority_Get_context( scheduler );
  the_node = _Scheduler_priority_Node_downcast( node );

  _Scheduler_priority_Ready_queue_extract(
    &the_thread->Object.Node,
    &the_node->Ready_queue,
    &context->Bit_map
  );
}

/**
 * @brief Returns a pointer to the first node.
 *
 * This routines returns a pointer to the first node on @a ready_queues.
 *
 * @param bit_map The priority bit map of the scheduler instance.
 * @param ready_queues The ready queues of the scheduler instance.
 *
 * @return This method returns the first node.
 */
RTEMS_INLINE_ROUTINE Chain_Node *_Scheduler_priority_Ready_queue_first(
  Priority_bit_map_Control *bit_map,
  Chain_Control            *ready_queues
)
{
  Priority_Control index = _Priority_bit_map_Get_highest( bit_map );
  Chain_Node *first = _Chain_First( &ready_queues[ index ] );

  _Assert( first != _Chain_Tail( &ready_queues[ index ] ) );

  return first;
}

/**
 * @brief Scheduling decision logic.
 *
 * This kernel routine implements scheduling decision logic
 * for priority-based scheduling.
 *
 * @param[in, out] scheduler The scheduler instance.
 * @param the_thread This parameter is unused.
 * @param force_dispatch Indicates whether the dispatch happens also if
 *      the currently executing thread is set as not preemptible.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Schedule_body(
  const Scheduler_Control *scheduler,
  Thread_Control          *the_thread,
  bool                     force_dispatch
)
{
  Scheduler_priority_Context *context =
    _Scheduler_priority_Get_context( scheduler );
  Thread_Control *heir = (Thread_Control *)
    _Scheduler_priority_Ready_queue_first(
      &context->Bit_map,
      &context->Ready[ 0 ]
    );

  ( void ) the_thread;

  _Scheduler_Update_heir( heir, force_dispatch );
}

/**
 * @brief Updates the specified ready queue data according to the new priority
 * value.
 *
 * @param[in, out] ready_queue The ready queue.
 * @param new_priority The new priority.
 * @param bit_map The priority bit map of the scheduler instance.
 * @param ready_queues The ready queues of the scheduler instance.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_update(
  Scheduler_priority_Ready_queue *ready_queue,
  unsigned int                    new_priority,
  Priority_bit_map_Control       *bit_map,
  Chain_Control                  *ready_queues
)
{
  ready_queue->current_priority = new_priority;
  ready_queue->ready_chain = &ready_queues[ new_priority ];

  _Priority_bit_map_Initialize_information(
    bit_map,
    &ready_queue->Priority_map,
    new_priority
  );
}

/** @} */

#ifdef __cplusplus
}
#endif

#endif
/* end of include file */