summaryrefslogtreecommitdiffstats
path: root/spec/score/thread/val/smp.yml
blob: c443c92fbe0e7f94906519f516f5f24ea90e8c6a (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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
copyrights:
- Copyright (C) 2021 embedded brains GmbH & Co. KG
enabled-by: RTEMS_SMP
links: []
test-actions:
- action-brief: |
    Create three worker threads and a mutex.  Use the mutex and the worker to
    move to a helping scheduler.
  action-code: |
    Per_CPU_Control*cpu_self;
    Thread_Control *executing;

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

    ctx->mutex_id = CreateMutex();

    ctx->worker_a_id = CreateTask( "WRKA", PRIO_NORMAL );
    SetScheduler( ctx->worker_a_id, 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 );
  checks:
  - brief: |
      Pin the runner thread while it executes on a processor owned by a
      helping scheduler.
    code: |
      T_eq_u32( rtems_scheduler_get_processor(), 1 );
      _Thread_Pin( executing );
    links:
    - role: validation
      uid: ../req/pinning-helping
  - brief: |
      Pin and unpin the runner thread.  This is a nested operation.
    code: |
      T_eq_u32( rtems_scheduler_get_processor(), 1 );
      _Thread_Pin( executing );
      _Thread_Unpin( executing, _Per_CPU_Get_snapshot() );
    links:
    - role: validation
      uid: ../req/pinning-nested
  - brief: |
      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.
    code: |
      ctx->busy = false;
      SetScheduler( ctx->worker_b_id, 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 );
    links:
    - role: validation
      uid: ../req/pinning-preemptible
  - brief: |
      Unpin the runner thread.  The runner moves back to its home scheduler.
    code: |
      cpu_self = _Thread_Dispatch_disable();
      _Thread_Unpin( executing, cpu_self );
      _Thread_Dispatch_direct( cpu_self );

      T_eq_u32( rtems_scheduler_get_processor(), 0 );
    links:
    - role: validation
      uid: ../req/pinning-nested
  - brief: |
      Release the mutex.
    code: |
      ReleaseMutex( ctx->mutex_id);
      T_eq_u32( rtems_scheduler_get_processor(), 0 );
    links: []
  - brief: |
      Pin the runner thread.  Unpin the runner thread while it is suspended.
    code: |
      _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 );
    links:
    - role: validation
      uid: ../req/pinning-unpin-suspended
  - brief: |
      Make sure the worker released the mutex.
    code: |
      SetSelfScheduler( SCHEDULER_B_ID, PRIO_LOW );
      SetSelfScheduler( SCHEDULER_A_ID, PRIO_NORMAL );
    links: []
  - brief: |
      Clean up all used resources.
    code: |
      DeleteTask( ctx->worker_a_id );
      DeleteTask( ctx->worker_b_id );
      DeleteTask( ctx->worker_c_id );
      DeleteMutex( ctx->mutex_id );
    links: []
  links: []
- action-brief: |
    Create three worker threads and a mutex.  Use the mutex and the worker to
    check that a suspended thread does not reconsider help requests.
  action-code: |
    T_scheduler_log_10       scheduler_log;
    size_t                   index;
    const T_scheduler_event *event;

    _SMP_barrier_Control_initialize( &ctx->barrier );
    _SMP_barrier_State_initialize( &ctx->barrier_state );

    ctx->counter = 0;
    ctx->mutex_id = CreateMutex();

    ctx->worker_a_id = CreateTask( "WRKA", PRIO_NORMAL );
    SetScheduler( ctx->worker_a_id, 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_NORMAL );
    SetScheduler( ctx->worker_c_id, SCHEDULER_B_ID, PRIO_HIGH );
    StartTask( ctx->worker_c_id, WorkerTask, ctx );
  checks:
  - brief: |
      Let worker B help worker A through the mutex.  Preempt worker A.  Delay
      the thread switch to worker A.
    code: |
      ctx->busy = true;
      SendEvents(
        ctx->worker_a_id,
        EVENT_OBTAIN | EVENT_COUNT_EARLY | EVENT_BUSY | EVENT_COUNT
      );
      WaitForCounter( ctx, 1 );

      SendEvents( ctx->worker_b_id, EVENT_OBTAIN );
      SetPriority( ctx->worker_b_id, PRIO_LOW );
      SendEvents( ctx->worker_c_id, EVENT_SET_TASK_SWITCH_EXTENSION );

      /* B0 */
      _SMP_barrier_Wait( &ctx->barrier, &ctx->barrier_state, 2 );
    links: []
  - brief: |
      Suspend worker A and let it wait on its thread state lock.  Check that
      worker A did not reconsider help requests.
    code: |
      T_scheduler_record_10( &scheduler_log );
      T_scheduler_set_event_handler( SchedulerBlock, ctx );
      SuspendTask( ctx->worker_a_id );
      WaitForExecutionStop( ctx->worker_a_id );
      T_scheduler_record( NULL );
      T_eq_sz( scheduler_log.header.recorded, 2 );
      index = 0;
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_int( event->operation, T_SCHEDULER_BLOCK );
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_int( event->operation, T_SCHEDULER_WITHDRAW_NODE );
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_ptr( event, &T_scheduler_event_null );
      SetTaskSwitchExtension( NULL );
    links:
    - role: validation
      uid: ../req/suspended-helping
  - brief: |
      Resume worker A.  Check that worker A did reconsider help requests after
      the thread dispatch.
    code: |
      T_scheduler_record_10( &scheduler_log );
      ResumeTask( ctx->worker_a_id );
      ctx->busy = false;
      WaitForCounter( ctx, 2 );
      WaitForExecutionStop( ctx->worker_a_id );
      T_scheduler_record( NULL );
      T_eq_sz( scheduler_log.header.recorded, 5 );
      index = 0;
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_int( event->operation, T_SCHEDULER_UNBLOCK );
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_int( event->operation, T_SCHEDULER_RECONSIDER_HELP_REQUEST );
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_int( event->operation, T_SCHEDULER_RECONSIDER_HELP_REQUEST );
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_int( event->operation, T_SCHEDULER_BLOCK );
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_int( event->operation, T_SCHEDULER_WITHDRAW_NODE );
      event = T_scheduler_next_any( &scheduler_log.header, &index );
      T_eq_ptr( event, &T_scheduler_event_null );
    links: []
  - brief: |
      Clean up all used resources.
    code: |
      SendEvents( ctx->worker_a_id, EVENT_RELEASE | EVENT_COUNT );
      WaitForCounter( ctx, 3 );

      SetPriority( ctx->worker_b_id, PRIO_HIGH );
      SendEvents( ctx->worker_b_id, EVENT_RELEASE );

      DeleteTask( ctx->worker_a_id );
      DeleteTask( ctx->worker_b_id );
      DeleteTask( ctx->worker_c_id );
      DeleteMutex( ctx->mutex_id );
    links: []
  links: []
