summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/schedulerprioritysmp.c
blob: fed94b11e2f4ca4c34dcf46aa0eedcd027866fe0 (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
/**
 * @file
 *
 * @brief Deterministic Priority SMP Scheduler Implementation
 *
 * @ingroup ScoreSchedulerSMP
 */

/*
 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * 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.
 */

#if HAVE_CONFIG_H
  #include "config.h"
#endif

#include <rtems/score/schedulerprioritysmp.h>
#include <rtems/score/schedulerpriorityimpl.h>
#include <rtems/score/schedulersmpimpl.h>
#include <rtems/score/wkspace.h>

void _Scheduler_priority_SMP_Initialize( void )
{
  Scheduler_SMP_Control *self = _Workspace_Allocate_or_fatal_error(
    sizeof( *self ) + PRIORITY_MAXIMUM * sizeof( Chain_Control )
  );

  _Chain_Initialize_empty( &self->scheduled );
  _Scheduler_priority_Ready_queue_initialize( &self->ready[ 0 ] );

  _Scheduler.information = self;
}

void _Scheduler_priority_SMP_Update( Thread_Control *thread )
{
  Scheduler_SMP_Control *self = _Scheduler_SMP_Instance();

  _Scheduler_priority_Update_body( thread, &self->ready[ 0 ] );
}

static Thread_Control *_Scheduler_priority_SMP_Get_highest_ready(
  Scheduler_SMP_Control *self
)
{
  Thread_Control *highest_ready = NULL;

  if ( !_Priority_bit_map_Is_empty() ) {
    highest_ready = _Scheduler_priority_Ready_queue_first( &self->ready[ 0 ] );
  }

  return highest_ready;
}

static void _Scheduler_priority_SMP_Move_from_scheduled_to_ready(
  Scheduler_SMP_Control *self,
  Thread_Control *scheduled_to_ready
)
{
  _Chain_Extract_unprotected( &scheduled_to_ready->Object.Node );
  _Scheduler_priority_Ready_queue_enqueue_first( scheduled_to_ready );
}

static void _Scheduler_priority_SMP_Move_from_ready_to_scheduled(
  Scheduler_SMP_Control *self,
  Thread_Control *ready_to_scheduled
)
{
  _Scheduler_priority_Ready_queue_extract( ready_to_scheduled );
  _Scheduler_simple_Insert_priority_fifo(
    &self->scheduled,
    ready_to_scheduled
  );
}

static void _Scheduler_priority_SMP_Insert_ready_lifo(
  Scheduler_SMP_Control *self,
  Thread_Control *thread
)
{
  _Scheduler_priority_Ready_queue_enqueue( thread );
}

static void _Scheduler_priority_SMP_Insert_ready_fifo(
  Scheduler_SMP_Control *self,
  Thread_Control *thread
)
{
  _Scheduler_priority_Ready_queue_enqueue_first( thread );
}

static void _Scheduler_priority_SMP_Do_extract(
  Scheduler_SMP_Control *self,
  Thread_Control *thread
)
{
  bool is_scheduled = thread->is_scheduled;

  ( void ) self;

  thread->is_in_the_air = is_scheduled;
  thread->is_scheduled = false;

  if ( is_scheduled ) {
    _Chain_Extract_unprotected( &thread->Object.Node );
  } else {
    _Scheduler_priority_Ready_queue_extract( thread );
  }
}

void _Scheduler_priority_SMP_Block( Thread_Control *thread )
{
  Scheduler_SMP_Control *self = _Scheduler_SMP_Instance();

  _Scheduler_SMP_Block(
    self,
    thread,
    _Scheduler_priority_SMP_Do_extract,
    _Scheduler_priority_SMP_Get_highest_ready,
    _Scheduler_priority_SMP_Move_from_ready_to_scheduled
  );
}

static void _Scheduler_priority_SMP_Enqueue_ordered(
  Scheduler_SMP_Control *self,
  Thread_Control *thread,
  Chain_Node_order order,
  Scheduler_SMP_Insert insert_ready,
  Scheduler_SMP_Insert insert_scheduled
)
{
  _Scheduler_SMP_Enqueue_ordered(
    self,
    thread,
    order,
    _Scheduler_priority_SMP_Get_highest_ready,
    insert_ready,
    insert_scheduled,
    _Scheduler_priority_SMP_Move_from_ready_to_scheduled,
    _Scheduler_priority_SMP_Move_from_scheduled_to_ready
  );
}

void _Scheduler_priority_SMP_Enqueue_lifo( Thread_Control *thread )
{
  Scheduler_SMP_Control *self = _Scheduler_SMP_Instance();

  _Scheduler_priority_SMP_Enqueue_ordered(
    self,
    thread,
    _Scheduler_simple_Insert_priority_lifo_order,
    _Scheduler_priority_SMP_Insert_ready_lifo,
    _Scheduler_SMP_Insert_scheduled_lifo
  );
}

void _Scheduler_priority_SMP_Enqueue_fifo( Thread_Control *thread )
{
  Scheduler_SMP_Control *self = _Scheduler_SMP_Instance();

  _Scheduler_priority_SMP_Enqueue_ordered(
    self,
    thread,
    _Scheduler_simple_Insert_priority_fifo_order,
    _Scheduler_priority_SMP_Insert_ready_fifo,
    _Scheduler_SMP_Insert_scheduled_fifo
  );
}

void _Scheduler_priority_SMP_Extract( Thread_Control *thread )
{
  Scheduler_SMP_Control *self = _Scheduler_SMP_Instance();

  _Scheduler_SMP_Extract(
    self,
    thread,
    _Scheduler_priority_SMP_Do_extract
  );
}

void _Scheduler_priority_SMP_Yield( Thread_Control *thread )
{
  ISR_Level level;

  _ISR_Disable( level );

  _Scheduler_priority_SMP_Extract( thread );
  _Scheduler_priority_SMP_Enqueue_fifo( thread );

  _ISR_Enable( level );
}

void _Scheduler_priority_SMP_Schedule( Thread_Control *thread )
{
  Scheduler_SMP_Control *self = _Scheduler_SMP_Instance();

  _Scheduler_SMP_Schedule(
    self,
    thread,
    _Scheduler_priority_SMP_Get_highest_ready,
    _Scheduler_priority_SMP_Move_from_ready_to_scheduled
  );
}