1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 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 <errno.h> |
---|
33 | #include <pthread.h> |
---|
34 | |
---|
35 | #include <rtems.h> |
---|
36 | #include <rtems/libcsupport.h> |
---|
37 | |
---|
38 | #include "tmacros.h" |
---|
39 | |
---|
40 | const char rtems_test_name[] = "SMPPSXMUTEX 1"; |
---|
41 | |
---|
42 | #define SCHED_A rtems_build_name(' ', ' ', ' ', 'A') |
---|
43 | |
---|
44 | #define SCHED_B rtems_build_name(' ', ' ', ' ', 'B') |
---|
45 | |
---|
46 | typedef struct { |
---|
47 | pthread_t thread_b; |
---|
48 | pthread_mutexattr_t mtx_attr; |
---|
49 | pthread_mutex_t mtx_a; |
---|
50 | pthread_mutex_t mtx_b; |
---|
51 | } test_context; |
---|
52 | |
---|
53 | static test_context test_instance; |
---|
54 | |
---|
55 | static void *thread_b(void *arg) |
---|
56 | { |
---|
57 | test_context *ctx; |
---|
58 | int prio_ceiling; |
---|
59 | int eno; |
---|
60 | |
---|
61 | ctx = arg; |
---|
62 | |
---|
63 | rtems_test_assert(rtems_scheduler_get_processor() == 1); |
---|
64 | |
---|
65 | eno = pthread_mutex_init(&ctx->mtx_b, &ctx->mtx_attr); |
---|
66 | rtems_test_assert(eno == 0); |
---|
67 | |
---|
68 | eno = pthread_mutex_getprioceiling(&ctx->mtx_b, &prio_ceiling); |
---|
69 | rtems_test_assert(eno == 0); |
---|
70 | rtems_test_assert(prio_ceiling == 254); |
---|
71 | |
---|
72 | eno = pthread_mutex_lock(&ctx->mtx_b); |
---|
73 | rtems_test_assert(eno == 0); |
---|
74 | |
---|
75 | eno = pthread_mutex_unlock(&ctx->mtx_b); |
---|
76 | rtems_test_assert(eno == 0); |
---|
77 | |
---|
78 | eno = pthread_mutex_destroy(&ctx->mtx_b); |
---|
79 | rtems_test_assert(eno == 0); |
---|
80 | |
---|
81 | eno = pthread_mutex_getprioceiling(&ctx->mtx_a, &prio_ceiling); |
---|
82 | rtems_test_assert(eno == 0); |
---|
83 | rtems_test_assert(prio_ceiling == 126); |
---|
84 | |
---|
85 | eno = pthread_mutex_lock(&ctx->mtx_a); |
---|
86 | rtems_test_assert(eno == EINVAL); |
---|
87 | |
---|
88 | return ctx; |
---|
89 | } |
---|
90 | |
---|
91 | static void test(test_context *ctx) |
---|
92 | { |
---|
93 | uint32_t cpu_count; |
---|
94 | int prio_ceiling; |
---|
95 | int eno; |
---|
96 | |
---|
97 | cpu_count = rtems_scheduler_get_processor_maximum(); |
---|
98 | |
---|
99 | rtems_test_assert(rtems_scheduler_get_processor() == 0); |
---|
100 | |
---|
101 | eno = pthread_mutexattr_init(&ctx->mtx_attr); |
---|
102 | rtems_test_assert(eno == 0); |
---|
103 | |
---|
104 | eno = pthread_mutexattr_setprotocol(&ctx->mtx_attr, PTHREAD_PRIO_PROTECT); |
---|
105 | rtems_test_assert(eno == 0); |
---|
106 | |
---|
107 | eno = pthread_mutex_init(&ctx->mtx_a, &ctx->mtx_attr); |
---|
108 | rtems_test_assert(eno == 0); |
---|
109 | |
---|
110 | eno = pthread_mutex_getprioceiling(&ctx->mtx_a, &prio_ceiling); |
---|
111 | rtems_test_assert(eno == 0); |
---|
112 | rtems_test_assert(prio_ceiling == 126); |
---|
113 | |
---|
114 | eno = pthread_mutex_lock(&ctx->mtx_a); |
---|
115 | rtems_test_assert(eno == 0); |
---|
116 | |
---|
117 | eno = pthread_mutex_unlock(&ctx->mtx_a); |
---|
118 | rtems_test_assert(eno == 0); |
---|
119 | |
---|
120 | if (cpu_count > 1) { |
---|
121 | rtems_id scheduler_a_id; |
---|
122 | rtems_id scheduler_b_id; |
---|
123 | rtems_status_code sc; |
---|
124 | rtems_task_priority prio; |
---|
125 | void *exit_code; |
---|
126 | |
---|
127 | sc = rtems_scheduler_ident(SCHED_A, &scheduler_a_id); |
---|
128 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
129 | |
---|
130 | sc = rtems_scheduler_ident(SCHED_B, &scheduler_b_id); |
---|
131 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
132 | |
---|
133 | sc = rtems_task_set_priority(pthread_self(), RTEMS_CURRENT_PRIORITY, &prio); |
---|
134 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
135 | |
---|
136 | sc = rtems_task_set_scheduler(pthread_self(), scheduler_b_id, prio); |
---|
137 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
138 | |
---|
139 | eno = pthread_create(&ctx->thread_b, NULL, thread_b, ctx); |
---|
140 | rtems_test_assert(eno == 0); |
---|
141 | |
---|
142 | exit_code = NULL; |
---|
143 | eno = pthread_join(ctx->thread_b, &exit_code); |
---|
144 | rtems_test_assert(eno == 0); |
---|
145 | rtems_test_assert(exit_code == ctx); |
---|
146 | |
---|
147 | sc = rtems_task_set_scheduler(pthread_self(), scheduler_a_id, prio); |
---|
148 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
149 | } |
---|
150 | |
---|
151 | eno = pthread_mutex_destroy(&ctx->mtx_a); |
---|
152 | rtems_test_assert(eno == 0); |
---|
153 | |
---|
154 | eno = pthread_mutexattr_destroy(&ctx->mtx_attr); |
---|
155 | rtems_test_assert(eno == 0); |
---|
156 | } |
---|
157 | |
---|
158 | static void *POSIX_Init(void *arg) |
---|
159 | { |
---|
160 | rtems_resource_snapshot snapshot; |
---|
161 | |
---|
162 | TEST_BEGIN(); |
---|
163 | |
---|
164 | rtems_resource_snapshot_take(&snapshot); |
---|
165 | |
---|
166 | test(&test_instance); |
---|
167 | |
---|
168 | rtems_test_assert(rtems_resource_snapshot_check(&snapshot)); |
---|
169 | |
---|
170 | TEST_END(); |
---|
171 | rtems_test_exit(0); |
---|
172 | } |
---|
173 | |
---|
174 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
175 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
176 | |
---|
177 | #define CONFIGURE_MAXIMUM_POSIX_THREADS 2 |
---|
178 | |
---|
179 | #define CONFIGURE_MAXIMUM_PROCESSORS 2 |
---|
180 | |
---|
181 | #define CONFIGURE_SCHEDULER_PRIORITY_SMP |
---|
182 | |
---|
183 | #include <rtems/scheduler.h> |
---|
184 | |
---|
185 | RTEMS_SCHEDULER_PRIORITY_SMP(a, 128); |
---|
186 | |
---|
187 | RTEMS_SCHEDULER_PRIORITY_SMP(b, 256); |
---|
188 | |
---|
189 | #define CONFIGURE_SCHEDULER_TABLE_ENTRIES \ |
---|
190 | RTEMS_SCHEDULER_TABLE_PRIORITY_SMP(a, SCHED_A), \ |
---|
191 | RTEMS_SCHEDULER_TABLE_PRIORITY_SMP(b, SCHED_B) \ |
---|
192 | |
---|
193 | #define CONFIGURE_SCHEDULER_ASSIGNMENTS \ |
---|
194 | RTEMS_SCHEDULER_ASSIGN(0, RTEMS_SCHEDULER_ASSIGN_PROCESSOR_MANDATORY), \ |
---|
195 | RTEMS_SCHEDULER_ASSIGN(1, RTEMS_SCHEDULER_ASSIGN_PROCESSOR_OPTIONAL) |
---|
196 | |
---|
197 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
198 | |
---|
199 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
200 | |
---|
201 | #define CONFIGURE_INIT |
---|
202 | |
---|
203 | #include <rtems/confdefs.h> |
---|