summaryrefslogtreecommitdiff
path: root/testsuites/validation/tc-score-smp-thread.c
blob: be7e9fd1ee69ee758f516d2487ea00273f90c67d (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
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSTestCaseScoreThreadValSmp
 */

/*
 * 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.
 */

/*
 * This file is part of the RTEMS quality process and was automatically
 * generated.  If you find something that needs to be fixed or
 * worded better please post a report or patch to an RTEMS mailing list
 * or raise a bug report:
 *
 * https://www.rtems.org/bugs.html
 *
 * For information on updating and regenerating please refer to the How-To
 * section in the Software Requirements Engineering chapter of the
 * RTEMS Software Engineering manual.  The manual is provided as a part of
 * a release.  For development sources please refer to the online
 * documentation at:
 *
 * https://docs.rtems.org
 */

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

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

#include "ts-config.h"
#include "tx-support.h"

#include <rtems/test.h>

/**
 * @defgroup RTEMSTestCaseScoreThreadValSmp spec:/score/thread/val/smp
 *
 * @ingroup RTEMSTestSuiteTestsuitesValidationSmpOnly0
 *
 * @brief Tests SMP-specific thread behaviour.
 *
 * This test case performs the following actions:
 *
 * - Create three worker threads and a mutex.  Use the mutex and the worker to
 *   move to a helping scheduler.
 *
 *   - Pin the runner thread while it executes on a processor owned by a
 *     helping scheduler.
 *
 *   - Pin and unpin the runner thread.  This is a nested operation.
 *
 *   - Preempt the pinned runner thread.  Worker B and C execute at the same
 *     time on processor 0 and 1 respectively for some point in time.  This
 *     shows that the pinning of the runner thread is maintained.
 *
 *   - Unpin the runner thread.  The runner moves back to its home scheduler.
 *
 *   - Release the mutex.
 *
 *   - Pin the runner thread.  Unpin the runner thread while it is suspended.
 *
 *   - Make sure the worker released the mutex.
 *
 *   - Clean up all used resources.
 *
 * @{
 */

/**
 * @brief Test context for spec:/score/thread/val/smp test case.
 */
typedef struct {
  /**
   * @brief This member contains the scheduler A identifier.
   */
  rtems_id scheduler_a_id;

  /**
   * @brief This member contains the scheduler B identifier.
   */
  rtems_id scheduler_b_id;

  /**
   * @brief This member contains the worker A identifier.
   */
  rtems_id worker_a_id;

  /**
   * @brief This member contains the worker B identifier.
   */
  rtems_id worker_b_id;

  /**
   * @brief This member contains the worker C identifier.
   */
  rtems_id worker_c_id;

  /**
   * @brief This member contains the mutex identifier.
   */
  rtems_id mutex_id;

  /**
   * @brief If this member is true, then the worker shall busy wait.
   */
  volatile bool busy;

  /**
   * @brief This member contains a counter for EVENT_COUNT.
   */
  volatile uint32_t counter;
} ScoreThreadValSmp_Context;

static ScoreThreadValSmp_Context
  ScoreThreadValSmp_Instance;

static T_fixture ScoreThreadValSmp_Fixture = {
  .setup = NULL,
  .stop = NULL,
  .teardown = NULL,
  .scope = NULL,
  .initial_context = &ScoreThreadValSmp_Instance
};

typedef ScoreThreadValSmp_Context Context;

typedef enum {
  EVENT_OBTAIN = RTEMS_EVENT_0,
  EVENT_RELEASE = RTEMS_EVENT_1,
  EVENT_BUSY = RTEMS_EVENT_2,
  EVENT_COUNT = RTEMS_EVENT_3,
  EVENT_LET_WORKER_C_COUNT = RTEMS_EVENT_4
} Event;

static void WorkerTask( rtems_task_argument arg )
{
  Context *ctx;

  ctx = (Context *) arg;

  while ( true ) {
    rtems_event_set events;

    events = ReceiveAnyEvents();

    if ( ( events & EVENT_OBTAIN ) != 0 ) {
      ObtainMutex( ctx->mutex_id );
    }

    if ( ( events & EVENT_RELEASE ) != 0 ) {
      ReleaseMutex( ctx->mutex_id );
    }

    if ( ( events & EVENT_BUSY ) != 0 ) {
      while ( ctx->busy ) {
        /* Do nothing */
      }
    }

    if ( ( events & EVENT_COUNT ) != 0 ) {
      ++ctx->counter;
    }

    if ( ( events & EVENT_LET_WORKER_C_COUNT ) != 0 ) {
      uint32_t counter;

      counter = ctx->counter;
      SendEvents( ctx->worker_c_id, EVENT_COUNT );

      while ( ctx->counter == counter ) {
        /* Wait */
      }
    }
  }
}

static void Suspend( void *arg )
{
  Thread_Control *thread;

  thread = arg;
  SuspendTask( thread->Object.id );
}

static void Resume( void *arg )
{
  Thread_Control *thread;

  thread = arg;
  ResumeTask( thread->Object.id );
}

/**
 * @brief Create three worker threads and a mutex.  Use the mutex and the
 *   worker to move to a helping scheduler.
 */
static void ScoreThreadValSmp_Action_0( ScoreThreadValSmp_Context *ctx )
{
  rtems_status_code sc;
  Per_CPU_Control  *cpu_self;
  Thread_Control   *executing;

  executing = _Thread_Get_executing();
  SetSelfPriority( PRIO_NORMAL );
  ctx->counter = 0;

  sc = rtems_scheduler_ident( TEST_SCHEDULER_A_NAME, &ctx->scheduler_a_id );
  T_rsc_success( sc );

  sc = rtems_scheduler_ident( TEST_SCHEDULER_B_NAME, &ctx->scheduler_b_id );
  T_rsc_success( sc );

  ctx->mutex_id = CreateMutex();

  ctx->worker_a_id = CreateTask( "WRKA", PRIO_NORMAL );
  SetScheduler( ctx->worker_a_id, ctx->scheduler_b_id, PRIO_NORMAL );
  StartTask( ctx->worker_a_id, WorkerTask, ctx );

  ctx->worker_b_id = CreateTask( "WRKB", PRIO_HIGH );
  StartTask( ctx->worker_b_id, WorkerTask, ctx );

  ctx->worker_c_id = CreateTask( "WRKC", PRIO_LOW );
  StartTask( ctx->worker_c_id, WorkerTask, ctx );

  ObtainMutex( ctx->mutex_id );
  SendEvents( ctx->worker_a_id, EVENT_OBTAIN | EVENT_RELEASE );

  ctx->busy = true;
  SendEvents( ctx->worker_b_id, EVENT_BUSY );

  /*
   * Pin the runner thread while it executes on a processor owned by a helping
   * scheduler.
   */
  T_eq_u32( rtems_scheduler_get_processor(), 1 );
  _Thread_Pin( executing );

  /*
   * Pin and unpin the runner thread.  This is a nested operation.
   */
  T_eq_u32( rtems_scheduler_get_processor(), 1 );
  _Thread_Pin( executing );
  _Thread_Unpin( executing, _Per_CPU_Get_snapshot() );

  /*
   * Preempt the pinned runner thread.  Worker B and C execute at the same time
   * on processor 0 and 1 respectively for some point in time.  This shows that
   * the pinning of the runner thread is maintained.
   */
  ctx->busy = false;
  SetScheduler( ctx->worker_b_id, ctx->scheduler_b_id, PRIO_HIGH );
  SendEvents( ctx->worker_b_id, EVENT_LET_WORKER_C_COUNT );

  T_eq_u32( rtems_scheduler_get_processor(), 1 );
  T_eq_u32( ctx->counter, 1 );

  /*
   * Unpin the runner thread.  The runner moves back to its home scheduler.
   */
  cpu_self = _Thread_Dispatch_disable();
  _Thread_Unpin( executing, cpu_self );
  _Thread_Dispatch_direct( cpu_self );

  T_eq_u32( rtems_scheduler_get_processor(), 0 );

  /*
   * Release the mutex.
   */
  ReleaseMutex( ctx->mutex_id);
  T_eq_u32( rtems_scheduler_get_processor(), 0 );

  /*
   * Pin the runner thread.  Unpin the runner thread while it is suspended.
   */
  _Thread_Pin( executing );

  /* We have to preempt the runner to end up in _Thread_Do_unpin() */
  SetPriority( ctx->worker_c_id, PRIO_HIGH );
  SendEvents( ctx->worker_c_id, EVENT_COUNT );
  T_eq_u32( ctx->counter, 2 );

  cpu_self = _Thread_Dispatch_disable();
  CallWithinISR( Suspend, executing );
  _Thread_Unpin( executing, cpu_self );
  CallWithinISR( Resume, executing );
  _Thread_Dispatch_direct( cpu_self );

  /*
   * Make sure the worker released the mutex.
   */
  SetSelfScheduler( ctx->scheduler_b_id, PRIO_LOW );
  SetSelfScheduler( ctx->scheduler_a_id, PRIO_NORMAL );

  /*
   * Clean up all used resources.
   */
  DeleteTask( ctx->worker_a_id );
  DeleteTask( ctx->worker_b_id );
  DeleteTask( ctx->worker_c_id );
  DeleteMutex( ctx->mutex_id );
  RestoreRunnerPriority();
}

/**
 * @fn void T_case_body_ScoreThreadValSmp( void )
 */
T_TEST_CASE_FIXTURE( ScoreThreadValSmp, &ScoreThreadValSmp_Fixture )
{
  ScoreThreadValSmp_Context *ctx;

  ctx = T_fixture_context();

  ScoreThreadValSmp_Action_0( ctx );
}

/** @} */