source: rtems/testsuites/sptests/spsem03/init.c

Last change on this file was bcef89f2, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/23 at 06:18:25

Update company name

The embedded brains GmbH & Co. KG is the legal successor of embedded
brains GmbH.

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Copyright (C) 2014, 2016 embedded brains GmbH & Co. KG
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "tmacros.h"
33
34const char rtems_test_name[] = "SPSEM 3";
35
36typedef struct {
37  rtems_id low;
38  rtems_id mid;
39  rtems_id high;
40  rtems_id inversion;
41  rtems_id sem_a;
42  rtems_id sem_b;
43} test_context;
44
45static test_context test_instance;
46
47static void assert_prio(rtems_id task_id, rtems_task_priority expected_prio)
48{
49  rtems_status_code sc;
50  rtems_task_priority prio;
51
52  sc = rtems_task_set_priority(task_id, RTEMS_CURRENT_PRIORITY, &prio);
53  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
54  rtems_test_assert(prio == expected_prio);
55}
56
57static void create_task(rtems_id *id, rtems_task_priority prio)
58{
59  rtems_status_code sc;
60
61  sc = rtems_task_create(
62    rtems_build_name('T', 'A', 'S', 'K'),
63    prio,
64    RTEMS_MINIMUM_STACK_SIZE,
65    RTEMS_DEFAULT_MODES,
66    RTEMS_DEFAULT_ATTRIBUTES,
67    id
68  );
69  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
70}
71
72static void start_task(rtems_id id, rtems_task_entry entry)
73{
74  rtems_status_code sc;
75
76  sc = rtems_task_start(id, entry, 0);
77  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
78}
79
80static void create_sema(rtems_id *id)
81{
82  rtems_status_code sc;
83
84  sc = rtems_semaphore_create(
85    rtems_build_name('S', 'E', 'M', 'A'),
86    1,
87    RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
88    0,
89    id
90  );
91  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
92}
93
94static void obtain_sema(rtems_id id)
95{
96  rtems_status_code sc;
97
98  sc = rtems_semaphore_obtain(id, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
99  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
100}
101
102static void inversion_task(rtems_task_argument arg)
103{
104  rtems_test_assert(0);
105}
106
107static void mid_task(rtems_task_argument arg)
108{
109  test_context *ctx = &test_instance;
110
111  obtain_sema(ctx->sem_b);
112  obtain_sema(ctx->sem_a);
113}
114
115static void high_task(rtems_task_argument arg)
116{
117  test_context *ctx = &test_instance;
118
119  start_task(ctx->inversion, inversion_task);
120  obtain_sema(ctx->sem_b);
121}
122
123static void Init(rtems_task_argument arg)
124{
125  test_context *ctx = &test_instance;
126
127  TEST_BEGIN();
128
129  ctx->low = rtems_task_self();
130
131  create_task(&ctx->mid, 3);
132  create_task(&ctx->high, 1);
133  create_task(&ctx->inversion, 2);
134  create_sema(&ctx->sem_a);
135  create_sema(&ctx->sem_b);
136
137  obtain_sema(ctx->sem_a);
138  start_task(ctx->mid, mid_task);
139  start_task(ctx->high, high_task);
140
141  /*
142   * Here we see that the priority of the high priority task blocked on
143   * semaphore B propagated to the low priority task owning semaphore A
144   * on which the owner of semaphore B depends.
145   */
146  assert_prio(ctx->low, 1);
147  assert_prio(ctx->mid, 1);
148  assert_prio(ctx->high, 1);
149  assert_prio(ctx->inversion, 2);
150
151  TEST_END();
152  rtems_test_exit(0);
153}
154
155#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
156#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
157
158#define CONFIGURE_MAXIMUM_TASKS 4
159#define CONFIGURE_MAXIMUM_SEMAPHORES 2
160
161#define CONFIGURE_INIT_TASK_PRIORITY 4
162#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
163
164#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
165
166#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
167
168#define CONFIGURE_INIT
169
170#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.