summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/spintrcritical21/init.c
blob: 5b1ecb28623cfd1c05edacbc55c94ca2820f5c6c (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
/*
 *  Classic API Signal to Task from ISR
 *
 *  Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
 *
 *  COPYRIGHT (c) 1989-2011.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

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

#include <rtems/test.h>
#include <rtems/test-info.h>

#include <rtems/score/threadimpl.h>
#include <rtems/rtems/eventimpl.h>

const char rtems_test_name[] = "SPINTRCRITICAL 21";

#define MAX_ITERATION_COUNT 10000

typedef struct {
  rtems_id        main_task;
  Thread_Control *main_thread;
  rtems_id        other_task;
} test_context;

static void clear_pending_events( void )
{
  rtems_event_set out;

  (void) rtems_event_receive(
    RTEMS_ALL_EVENTS,
    RTEMS_NO_WAIT | RTEMS_EVENT_ANY,
    0,
    &out
  );
}

static bool is_blocked( Thread_Wait_flags flags )
{
  return flags == ( THREAD_WAIT_CLASS_EVENT | THREAD_WAIT_STATE_BLOCKED );
}

static bool interrupts_blocking_op( Thread_Wait_flags flags )
{
  return flags
    == ( THREAD_WAIT_CLASS_EVENT | THREAD_WAIT_STATE_INTEND_TO_BLOCK );
}

static T_interrupt_test_state event_from_isr_interrupt(
  void *arg
)
{
  test_context           *ctx;
  T_interrupt_test_state state;
  Thread_Wait_flags      flags;
  rtems_status_code      status;

  ctx = arg;
  flags = _Thread_Wait_flags_get( ctx->main_thread );

  if ( interrupts_blocking_op( flags ) ) {
    /*
     *  This event send hits the critical section but sends to
     *  another task so doesn't impact this critical section.
     */
    status = rtems_event_send( ctx->other_task, 0x02 );
    T_quiet_rsc_success( status );

    /*
     *  This event send hits the main task but doesn't satisfy
     *  it's blocking condition so it will still block
     */
    status = rtems_event_send( ctx->main_task, 0x02 );
    T_quiet_rsc_success( status );

    state = T_INTERRUPT_TEST_DONE;
  } else if ( is_blocked( flags ) ) {
    state = T_INTERRUPT_TEST_LATE;
  } else {
    state = T_INTERRUPT_TEST_EARLY;
  }

  status = rtems_event_send( ctx->main_task, 0x01 );
  T_quiet_rsc_success( status );

  return state;
}

static void event_from_isr_action( void *arg )
{
  rtems_status_code status;
  rtems_event_set   out;

  (void) arg;

  status = rtems_event_receive( 0x01, RTEMS_DEFAULT_OPTIONS, 0, &out );
  T_quiet_rsc_success( status );
}

static const T_interrupt_test_config event_from_isr_config = {
  .action = event_from_isr_action,
  .interrupt = event_from_isr_interrupt,
  .max_iteration_count = MAX_ITERATION_COUNT
};

T_TEST_CASE(EventFromISR)
{
  test_context           ctx;
  T_interrupt_test_state state;
  rtems_status_code      status;

  ctx.main_task = rtems_task_self();
  ctx.main_thread = _Thread_Get_executing();

  status = rtems_task_create(
    0xa5a5a5a5,
    1,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &ctx.other_task
  );
  T_assert_rsc_success( status );

  state = T_interrupt_test( &event_from_isr_config, &ctx );
  T_eq_int( state, T_INTERRUPT_TEST_DONE );

  status = rtems_task_delete( ctx.other_task );
  T_rsc_success( status );

  clear_pending_events();
}

static T_interrupt_test_state event_with_timeout_from_isr_interrupt(
  void *arg
)
{
  test_context           *ctx;
  T_interrupt_test_state  state;
  Thread_Wait_flags       flags;
  rtems_status_code       status;

  ctx = arg;
  flags = _Thread_Wait_flags_get( ctx->main_thread );

  if ( interrupts_blocking_op( flags ) ) {
    /*
     *  We want to catch the task while it is blocking.  Otherwise
     *  just send and make it happy.
     */
    state = T_INTERRUPT_TEST_DONE;
  } else if ( is_blocked( flags ) ) {
    state = T_INTERRUPT_TEST_LATE;
  } else {
    state = T_INTERRUPT_TEST_EARLY;
  }

  status = rtems_event_send( ctx->main_task, 0x01 );
  T_quiet_rsc_success( status );

  return state;
}

static void event_with_timeout_from_isr_action( void *arg )
{
  rtems_status_code status;
  rtems_event_set   out;

  (void) arg;

  status = rtems_event_receive( 0x01, RTEMS_DEFAULT_OPTIONS, 1, &out );
  T_quiet_true( status == RTEMS_SUCCESSFUL || status == RTEMS_TIMEOUT );
}

static const T_interrupt_test_config event_with_timeout_from_isr_config = {
  .action = event_with_timeout_from_isr_action,
  .interrupt = event_with_timeout_from_isr_interrupt,
  .max_iteration_count = MAX_ITERATION_COUNT
};

T_TEST_CASE( EventWithTimeoutFromISR )
{
  test_context           ctx;
  T_interrupt_test_state state;

  ctx.main_task = rtems_task_self();
  ctx.main_thread = _Thread_Get_executing();
  ctx.other_task = 0xdeadbeef;

  state = T_interrupt_test( &event_with_timeout_from_isr_config, &ctx );
  T_eq_int( state, T_INTERRUPT_TEST_DONE );

  clear_pending_events();
}

static rtems_task Init( rtems_task_argument argument )
{
  rtems_test_run( argument, TEST_STATE );
}

#define CONFIGURE_INIT

#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_MICROSECONDS_PER_TICK   1000

#define CONFIGURE_MAXIMUM_TASKS             2

#include <rtems/confdefs.h>