summaryrefslogtreecommitdiffstats
path: root/testsuites/validation/tc-sys-lock.c
blob: 85b290ab9bf7e65e240ae2d6d2ff002aeadc5fad (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSTestCaseNewlibValSysLock
 */

/*
 * Copyright (C) 2021 embedded brains GmbH (http://www.embedded-brains.de)
 *
 * 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.
 */

/*
 * This file is part of the RTEMS quality process and was automatically
 * generated.  If you find something that needs to be fixed or
 * worded better please post a report or patch to an RTEMS mailing list
 * or raise a bug report:
 *
 * https://www.rtems.org/bugs.html
 *
 * For information on updating and regenerating please refer to the How-To
 * section in the Software Requirements Engineering chapter of the
 * RTEMS Software Engineering manual.  The manual is provided as a part of
 * a release.  For development sources please refer to the online
 * documentation at:
 *
 * https://docs.rtems.org
 */

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

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

#include "tr-mtx-seize-try.h"
#include "tr-mtx-seize-wait.h"
#include "tr-mtx-surrender.h"
#include "tr-tq-timeout-priority-inherit.h"
#include "tx-thread-queue.h"

#include <rtems/test.h>

/**
 * @defgroup RTEMSTestCaseNewlibValSysLock spec:/newlib/val/sys-lock
 *
 * @ingroup RTEMSTestSuiteTestsuitesValidationNoClock0
 *
 * @brief Tests the <sys/lock.h> mutex directives.
 *
 * This test case performs the following actions:
 *
 * - Create a mutex and validate the mutex directives.
 *
 *   - Validate the _Mutex_Try_acquire() directive.
 *
 *   - Validate the _Mutex_Acquire_timed() directive for valid timeout
 *     parameters.
 *
 *   - Validate the _Mutex_Acquire_timed() directive for an invalid timeout
 *     parameter.
 *
 *   - Validate the _Mutex_Acquire_timed() timeout behaviour.
 *
 *   - Validate the _Mutex_Acquire() directive.
 *
 *   - Validate the _Mutex_Try_acquire() directive.
 *
 *   - Destroy the mutex.
 *
 * - Create a recursive mutex and validate the mutex directives.
 *
 *   - Validate the _Mutex_recursive_Try_acquire() directive.
 *
 *   - Validate the _Mutex_recursive_Acquire_timed() directive for valid
 *     timeout parameters.
 *
 *   - Validate the _Mutex_recursive_Acquire_timed() directive for an invalid
 *     timeout parameter.
 *
 *   - Validate the _Mutex_recursive_Acquire_timed() timeout behaviour.
 *
 *   - Validate the _Mutex_recursive_Acquire() directive.
 *
 *   - Validate the _Mutex_recursive_Try_acquire() directive.
 *
 *   - Destroy the mutex.
 *
 * @{
 */

/**
 * @brief Test context for spec:/newlib/val/sys-lock test case.
 */
typedef struct {
  /**
   * @brief This member contains the thread queue test context.
   */
  TQMtxContext tq_mtx_ctx;
} NewlibValSysLock_Context;

static NewlibValSysLock_Context
  NewlibValSysLock_Instance;

static Status_Control Enqueue( TQContext *ctx, TQWait wait )
{
  const struct timespec abstime = {
    .tv_sec = INT64_MAX,
    .tv_nsec = 0
  };
  int                   eno;

  switch ( wait ) {
    case TQ_NO_WAIT:
      eno = _Mutex_Try_acquire( ctx->thread_queue_object );
      break;
    case TQ_WAIT_FOREVER:
      _Mutex_Acquire( ctx->thread_queue_object );
      eno = 0;
      break;
    case TQ_WAIT_TIMED:
      eno = _Mutex_Acquire_timed( ctx->thread_queue_object, &abstime );
      break;
    default:
      T_unreachable();
      break;
  }

  return STATUS_BUILD( 0, eno );
}

static Status_Control Surrender( TQContext *ctx )
{
  _Mutex_Release( ctx->thread_queue_object );

  return STATUS_SUCCESSFUL;
}

static rtems_tcb *GetOwner( TQContext *ctx )
{
  const struct _Mutex_Control *mutex;

  mutex = ctx->thread_queue_object;

  return mutex->_Queue._owner;
}

static Status_Control RecursiveEnqueue( TQContext *ctx, TQWait wait )
{
  const struct timespec abstime = {
    .tv_sec = INT64_MAX,
    .tv_nsec = 0
  };
  int                   eno;

  switch ( wait ) {
    case TQ_NO_WAIT:
      eno = _Mutex_recursive_Try_acquire( ctx->thread_queue_object );
      break;
    case TQ_WAIT_FOREVER:
      _Mutex_recursive_Acquire( ctx->thread_queue_object );
      eno = 0;
      break;
    case TQ_WAIT_TIMED:
      eno = _Mutex_recursive_Acquire_timed(
        ctx->thread_queue_object,
        &abstime
      );
      break;
    default:
      T_unreachable();
      break;
  }

  return STATUS_BUILD( 0, eno );
}

static Status_Control RecursiveSurrender( TQContext *ctx )
{
  _Mutex_recursive_Release( ctx->thread_queue_object );

  return STATUS_SUCCESSFUL;
}

static rtems_tcb *RecursiveGetOwner( TQContext *ctx )
{
  const struct _Mutex_recursive_Control *mutex;

  mutex = ctx->thread_queue_object;

  return mutex->_Mutex._Queue._owner;
}

static void NewlibValSysLock_Setup( NewlibValSysLock_Context *ctx )
{
  memset( ctx, 0, sizeof( *ctx ) );
  ctx->tq_mtx_ctx.base.enqueue_variant = TQ_ENQUEUE_BLOCKS;
  ctx->tq_mtx_ctx.base.discipline = TQ_PRIORITY;
  ctx->tq_mtx_ctx.base.deadlock = TQ_DEADLOCK_FATAL;
  ctx->tq_mtx_ctx.base.convert_status = TQConvertStatusPOSIX;
  ctx->tq_mtx_ctx.protocol = TQ_MTX_PRIORITY_INHERIT;
  ctx->tq_mtx_ctx.owner_check = TQ_MTX_NO_OWNER_CHECK;
  ctx->tq_mtx_ctx.priority_ceiling = PRIO_INVALID;
  TQInitialize( &ctx->tq_mtx_ctx.base );
}

static void NewlibValSysLock_Setup_Wrap( void *arg )
{
  NewlibValSysLock_Context *ctx;

  ctx = arg;
  NewlibValSysLock_Setup( ctx );
}

static void NewlibValSysLock_Teardown( NewlibValSysLock_Context *ctx )
{
  TQDestroy( &ctx->tq_mtx_ctx.base );
  RestoreRunnerPriority();
}

static void NewlibValSysLock_Teardown_Wrap( void *arg )
{
  NewlibValSysLock_Context *ctx;

  ctx = arg;
  NewlibValSysLock_Teardown( ctx );
}

static T_fixture NewlibValSysLock_Fixture = {
  .setup = NewlibValSysLock_Setup_Wrap,
  .stop = NULL,
  .teardown = NewlibValSysLock_Teardown_Wrap,
  .scope = NULL,
  .initial_context = &NewlibValSysLock_Instance
};

/**
 * @brief Create a mutex and validate the mutex directives.
 */
static void NewlibValSysLock_Action_0( NewlibValSysLock_Context *ctx )
{
  const struct timespec invalid_abstime = {
    .tv_sec = -1,
    .tv_nsec = -1
  };
  int                   eno;
  struct _Mutex_Control mutex;

  _Mutex_Initialize( &mutex );

  ctx->tq_mtx_ctx.base.thread_queue_object = &mutex;
  ctx->tq_mtx_ctx.base.enqueue_prepare = TQEnqueuePrepareDefault;
  ctx->tq_mtx_ctx.base.enqueue_done = TQEnqueueDoneDefault;
  ctx->tq_mtx_ctx.base.enqueue = Enqueue;
  ctx->tq_mtx_ctx.base.surrender = Surrender;
  ctx->tq_mtx_ctx.base.get_owner = GetOwner;

  /*
   * Validate the _Mutex_Try_acquire() directive.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_NO_WAIT;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_UNAVAILABLE;
  ScoreMtxReqSeizeTry_Run( &ctx->tq_mtx_ctx );

  /*
   * Validate the _Mutex_Acquire_timed() directive for valid timeout
   * parameters.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_WAIT_TIMED;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_DEADLOCK;
  ScoreMtxReqSeizeWait_Run( &ctx->tq_mtx_ctx );

  /*
   * Validate the _Mutex_Acquire_timed() directive for an invalid timeout
   * parameter.
   */
  TQSetScheduler(
    &ctx->tq_mtx_ctx.base,
    TQ_HELPER_A,
    SCHEDULER_A_ID,
    PRIO_HIGH
  );
  TQSend( &ctx->tq_mtx_ctx.base, TQ_HELPER_A, TQ_EVENT_ENQUEUE );
  eno = _Mutex_Acquire_timed( &mutex, &invalid_abstime );
  T_eq_int( eno, EINVAL );
  TQSend( &ctx->tq_mtx_ctx.base, TQ_HELPER_A, TQ_EVENT_SURRENDER );

  /*
   * Validate the _Mutex_Acquire_timed() timeout behaviour.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_WAIT_FOREVER;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_DEADLOCK;
  ScoreTqReqTimeoutPriorityInherit_Run(
    &ctx->tq_mtx_ctx.base
  );

  /*
   * Validate the _Mutex_Acquire() directive.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_WAIT_FOREVER;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_DEADLOCK;
  ScoreMtxReqSeizeWait_Run( &ctx->tq_mtx_ctx );

  /*
   * Validate the _Mutex_Try_acquire() directive.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_WAIT_FOREVER;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_DEADLOCK;
  ScoreMtxReqSurrender_Run( &ctx->tq_mtx_ctx );

  /*
   * Destroy the mutex.
   */
  _Mutex_Destroy( &mutex );
}

/**
 * @brief Create a recursive mutex and validate the mutex directives.
 */
static void NewlibValSysLock_Action_1( NewlibValSysLock_Context *ctx )
{
  const struct timespec invalid_abstime = {
    .tv_sec = -1,
    .tv_nsec = -1
  };
  int                             eno;
  struct _Mutex_recursive_Control mutex;

  _Mutex_recursive_Initialize( &mutex );

  ctx->tq_mtx_ctx.base.thread_queue_object = &mutex;
  ctx->tq_mtx_ctx.base.enqueue_prepare = TQEnqueuePrepareDefault;
  ctx->tq_mtx_ctx.base.enqueue_done = TQEnqueueDoneDefault;
  ctx->tq_mtx_ctx.base.enqueue = RecursiveEnqueue;
  ctx->tq_mtx_ctx.base.surrender = RecursiveSurrender;
  ctx->tq_mtx_ctx.base.get_owner = RecursiveGetOwner;

  /*
   * Validate the _Mutex_recursive_Try_acquire() directive.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_NO_WAIT;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_ALLOWED;
  ScoreMtxReqSeizeTry_Run( &ctx->tq_mtx_ctx );

  /*
   * Validate the _Mutex_recursive_Acquire_timed() directive for valid timeout
   * parameters.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_WAIT_TIMED;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_ALLOWED;
  ScoreMtxReqSeizeWait_Run( &ctx->tq_mtx_ctx );

  /*
   * Validate the _Mutex_recursive_Acquire_timed() directive for an invalid
   * timeout parameter.
   */
  TQSetScheduler(
    &ctx->tq_mtx_ctx.base,
    TQ_HELPER_A,
    SCHEDULER_A_ID,
    PRIO_HIGH
  );
  TQSend( &ctx->tq_mtx_ctx.base, TQ_HELPER_A, TQ_EVENT_ENQUEUE );
  eno = _Mutex_recursive_Acquire_timed( &mutex, &invalid_abstime );
  T_eq_int( eno, EINVAL );
  TQSend( &ctx->tq_mtx_ctx.base, TQ_HELPER_A, TQ_EVENT_SURRENDER );

  /*
   * Validate the _Mutex_recursive_Acquire_timed() timeout behaviour.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_WAIT_FOREVER;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_ALLOWED;
  ScoreTqReqTimeoutPriorityInherit_Run(
    &ctx->tq_mtx_ctx.base
  );

  /*
   * Validate the _Mutex_recursive_Acquire() directive.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_WAIT_FOREVER;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_ALLOWED;
  ScoreMtxReqSeizeWait_Run( &ctx->tq_mtx_ctx );

  /*
   * Validate the _Mutex_recursive_Try_acquire() directive.
   */
  ctx->tq_mtx_ctx.base.wait = TQ_WAIT_FOREVER;
  ctx->tq_mtx_ctx.recursive = TQ_MTX_RECURSIVE_ALLOWED;
  ScoreMtxReqSurrender_Run( &ctx->tq_mtx_ctx );

  /*
   * Destroy the mutex.
   */
  _Mutex_recursive_Destroy( &mutex );
}

/**
 * @fn void T_case_body_NewlibValSysLock( void )
 */
T_TEST_CASE_FIXTURE( NewlibValSysLock, &NewlibValSysLock_Fixture )
{
  NewlibValSysLock_Context *ctx;

  ctx = T_fixture_context();

  NewlibValSysLock_Action_0( ctx );
  NewlibValSysLock_Action_1( ctx );
}

/** @} */