summaryrefslogtreecommitdiff
path: root/testsuites/validation/tx-support.c
blob: 6a65055b194968bebcb4b404d9222dccc5055e78 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSTestSuites
 *
 * @brief This source file contains the implementation of support functions for
 *   the validation test cases.
 */

/*
 * Copyright (C) 2021 embedded brains GmbH (http://www.embedded-brains.de)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

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

#include "tx-support.h"

#include <rtems/test.h>
#include <rtems/score/percpu.h>
#include <rtems/score/smpimpl.h>
#include <rtems/score/threaddispatch.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/watchdogimpl.h>

rtems_id DoCreateTask( rtems_name name, rtems_task_priority priority )
{
  rtems_status_code sc;
  rtems_id          id;

  sc = rtems_task_create(
    name,
    priority,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &id
  );
  T_assert_rsc_success( sc );

  return id;
}

void StartTask( rtems_id id, rtems_task_entry entry, void *arg )
{
  rtems_status_code sc;

  sc = rtems_task_start( id, entry, (rtems_task_argument) arg);
  T_assert_rsc_success( sc );
}

void DeleteTask( rtems_id id )
{
  if ( id != 0 ) {
    rtems_status_code sc;

    sc = rtems_task_delete( id );
    T_quiet_rsc_success( sc );
  }
}

void SuspendTask( rtems_id id )
{
  rtems_status_code sc;

  sc = rtems_task_suspend( id );
  T_quiet_rsc_success( sc );
}

void SuspendSelf( void )
{
  SuspendTask( RTEMS_SELF );
}

void ResumeTask( rtems_id id )
{
  rtems_status_code sc;

  sc = rtems_task_resume( id );
  T_quiet_rsc_success( sc );
}

rtems_event_set ReceiveAnyEvents( void )
{
  rtems_status_code sc;
  rtems_event_set   events;

  events = 0;
  sc = rtems_event_receive(
    RTEMS_ALL_EVENTS,
    RTEMS_EVENT_ANY | RTEMS_WAIT,
    RTEMS_NO_TIMEOUT,
    &events
  );
  T_quiet_rsc_success( sc );

  return events;
}

void ReceiveAllEvents( rtems_event_set events )
{
  rtems_status_code sc;
  rtems_event_set   received;

  received = 0;
  sc = rtems_event_receive(
    events,
    RTEMS_EVENT_ALL | RTEMS_WAIT,
    RTEMS_NO_TIMEOUT,
    &received
  );
  T_quiet_rsc_success( sc );
  T_quiet_eq_u32( received, events );
}

void SendEvents( rtems_id id, rtems_event_set events )
{
  rtems_status_code sc;

  sc = rtems_event_send( id, events );
  T_quiet_rsc_success( sc );
}

rtems_mode GetMode( void )
{
  return SetMode( RTEMS_DEFAULT_MODES, RTEMS_CURRENT_MODE );
}

rtems_mode SetMode( rtems_mode set, rtems_mode mask )
{
  rtems_status_code sc;
  rtems_mode        previous;

  sc = rtems_task_mode( set, mask, &previous );
  T_quiet_rsc_success( sc );

  return previous;
}

rtems_task_priority GetPriority( rtems_id id )
{
  return SetPriority( id, RTEMS_CURRENT_PRIORITY );
}

rtems_task_priority SetPriority( rtems_id id, rtems_task_priority priority )
{
  rtems_status_code   sc;
  rtems_task_priority previous;

  sc = rtems_task_set_priority( id, priority, &previous );
  T_quiet_rsc_success( sc );

  return previous;
}

rtems_task_priority GetSelfPriority( void )
{
  return SetPriority( RTEMS_SELF, RTEMS_CURRENT_PRIORITY );
}

rtems_task_priority SetSelfPriority( rtems_task_priority priority )
{
  return SetPriority( RTEMS_SELF, priority );
}

rtems_id GetScheduler( rtems_id id )
{
  rtems_status_code sc;
  rtems_id          scheduler_id;

  scheduler_id = 0xffffffff;
  sc = rtems_task_get_scheduler( id, &scheduler_id );
  T_quiet_rsc_success( sc );

  return scheduler_id;
}

rtems_id GetSelfScheduler( void )
{
  return GetScheduler( RTEMS_SELF );
}

void SetScheduler(
  rtems_id            task_id,
  rtems_id            scheduler_id,
  rtems_task_priority priority
)
{
  rtems_status_code sc;

  sc = rtems_task_set_scheduler( task_id, scheduler_id, priority );
  T_quiet_rsc_success( sc );
}

void SetSelfScheduler( rtems_id scheduler_id, rtems_task_priority priority )
{
  SetScheduler( RTEMS_SELF, scheduler_id, priority );
}

void GetAffinity( rtems_id id, cpu_set_t *set )
{
  rtems_status_code sc;

  CPU_ZERO( set );
  sc = rtems_task_get_affinity( id, sizeof( *set ), set );
  T_quiet_rsc_success( sc );
}

void GetSelfAffinity( cpu_set_t *set )
{
  GetAffinity( RTEMS_SELF, set );
}

void SetAffinity( rtems_id id, const cpu_set_t *set )
{
  rtems_status_code sc;

  sc = rtems_task_set_affinity( id, sizeof( *set ), set );
  T_quiet_rsc_success( sc );
}

void SetSelfAffinity( const cpu_set_t *set )
{
  SetAffinity( RTEMS_SELF, set );
}

void SetAffinityOne( rtems_id id, uint32_t cpu_index )
{
  cpu_set_t set;

  CPU_ZERO( &set );
  CPU_SET( (int) cpu_index, &set );
  SetAffinity( id, &set );
}

void SetSelfAffinityOne( uint32_t cpu_index )
{
  SetAffinityOne( RTEMS_SELF, cpu_index );
}

void SetAffinityAll( rtems_id id )
{
  cpu_set_t set;

  CPU_FILL( &set );
  SetAffinity( id, &set );
}

void SetSelfAffinityAll( void )
{
  SetAffinityAll( RTEMS_SELF );
}

void Yield( void )
{
  rtems_status_code sc;

  sc = rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
  T_quiet_rsc_success( sc );
}

void RestoreRunnerASR( void )
{
  rtems_status_code sc;

  sc = rtems_signal_catch( NULL, RTEMS_DEFAULT_MODES );
  T_quiet_rsc_success( sc );
}

void RestoreRunnerMode( void )
{
  rtems_status_code sc;
  rtems_mode        mode;

  sc = rtems_task_mode( RTEMS_DEFAULT_MODES, RTEMS_ALL_MODE_MASKS, &mode );
  T_quiet_rsc_success( sc );
}

void RestoreRunnerPriority( void )
{
  SetSelfPriority( PRIO_ULTRA_HIGH );
}

Thread_Control *GetThread( rtems_id id )
{
  Thread_Control  *the_thread;
  ISR_lock_Context lock_context;

  the_thread = _Thread_Get( id, &lock_context );
  _ISR_lock_ISR_enable( &lock_context);
  return the_thread;
}

void WaitForExecutionStop( rtems_id task_id )
{
#if defined( RTEMS_SMP )
  Thread_Control *the_thread;

  the_thread = GetThread( task_id );
  T_assert_not_null( the_thread );

  while ( _Thread_Is_executing_on_a_processor( the_thread ) ) {
    /* Wait */
  }
#else
  (void) task_id;
#endif
}

#if defined( RTEMS_SMP )
static void DoWatchdogTick( void *arg )
{
  (void) arg;
  _Watchdog_Tick( _Per_CPU_Get() );
}
#endif

void ClockTick( void )
{
  Per_CPU_Control *cpu_self;

  cpu_self = _Thread_Dispatch_disable();
#if defined( RTEMS_SMP )
  _SMP_Broadcast_action( DoWatchdogTick, NULL );
#else
  _Watchdog_Tick( cpu_self );
#endif
  _Thread_Dispatch_enable( cpu_self );
}