summaryrefslogtreecommitdiffstats
path: root/cpukit/score/include/rtems/score/schedulerpriorityimpl.h
blob: 73c635dced11e5e43455a2008ae3546482999d8e (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
/**
 * @file
 *
 * @brief Inlined Routines Associated with the Manipulation of the
 * Priority-Based Scheduling Structures
 *
 * This inline file contains all of the inlined routines associated with
 * the manipulation of the priority-based scheduling structures.
 */

/*
 *  Copyright (C) 2010 Gedare Bloom.
 *  Copyright (C) 2011 On-Line Applications Research Corporation (OAR).
 *
 *  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.
 */

#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>
#include <rtems/score/wkspace.h>

#ifdef __cplusplus
extern "C" {
#endif

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

/**
 * @brief Ready queue initialization.
 *
 * This routine initializes @a the_ready_queue for priority-based scheduling.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_initialize(void)
{
  size_t         index;
  Chain_Control *ready_queues;

  /* allocate ready queue structures */
  _Scheduler.information = _Workspace_Allocate_or_fatal_error(
    ((size_t) PRIORITY_MAXIMUM + 1) * sizeof(Chain_Control)
  );

  /* initialize ready queue structures */
  ready_queues = (Chain_Control *) _Scheduler.information;
  for( index=0; index <= PRIORITY_MAXIMUM; index++)
    _Chain_Initialize_empty( &ready_queues[index] );
}

/**
 * @brief Put a thread to the ready queue.
 *
 * This routine puts @a the_thread on to the priority-based ready queue.
 *
 * @param[in] the_thread is a pointer to the thread
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_enqueue(
  Thread_Control                  *the_thread
)
{
  Scheduler_priority_Per_thread *sched_info;
  Chain_Control                 *ready;

  sched_info = (Scheduler_priority_Per_thread *) the_thread->scheduler_info;
  ready      = sched_info->ready_chain;

  _Priority_bit_map_Add( &sched_info->Priority_map );

  _Chain_Append_unprotected( ready, &the_thread->Object.Node );
}

/**
 * @brief Put a thread to the head of the ready queue.
 *
 * 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.
 *
 * @param[in] the_thread is a pointer to the thread.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_enqueue_first(
  Thread_Control                   *the_thread
)
{
  Scheduler_priority_Per_thread *sched_info;

  sched_info = (Scheduler_priority_Per_thread *) the_thread->scheduler_info;

  _Priority_bit_map_Add( &sched_info->Priority_map );

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

/**
 * @brief Remove a specific thread from the ready queue.
 *
 * This routine removes a specific thread from the specified
 * priority-based ready queue.
 *
 * @param[in] the_thread is a pointer to the thread.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_extract(
  Thread_Control        *the_thread
)
{
  Scheduler_priority_Per_thread *sched_info;
  Chain_Control                 *ready;

  sched_info = (Scheduler_priority_Per_thread *) the_thread->scheduler_info;
  ready      = sched_info->ready_chain;

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

/**
 * @brief Return a pointer to the first thread.
 *
 * This routines returns a pointer to the first thread on @a the_ready_queue.
 *
 * @param[in] the_ready_queue - pointer to thread queue
 *
 * @return This method returns the 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;
}

/**
 * @brief Requeue a thread on the ready queue.
 *
 * This routine is invoked when a thread changes priority and should be
 * moved to a different position on the ready queue.
 *
 * @param[in] the_thread is a pointer to the thread
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Ready_queue_requeue(
  Thread_Control            *the_thread
)
{
  Scheduler_priority_Per_thread *sched_info;

  sched_info = (Scheduler_priority_Per_thread *) the_thread->scheduler_info;

  if ( !_Chain_Has_only_one_node( sched_info->ready_chain ) ) {
    _Chain_Extract_unprotected( &the_thread->Object.Node );

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

/**
 * @brief Scheduling decision logic.
 *
 * This kernel routine implements scheduling decision logic
 * for priority-based scheduling.
 */
RTEMS_INLINE_ROUTINE void _Scheduler_priority_Schedule_body(
  Thread_Control *thread,
  bool force_dispatch
)
{
  Thread_Control *heir = _Scheduler_priority_Ready_queue_first(
    (Chain_Control *) _Scheduler.information
  );

  ( void ) thread;

  _Scheduler_Update_heir( heir, force_dispatch );
}

/**
 * @brief Priority comparison.
 *
 * This routine implements priority comparison for priority-based
 * scheduling.
 *
 * @return >0 for higher priority, 0 for equal and <0 for lower priority.
 */
RTEMS_INLINE_ROUTINE int _Scheduler_priority_Priority_compare_body(
  Priority_Control      p1,
  Priority_Control      p2
)
{
  /* High priority in priority scheduler is represented by low numbers. */
  return ( p2 - p1 );
}

/** @} */

#ifdef __cplusplus
}
#endif

#endif
/* end of include file */