summaryrefslogtreecommitdiffstats
path: root/spec/score/tq/req/surrender.yml
blob: 7f77ab936b5a683dd882d72b513a23dd01d88876 (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
SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
copyrights:
- Copyright (C) 2021 embedded brains GmbH (http://www.embedded-brains.de)
enabled-by: true
functional-type: action
links:
- role: requirement-refinement
  uid: ../if/group
post-conditions:
- name: Dequeue
  states:
  - name: FIFO
    test-code: |
      T_eq_u32( TQGetWorkerCounter( ctx->tq_ctx, TQ_BLOCKER_A ), 1 );
      T_eq_u32(
        TQGetWorkerCounter( ctx->tq_ctx, TQ_BLOCKER_B ),
        ctx->expected_blocker_b_counter
      );
    text: |
      The first thread in FIFO order shall be dequeued from the thread queue.
  - name: Priority
    test-code: |
      T_eq_u32( TQGetWorkerCounter( ctx->tq_ctx, TQ_BLOCKER_A ), 1 );
      T_eq_u32( TQGetWorkerCounter( ctx->tq_ctx, TQ_BLOCKER_B ), 2 );
    text: |
      The first thread in priority order shall be dequeued from the thread
      queue.
  test-epilogue: null
  test-prologue: null
- name: Unblock
  states:
  - name: 'Yes'
    test-code: |
      T_eq_ptr( GetUnblock( ctx, &i ), GetTCB( ctx, TQ_BLOCKER_A ) );
      T_eq_ptr( GetUnblock( ctx, &i ), NULL );
    text: |
      The dequeued thread shall be unblocked by surrender operation.
  - name: 'No'
    test-code: |
      T_eq_ptr( GetUnblock( ctx, &i ), NULL );
    text: |
      The dequeued thread shall not be unblocked by surrender operation.
  test-epilogue: null
  test-prologue: |
    size_t i;

    i = 0;
pre-conditions:
- name: HasOwner
  states:
  - name: 'Yes'
    test-code: |
      if ( ctx->tq_ctx->get_owner == NULL ) {
        ${.:skip}
      }
    text: |
      Where the thread queue has a previous owner thread.
  - name: 'No'
    test-code: |
      if ( ctx->tq_ctx->get_owner != NULL ) {
        ${.:skip}
      }
    text: |
      Where the thread queue has no owner threads.
  test-epilogue: null
  test-prologue: null
- name: Discipline
  states:
  - name: FIFO
    test-code: |
      if ( ctx->tq_ctx->discipline != TQ_FIFO ) {
        ${.:skip}
      }
    text: |
      Where the thread queue uses the FIFO discipline.
  - name: Priority
    test-code: |
      if ( ctx->tq_ctx->discipline != TQ_PRIORITY ) {
        ${.:skip}
      }
    text: |
      Where the thread queue uses the priority discipline.
  test-epilogue: null
  test-prologue: null
- name: WaitState
  states:
  - name: Blocked
    test-code: |
      ctx->intend_to_block = false;
    text: |
      While the dequeued thread is in the blocked wait state.
  - name: IntendToBlock
    test-code: |
      ctx->intend_to_block = true;
    text: |
      While the dequeued thread is in the intend to block wait state.
  test-epilogue: null
  test-prologue: null
rationale: null
references: []
requirement-type: functional
skip-reasons:
  OnlyOneExecutingThread: |
    Where the system was built with SMP support disabled, there is at most one
    executing thread.  Thread queues with an owner can only be surrendered by
    the previous owner thread.  Thus, the dequeued thread cannot be in the
    intend to block wait state.
test-action: |
  Status_Control status;

  TQResetCounter( ctx->tq_ctx );
  ctx->expected_blocker_b_counter = 0;

  status = TQEnqueue( ctx->tq_ctx, TQ_NO_WAIT );
  T_eq_int( status, TQConvertStatus( ctx->tq_ctx, STATUS_SUCCESSFUL ) );

  if ( ctx->intend_to_block ) {
  #if defined(RTEMS_SMP)
    SMP_barrier_State state;
  #endif

    /*
     * In uniprocessor configurations, it is impossible to dequeue a thread
     * in FIFO order which is in the intend to block wait state.  Run this
     * test with just one worker.
     */
    if ( ctx->tq_ctx->discipline != TQ_FIFO ) {
      TQSendAndWaitForExecutionStop(
        ctx->tq_ctx,
        TQ_BLOCKER_B,
        TQ_EVENT_ENQUEUE
      );
      ctx->expected_blocker_b_counter = 2;
    }


  #if defined(RTEMS_SMP)
    _SMP_barrier_Control_initialize( &ctx->barrier );
    _SMP_barrier_State_initialize( &state );
  #endif

    T_scheduler_set_event_handler( SchedulerBlock, ctx );
    TQSend( ctx->tq_ctx, TQ_BLOCKER_A, TQ_EVENT_ENQUEUE );

  #if defined(RTEMS_SMP)
    /* B0 */
    _SMP_barrier_Wait( &ctx->barrier, &state, 2 );

    Surrender( ctx );

    /* B1 */
    _SMP_barrier_Wait( &ctx->barrier, &state, 2 );
  #endif
  } else {
    TQSend(
      ctx->tq_ctx,
      TQ_BLOCKER_A,
      TQ_EVENT_HELPER_A_SYNC | TQ_EVENT_ENQUEUE
    );
    TQSynchronizeRunner();
    TQWaitForExecutionStop( ctx->tq_ctx, TQ_BLOCKER_A );

    TQSend(
      ctx->tq_ctx,
      TQ_BLOCKER_B,
      TQ_EVENT_HELPER_A_SYNC | TQ_EVENT_ENQUEUE
    );
    TQSynchronizeRunner();
    TQWaitForExecutionStop( ctx->tq_ctx, TQ_BLOCKER_B );
    ctx->expected_blocker_b_counter = 2;

    Surrender( ctx );
  }

  TQSendAndWaitForExecutionStop(
    ctx->tq_ctx,
    TQ_BLOCKER_A,
    TQ_EVENT_SURRENDER
  );

  if ( ctx->expected_blocker_b_counter != 0 ) {
    TQSendAndWaitForExecutionStop(
      ctx->tq_ctx,
      TQ_BLOCKER_B,
      TQ_EVENT_SURRENDER
    );
  }
test-brief: null
test-cleanup: null
test-context:
- brief: |
    This member contains the call within ISR request.
  description: null
  member: |
    CallWithinISRRequest request;
- brief: |
    This member contains the barrier to synchronize the runner and the worker.
  description: null
  member: |
    SMP_barrier_Control barrier
- brief: |
    If this member is true, then the dequeued thread shall be in the intend to
    block wait state.
  description: null
  member: |
    bool intend_to_block
- brief: |
    If this member contains the expected counter of worker B.
  description: null
  member: |
    uint32_t expected_blocker_b_counter
test-context-support: null
test-description: null
test-header:
  code: null
  freestanding: false
  includes: []
  local-includes:
  - tx-thread-queue.h
  run-params:
  - description: |
      is the thread queue test context.
    dir: inout
    name: tq_ctx
    specifier: TQContext *${.:name}
  target: testsuites/validation/tr-tq-surrender.h
test-includes:
- rtems/score/smpbarrier.h
- rtems/score/threadimpl.h
test-local-includes:
- tx-support.h
- tr-tq-surrender.h
test-prepare: null
test-setup:
  brief: null
  code: |
    ctx->request.arg = ctx;
    TQReset( ctx->tq_ctx );
    TQSetPriority( ctx->tq_ctx, TQ_BLOCKER_A, PRIO_VERY_HIGH );
    TQSetPriority( ctx->tq_ctx, TQ_BLOCKER_B, PRIO_HIGH );

    #if defined(RTEMS_SMP)
    /*
     * For the mutexes with priority ceiling protocol, we need a scheduler with
     * two processors to set up the intend to block wait state.
     */
    RemoveProcessor( SCHEDULER_B_ID, 1 );
    AddProcessor( SCHEDULER_A_ID, 1 );
    #endif
  description: null
test-stop: null
test-support: |
  typedef ${.:/test-context-type} Context;

  static const rtems_tcb *GetUnblock( Context *ctx, size_t *index )
  {
    return TQGetNextUnblock( ctx->tq_ctx, index )->thread;
  }

  static const rtems_tcb *GetTCB( Context *ctx, TQWorkerKind worker )
  {
    return ctx->tq_ctx->worker_tcb[ worker ];
  }

  static void Surrender( void *arg )
  {
    Context       *ctx;
    Status_Control status;

    ctx = arg;
    TQSchedulerRecordStart( ctx->tq_ctx );

    status = TQSurrender( ctx->tq_ctx );
    T_eq_int( status, TQConvertStatus( ctx->tq_ctx, STATUS_SUCCESSFUL ) );

    TQSchedulerRecordStop( ctx->tq_ctx );
  }

  #if defined(RTEMS_SMP)
  static void Delay( void *arg )
  {
    Context          *ctx;
    SMP_barrier_State state;

    ctx = arg;
    _SMP_barrier_State_initialize( &state );

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

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

  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
    ) {
      T_scheduler_set_event_handler( NULL, NULL );
  #if defined(RTEMS_SMP)
      ctx->request.handler = Delay;
  #else
      ctx->request.handler = Surrender;
  #endif
      CallWithinISRSubmit( &ctx->request );
    }
  }
test-target: testsuites/validation/tr-tq-surrender.c
test-teardown:
  brief: null
  code: |
    TQReset( ctx->tq_ctx );

    #if defined(RTEMS_SMP)
    RemoveProcessor( SCHEDULER_A_ID, 1 );
    AddProcessor( SCHEDULER_B_ID, 1 );
    #endif
  description: null
text: |
  When the thread queue enqueue operation timed out.
transition-map:
- enabled-by: true
  post-conditions:
    Dequeue:
    - specified-by: Discipline
    Unblock:
    - if:
        pre-conditions:
          WaitState: IntendToBlock
      then: 'No'
    - else: 'Yes'
  pre-conditions:
    HasOwner: all
    Discipline: all
    WaitState: all
- enabled-by:
    not: RTEMS_SMP
  post-conditions: OnlyOneExecutingThread
  pre-conditions:
    HasOwner:
    - 'Yes'
    Discipline: all
    WaitState:
    - IntendToBlock
type: requirement