source: rtems/testsuites/smptests/smppsxsignal01/init.c @ 98c6d50

5
Last change on this file since 98c6d50 was 98c6d50, checked in by Chris Johns <chrisj@…>, on 10/19/17 at 05:39:16

testsuite: Use printk for all test output where possible.

  • Remove the printf support leaving the direct printk support configured with TESTS_USE_PRINTK and all other output goes via a buffered vsniprintf call to printk.
  • Control the test's single init for functions and global data with TEST_INIT and not CONFIGURE_INIT. They are now separate.

Updates #3170.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#define TEST_INIT
20
21#include "tmacros.h"
22
23#include <pthread.h>
24#include <signal.h>
25
26const char rtems_test_name[] = "SMPPSXSIGNAL 1";
27
28#define TEST_SIGNAL SIGUSR1
29
30typedef enum {
31  SIG_READY,
32  SIG_SENT,
33  SIG_PROCESSED
34} test_state;
35
36typedef struct {
37  test_state state;
38  pthread_t consumer;
39  pthread_t producer;
40  uint32_t consumer_processor;
41  uint32_t producer_processor;
42} test_context;
43
44static void change_state(test_context *ctx, test_state new_state)
45{
46  ctx->state = new_state;
47  _CPU_SMP_Processor_event_broadcast();
48}
49
50static void wait_for_state(const test_context *ctx, test_state desired_state)
51{
52  while ( ctx->state != desired_state ) {
53    _CPU_SMP_Processor_event_receive();
54  }
55}
56
57static test_context ctx_instance = {
58  .state = SIG_READY
59};
60
61static void signal_handler(int signum)
62{
63  test_context *ctx = &ctx_instance;
64
65  switch (ctx->state) {
66    case SIG_SENT:
67      change_state(ctx, SIG_PROCESSED);
68      break;
69    default:
70      rtems_test_assert(0);
71  }
72}
73
74static void signal_send(test_context *ctx, test_state new_state)
75{
76  int eno;
77
78  eno = pthread_kill(ctx->consumer, TEST_SIGNAL);
79  rtems_test_assert(eno == 0);
80
81  change_state(ctx, new_state);
82}
83
84static void check_consumer_processor(const test_context *ctx)
85{
86  rtems_test_assert(
87    ctx->consumer_processor == rtems_get_current_processor()
88  );
89}
90
91static void check_producer_processor(const test_context *ctx)
92{
93  rtems_test_assert(
94    ctx->producer_processor == rtems_get_current_processor()
95  );
96}
97
98static void *producer(void *arg)
99{
100  test_context *ctx = arg;
101
102  ctx->producer_processor = rtems_get_current_processor();
103
104  rtems_test_assert(ctx->consumer_processor != ctx->producer_processor);
105
106  wait_for_state(ctx, SIG_READY);
107  signal_send(ctx, SIG_SENT);
108
109  check_producer_processor(ctx);
110
111  return ctx;
112}
113
114static void test(void)
115{
116  test_context *ctx = &ctx_instance;
117  struct sigaction new_action;
118  sigset_t test_signal_set;
119  int rv;
120  pthread_attr_t attr;
121  int eno;
122  void *producer_status;
123
124  ctx->consumer = pthread_self();
125  ctx->consumer_processor = rtems_get_current_processor();
126
127  memset(&new_action, 0, sizeof(new_action));
128  new_action.sa_handler = signal_handler;
129
130  rv = sigaction(TEST_SIGNAL, &new_action, NULL);
131  rtems_test_assert(rv == 0);
132
133  rv = sigemptyset(&test_signal_set);
134  rtems_test_assert(rv == 0);
135
136  rv = sigaddset(&test_signal_set, TEST_SIGNAL);
137  rtems_test_assert(rv == 0);
138
139  eno = pthread_sigmask(SIG_UNBLOCK, &test_signal_set, NULL);
140  rtems_test_assert(eno == 0);
141
142  eno = pthread_attr_init(&attr);
143  rtems_test_assert(eno == 0);
144
145  eno = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
146  rtems_test_assert(eno == 0);
147
148  eno = pthread_create(&ctx->producer, &attr, producer, ctx);
149  rtems_test_assert(eno == 0);
150
151  eno = pthread_attr_destroy(&attr);
152  rtems_test_assert(eno == 0);
153
154  check_consumer_processor(ctx);
155
156  wait_for_state(ctx, SIG_PROCESSED);
157
158  check_consumer_processor(ctx);
159
160  producer_status = NULL;
161  pthread_join(ctx->producer, &producer_status);
162  rtems_test_assert(eno == 0);
163  rtems_test_assert(producer_status == ctx);
164}
165
166static void *POSIX_Init(void *arg)
167{
168  TEST_BEGIN();
169
170  if (rtems_get_processor_count() >= 2) {
171    test();
172  }
173
174  TEST_END();
175  rtems_test_exit(0);
176}
177
178#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
179#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
180
181#define CONFIGURE_MAXIMUM_PROCESSORS 2
182
183#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
184
185#define CONFIGURE_MAXIMUM_POSIX_THREADS 2
186
187#define CONFIGURE_POSIX_INIT_THREAD_TABLE
188
189#define CONFIGURE_INIT
190
191#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.