summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/condition.c
blob: fffe48ee30f6a9ce3db1ac5ff17a4a71085cd5e3 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSScore
 *
 * @brief This source file contains the implementation of
 *   _Condition_Wait(), _Condition_Wait_timed(), _Condition_Wait_recursive(),
 *   _Condition_Wait_recursive_timed(),
 *   _Condition_Wait_recursive_timed_ticks(), _Condition_Signal(), and
 *   _Condition_Broadcast().
 */

/*
 * Copyright (C) 2015, 2016 embedded brains GmbH & Co. KG
 *
 * 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.
 */

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

#include <sys/lock.h>
#include <errno.h>
#include <limits.h>

#include <rtems/score/atomic.h>
#include <rtems/score/chainimpl.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/threadqimpl.h>
#include <rtems/score/todimpl.h>
#include <rtems/score/watchdogimpl.h>

#define CONDITION_TQ_OPERATIONS &_Thread_queue_Operations_FIFO

typedef struct {
  Thread_queue_Syslock_queue Queue;
} Condition_Control;

RTEMS_STATIC_ASSERT(
  offsetof( Condition_Control, Queue )
    == offsetof( struct _Condition_Control, _Queue ),
  CONDITION_CONTROL_QUEUE
);

RTEMS_STATIC_ASSERT(
  sizeof( Condition_Control ) == sizeof( struct _Condition_Control ),
  CONDITION_CONTROL_SIZE
);

static Condition_Control *_Condition_Get(
  struct _Condition_Control *_condition
)
{
  return (Condition_Control *) _condition;
}

static Thread_Control *_Condition_Queue_acquire_critical(
  Condition_Control    *condition,
  Thread_queue_Context *queue_context
)
{
  Thread_Control *executing;

  executing = _Thread_Executing;
  _Thread_queue_Queue_acquire_critical(
    &condition->Queue.Queue,
    &executing->Potpourri_stats,
    &queue_context->Lock_context.Lock_context
  );

  return executing;
}

static void _Condition_Queue_release(
  Condition_Control    *condition,
  Thread_queue_Context *queue_context
)
{
  _Thread_queue_Queue_release(
    &condition->Queue.Queue,
    &queue_context->Lock_context.Lock_context
  );
}

typedef struct {
  Thread_queue_Context   Base;
  struct _Mutex_Control *mutex;
} Condition_Enqueue_context;

static void _Condition_Mutex_release( Thread_queue_Context *queue_context )
{
  Condition_Enqueue_context *context;

  context = (Condition_Enqueue_context *) queue_context;
  _Mutex_Release( context->mutex );
}

static void _Condition_Enqueue_no_timeout(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Per_CPU_Control      *cpu_self,
  Thread_queue_Context *queue_context
)
{
  _Condition_Mutex_release( queue_context );
}

static void _Condition_Enqueue_with_timeout(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Per_CPU_Control      *cpu_self,
  Thread_queue_Context *queue_context
)
{
  _Thread_queue_Add_timeout_realtime_timespec(
    queue,
    the_thread,
    cpu_self,
    queue_context
  );
  _Condition_Mutex_release( queue_context );
}

static Thread_Control *_Condition_Do_wait(
  struct _Condition_Control *_condition,
  struct _Mutex_Control     *_mutex,
  Condition_Enqueue_context *context
)
{
  Condition_Control *condition;
  Thread_Control    *executing;

  context->mutex = _mutex;
  condition = _Condition_Get( _condition );
  _ISR_lock_ISR_disable( &context->Base.Lock_context.Lock_context );
  executing = _Condition_Queue_acquire_critical( condition, &context->Base );
  _Thread_queue_Context_set_thread_state(
    &context->Base,
    STATES_WAITING_FOR_CONDITION_VARIABLE
  );
  _Thread_queue_Enqueue(
    &condition->Queue.Queue,
    CONDITION_TQ_OPERATIONS,
    executing,
    &context->Base
  );

  return executing;
}

void _Condition_Wait(
  struct _Condition_Control *_condition,
  struct _Mutex_Control     *_mutex
)
{
  Condition_Enqueue_context context;

  _Thread_queue_Context_initialize( &context.Base );
  _Thread_queue_Context_set_enqueue_callout(
    &context.Base,
    _Condition_Enqueue_no_timeout
  );
  _Condition_Do_wait( _condition, _mutex, &context );
  _Mutex_Acquire( _mutex );
}