test-brief: |
  Tests SMP-specific thread behaviour.
test-context:
- brief: |
    This member contains the worker A identifier.
  description: null
  member: |
    rtems_id worker_a_id
- brief: |
    This member contains the worker B identifier.
  description: null
  member: |
    rtems_id worker_b_id
- brief: |
    This member contains the worker C identifier.
  description: null
  member: |
    rtems_id worker_c_id
- brief: |
    This member contains the mutex identifier.
  description: null
  member: |
    rtems_id mutex_id
- brief: |
    If this member is true, then the worker shall busy wait.
  description: null
  member: |
    volatile bool busy
- brief: |
    This member contains a counter for EVENT_COUNT.
  description: null
  member: |
    volatile uint32_t counter
- brief: |
    This member contains the barrier to synchronize the runner and the workers.
  description: null
  member: |
    SMP_barrier_Control barrier
- brief: |
    This member contains the barrier state for the runner processor.
  description: null
  member: |
    SMP_barrier_State barrier_state
test-context-support: null
test-description: null
test-header: null
test-includes:
- rtems.h
- rtems/test-scheduler.h
- rtems/score/smpbarrier.h
- rtems/score/threadimpl.h
test-local-includes:
- ts-config.h
- tx-support.h
test-setup:
  brief: null
  code: |
    SetSelfPriority( PRIO_NORMAL );
  description: null
test-stop: null
test-support: |
  #define EVENT_OBTAIN RTEMS_EVENT_0

  #define EVENT_RELEASE RTEMS_EVENT_1

  #define EVENT_COUNT_EARLY RTEMS_EVENT_2

  #define EVENT_BUSY RTEMS_EVENT_3

  #define EVENT_COUNT RTEMS_EVENT_4

  #define EVENT_LET_WORKER_C_COUNT RTEMS_EVENT_5

  #define EVENT_SET_TASK_SWITCH_EXTENSION RTEMS_EVENT_6

  typedef ${.:/test-context-type} Context;

  static void TaskSwitchExtension( rtems_tcb *executing, rtems_tcb *heir )
  {
    Context        *ctx;
    Thread_Control *thread;

    (void) executing;
    (void) heir;

    ctx = T_fixture_context();
    thread = GetThread( ctx->worker_a_id );

    if ( thread == heir ) {
      SMP_barrier_State state;

      _SMP_barrier_State_initialize( &state );

      /* B0 */
      _SMP_barrier_Wait( &ctx->barrier, &state, 2 );

      /* B1 */
      _SMP_barrier_Wait( &ctx->barrier, &state, 2 );
    }
  }

  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_COUNT_EARLY ) != 0 ) {
        ++ctx->counter;
      }

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

      if ( ( events & EVENT_SET_TASK_SWITCH_EXTENSION ) != 0 ) {
        SetTaskSwitchExtension( TaskSwitchExtension );
      }
    }
  }

  static void SchedulerBlock(
    void                    *arg,
    const T_scheduler_event *event,
    T_scheduler_when         when
  )
  {
    Context *ctx;

    ctx = arg;

    if (
      when == T_SCHEDULER_BEFORE &&
      event->operation == T_SCHEDULER_BLOCK
    ) {
      Thread_Control *thread;

      T_scheduler_set_event_handler( NULL, NULL );

      /* B1 */
      _SMP_barrier_Wait( &ctx->barrier, &ctx->barrier_state, 2 );

      thread = GetThread( ctx->worker_a_id );
      TicketLockWaitForOthers( &thread->Join_queue.Queue.Lock, 1 );
    }
  }

  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 );
  }

  static void WaitForCounter( const Context *ctx, uint32_t expected )
  {
    while ( ctx->counter != expected ) {
      /* Wait */
    }
  }
test-target: testsuites/validation/tc-score-smp-thread.c
test-teardown:
  brief: null
  code: |
    RestoreRunnerPriority();
  description: null
type: test-case