1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 2013 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 | |
---|
34 | #include <pthread.h> |
---|
35 | #include <signal.h> |
---|
36 | |
---|
37 | const char rtems_test_name[] = "SMPPSXSIGNAL 1"; |
---|
38 | |
---|
39 | #define TEST_SIGNAL SIGUSR1 |
---|
40 | |
---|
41 | typedef enum { |
---|
42 | SIG_READY, |
---|
43 | SIG_SENT, |
---|
44 | SIG_PROCESSED |
---|
45 | } test_state; |
---|
46 | |
---|
47 | typedef struct { |
---|
48 | volatile test_state state; |
---|
49 | pthread_t consumer; |
---|
50 | pthread_t producer; |
---|
51 | uint32_t consumer_processor; |
---|
52 | uint32_t producer_processor; |
---|
53 | } test_context; |
---|
54 | |
---|
55 | static void change_state(test_context *ctx, test_state new_state) |
---|
56 | { |
---|
57 | ctx->state = new_state; |
---|
58 | } |
---|
59 | |
---|
60 | static void wait_for_state(const test_context *ctx, test_state desired_state) |
---|
61 | { |
---|
62 | while ( ctx->state != desired_state ) { |
---|
63 | /* Wait */ |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | static test_context ctx_instance = { |
---|
68 | .state = SIG_READY |
---|
69 | }; |
---|
70 | |
---|
71 | static void signal_handler(int signum) |
---|
72 | { |
---|
73 | test_context *ctx = &ctx_instance; |
---|
74 | |
---|
75 | switch (ctx->state) { |
---|
76 | case SIG_SENT: |
---|
77 | change_state(ctx, SIG_PROCESSED); |
---|
78 | break; |
---|
79 | default: |
---|
80 | rtems_test_assert(0); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | static void signal_send(test_context *ctx, test_state new_state) |
---|
85 | { |
---|
86 | int eno; |
---|
87 | |
---|
88 | eno = pthread_kill(ctx->consumer, TEST_SIGNAL); |
---|
89 | rtems_test_assert(eno == 0); |
---|
90 | |
---|
91 | change_state(ctx, new_state); |
---|
92 | } |
---|
93 | |
---|
94 | static void check_consumer_processor(const test_context *ctx) |
---|
95 | { |
---|
96 | rtems_test_assert( |
---|
97 | ctx->consumer_processor == rtems_scheduler_get_processor() |
---|
98 | ); |
---|
99 | } |
---|
100 | |
---|
101 | static void check_producer_processor(const test_context *ctx) |
---|
102 | { |
---|
103 | rtems_test_assert( |
---|
104 | ctx->producer_processor == rtems_scheduler_get_processor() |
---|
105 | ); |
---|
106 | } |
---|
107 | |
---|
108 | static void *producer(void *arg) |
---|
109 | { |
---|
110 | test_context *ctx = arg; |
---|
111 | |
---|
112 | ctx->producer_processor = rtems_scheduler_get_processor(); |
---|
113 | |
---|
114 | rtems_test_assert(ctx->consumer_processor != ctx->producer_processor); |
---|
115 | |
---|
116 | wait_for_state(ctx, SIG_READY); |
---|
117 | signal_send(ctx, SIG_SENT); |
---|
118 | |
---|
119 | check_producer_processor(ctx); |
---|
120 | |
---|
121 | return ctx; |
---|
122 | } |
---|
123 | |
---|
124 | static void test(void) |
---|
125 | { |
---|
126 | test_context *ctx = &ctx_instance; |
---|
127 | struct sigaction new_action; |
---|
128 | sigset_t test_signal_set; |
---|
129 | int rv; |
---|
130 | pthread_attr_t attr; |
---|
131 | int eno; |
---|
132 | void *producer_status; |
---|
133 | |
---|
134 | ctx->consumer = pthread_self(); |
---|
135 | ctx->consumer_processor = rtems_scheduler_get_processor(); |
---|
136 | |
---|
137 | memset(&new_action, 0, sizeof(new_action)); |
---|
138 | new_action.sa_handler = signal_handler; |
---|
139 | |
---|
140 | rv = sigaction(TEST_SIGNAL, &new_action, NULL); |
---|
141 | rtems_test_assert(rv == 0); |
---|
142 | |
---|
143 | rv = sigemptyset(&test_signal_set); |
---|
144 | rtems_test_assert(rv == 0); |
---|
145 | |
---|
146 | rv = sigaddset(&test_signal_set, TEST_SIGNAL); |
---|
147 | rtems_test_assert(rv == 0); |
---|
148 | |
---|
149 | eno = pthread_sigmask(SIG_UNBLOCK, &test_signal_set, NULL); |
---|
150 | rtems_test_assert(eno == 0); |
---|
151 | |
---|
152 | eno = pthread_attr_init(&attr); |
---|
153 | rtems_test_assert(eno == 0); |
---|
154 | |
---|
155 | eno = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
---|
156 | rtems_test_assert(eno == 0); |
---|
157 | |
---|
158 | eno = pthread_create(&ctx->producer, &attr, producer, ctx); |
---|
159 | rtems_test_assert(eno == 0); |
---|
160 | |
---|
161 | eno = pthread_attr_destroy(&attr); |
---|
162 | rtems_test_assert(eno == 0); |
---|
163 | |
---|
164 | check_consumer_processor(ctx); |
---|
165 | |
---|
166 | wait_for_state(ctx, SIG_PROCESSED); |
---|
167 | |
---|
168 | check_consumer_processor(ctx); |
---|
169 | |
---|
170 | producer_status = NULL; |
---|
171 | pthread_join(ctx->producer, &producer_status); |
---|
172 | rtems_test_assert(eno == 0); |
---|
173 | rtems_test_assert(producer_status == ctx); |
---|
174 | } |
---|
175 | |
---|
176 | static void *POSIX_Init(void *arg) |
---|
177 | { |
---|
178 | TEST_BEGIN(); |
---|
179 | |
---|
180 | if (rtems_scheduler_get_processor_maximum() >= 2) { |
---|
181 | test(); |
---|
182 | } |
---|
183 | |
---|
184 | TEST_END(); |
---|
185 | rtems_test_exit(0); |
---|
186 | } |
---|
187 | |
---|
188 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
189 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
190 | |
---|
191 | #define CONFIGURE_MAXIMUM_PROCESSORS 2 |
---|
192 | |
---|
193 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
194 | |
---|
195 | #define CONFIGURE_MAXIMUM_POSIX_THREADS 2 |
---|
196 | |
---|
197 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
198 | |
---|
199 | #define CONFIGURE_INIT |
---|
200 | |
---|
201 | #include <rtems/confdefs.h> |
---|