int _Condition_Wait_timed(
  struct _Condition_Control *_condition,
  struct _Mutex_Control     *_mutex,
  const struct timespec     *abstime
)
{
  Condition_Enqueue_context  context;
  Thread_Control            *executing;
  int                        eno;

  _Thread_queue_Context_initialize( &context.Base );
  _Thread_queue_Context_set_enqueue_callout(
    &context.Base,
    _Condition_Enqueue_with_timeout
  );
  _Thread_queue_Context_set_timeout_argument( &context.Base, abstime, true );
  executing = _Condition_Do_wait( _condition, _mutex, &context );
  eno = STATUS_GET_POSIX( _Thread_Wait_get_status( executing ) );
  _Mutex_Acquire( _mutex );

  return eno;
}

static unsigned int _Condition_Unnest_mutex(
  struct _Mutex_recursive_Control *_mutex
)
{
  unsigned int nest_level;

  nest_level = _mutex->_nest_level;
  _mutex->_nest_level = 0;

  return nest_level;
}

void _Condition_Wait_recursive(
  struct _Condition_Control       *_condition,
  struct _Mutex_recursive_Control *_mutex
)
{
  Condition_Enqueue_context context;
  unsigned int              nest_level;

  _Thread_queue_Context_initialize( &context.Base );
  _Thread_queue_Context_set_enqueue_callout(
    &context.Base,
    _Condition_Enqueue_no_timeout
  );
  nest_level = _Condition_Unnest_mutex( _mutex );
  _Condition_Do_wait( _condition, &_mutex->_Mutex, &context );
  _Mutex_recursive_Acquire( _mutex );
  _mutex->_nest_level = nest_level;
}

int _Condition_Wait_recursive_timed(
  struct _Condition_Control       *_condition,
  struct _Mutex_recursive_Control *_mutex,
  const struct timespec           *abstime
)
{
  Condition_Enqueue_context  context;
  Thread_Control            *executing;
  int                        eno;
  unsigned int               nest_level;

  _Thread_queue_Context_initialize( &context.Base );
  _Thread_queue_Context_set_enqueue_callout(
    &context.Base,
    _Condition_Enqueue_with_timeout
  );
  _Thread_queue_Context_set_timeout_argument( &context.Base, abstime, true );
  nest_level = _Condition_Unnest_mutex( _mutex );
  executing = _Condition_Do_wait( _condition, &_mutex->_Mutex, &context );
  eno = STATUS_GET_POSIX( _Thread_Wait_get_status( executing ) );
  _Mutex_recursive_Acquire( _mutex );
  _mutex->_nest_level = nest_level;

  return eno;
}

typedef struct {
  Thread_queue_Context Base;
  int                  count;
} Condition_Flush_context;

static Thread_Control *_Condition_Flush_filter(
  Thread_Control       *the_thread,
  Thread_queue_Queue   *queue,
  Thread_queue_Context *queue_context
)
{
  Condition_Flush_context *context;

  context = (Condition_Flush_context *) queue_context;

  if ( context->count <= 0 ) {
    return NULL;
  }

  --context->count;

  return the_thread;
}

static void _Condition_Wake( struct _Condition_Control *_condition, int count )
{
  Condition_Control       *condition;
  Condition_Flush_context  context;

  condition = _Condition_Get( _condition );
  _Thread_queue_Context_initialize( &context.Base );
  _ISR_lock_ISR_disable( &context.Base.Lock_context.Lock_context );
  _Condition_Queue_acquire_critical( condition, &context.Base );

  /*
   * In common uses cases of condition variables there are normally no threads
   * on the queue, so check this condition early.
   */
  if (
    RTEMS_PREDICT_TRUE( _Thread_queue_Is_empty( &condition->Queue.Queue ) )
  ) {
    _Condition_Queue_release( condition, &context.Base );
    return;
  }

  context.count = count;
  _Thread_queue_Flush_critical(
    &condition->Queue.Queue,
    CONDITION_TQ_OPERATIONS,
    _Condition_Flush_filter,
    &context.Base
  );
}

void _Condition_Signal( struct _Condition_Control *_condition )
{
  _Condition_Wake( _condition, 1 );
}

void _Condition_Broadcast( struct _Condition_Control *_condition )
{
  _Condition_Wake( _condition, INT_MAX );
}