summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/threadqops.c
blob: 2967a0efc341a63070d54f7a67810523b5239810 (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
/*
 * Copyright (c) 2015 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.org/license/LICENSE.
 */

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

#include <rtems/score/threadimpl.h>
#include <rtems/score/chainimpl.h>
#include <rtems/score/rbtreeimpl.h>

static void _Thread_queue_Do_nothing_priority_change(
  Thread_Control       *the_thread,
  Priority_Control      new_priority,
  Thread_queue_Control *the_thread_queue
)
{
  /* Do nothing */
}

static void _Thread_queue_Do_nothing_extract(
  Thread_queue_Control *the_thread_queue,
  Thread_Control       *the_thread
)
{
  /* Do nothing */
}

static void _Thread_queue_FIFO_initialize(
  Thread_queue_Control *the_thread_queue
)
{
  _Chain_Initialize_empty( &the_thread_queue->Queues.Fifo );
}

static void _Thread_queue_FIFO_enqueue(
  Thread_queue_Control *the_thread_queue,
  Thread_Control       *the_thread
)
{
  _Chain_Append_unprotected(
    &the_thread_queue->Queues.Fifo,
    &the_thread->Wait.Node.Chain
  );
}

static void _Thread_queue_FIFO_extract(
  Thread_queue_Control *the_thread_queue,
  Thread_Control       *the_thread
)
{
  _Chain_Extract_unprotected( &the_thread->Wait.Node.Chain );
}

static Thread_Control *_Thread_queue_FIFO_first(
  Thread_queue_Control *the_thread_queue
)
{
  Chain_Control *fifo = &the_thread_queue->Queues.Fifo;

  return _Chain_Is_empty( fifo ) ?
    NULL : THREAD_CHAIN_NODE_TO_THREAD( _Chain_First( fifo ) );
}

static void _Thread_queue_Priority_priority_change(
  Thread_Control       *the_thread,
  Priority_Control      new_priority,
  Thread_queue_Control *the_thread_queue
)
{
  _RBTree_Extract(
    &the_thread_queue->Queues.Priority,
    &the_thread->Wait.Node.RBTree
  );
  _RBTree_Insert(
    &the_thread_queue->Queues.Priority,
    &the_thread->Wait.Node.RBTree,
    _Thread_queue_Compare_priority,
    false
  );
}

static void _Thread_queue_Priority_initialize(
  Thread_queue_Control *the_thread_queue
)
{
  _RBTree_Initialize_empty( &the_thread_queue->Queues.Priority );
}

static void _Thread_queue_Priority_enqueue(
  Thread_queue_Control *the_thread_queue,
  Thread_Control       *the_thread
)
{
  _RBTree_Insert(
    &the_thread_queue->Queues.Priority,
    &the_thread->Wait.Node.RBTree,
    _Thread_queue_Compare_priority,
    false
  );
}

static void _Thread_queue_Priority_extract(
  Thread_queue_Control *the_thread_queue,
  Thread_Control       *the_thread
)
{
  _RBTree_Extract(
    &the_thread_queue->Queues.Priority,
    &the_thread->Wait.Node.RBTree
  );
}

static Thread_Control *_Thread_queue_Priority_first(
  Thread_queue_Control *the_thread_queue
)
{
  RBTree_Node *first;

  first = _RBTree_First( &the_thread_queue->Queues.Priority, RBT_LEFT );

  return first != NULL ? THREAD_RBTREE_NODE_TO_THREAD( first ) : NULL;
}

const Thread_queue_Operations _Thread_queue_Operations_default = {
  .priority_change = _Thread_queue_Do_nothing_priority_change,
  .extract = _Thread_queue_Do_nothing_extract
  /*
   * The default operations are only used in _Thread_Change_priority() and
   * _Thread_Timeout() and don't have a thread queue associated with them, so
   * the enqueue and first operations are superfluous.
   */
};

const Thread_queue_Operations _Thread_queue_Operations_FIFO = {
  .priority_change = _Thread_queue_Do_nothing_priority_change,
  .initialize = _Thread_queue_FIFO_initialize,
  .enqueue = _Thread_queue_FIFO_enqueue,
  .extract = _Thread_queue_FIFO_extract,
  .first = _Thread_queue_FIFO_first
};

const Thread_queue_Operations _Thread_queue_Operations_priority = {
  .priority_change = _Thread_queue_Priority_priority_change,
  .initialize = _Thread_queue_Priority_initialize,
  .enqueue = _Thread_queue_Priority_enqueue,
  .extract = _Thread_queue_Priority_extract,
  .first = _Thread_queue_Priority_first
};