source: rtems/testsuites/smptests/smpthreadlife01/init.c @ a92989a

4.115
Last change on this file since a92989a was a92989a, checked in by Sebastian Huber <sebastian.huber@…>, on 06/18/14 at 13:16:02

score: Fix thread deletion on SMP

Close the thread object in _Thread_Make_zombie() so that all blocking
operations that use _Thread_Get() in the corresponding release directive
can find a terminating thread and can complete the operation.

  • Property mode set to 100644
File size: 11.3 KB
Line 
1/*
2 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include "tmacros.h"
20
21#include <rtems.h>
22#include <rtems/counter.h>
23#include <rtems/libcsupport.h>
24#include <rtems/score/profiling.h>
25#include <rtems/score/smpbarrier.h>
26#include <rtems/score/threadimpl.h>
27
28const char rtems_test_name[] = "SMPTHREADLIFE 1";
29
30#define CPU_COUNT 2
31
32typedef struct {
33  volatile rtems_task_argument main_arg;
34  volatile rtems_task_argument worker_arg;
35  volatile bool terminated;
36  SMP_barrier_Control barrier;
37  SMP_barrier_State main_barrier_state;
38  SMP_barrier_State worker_barrier_state;
39  Thread_Control *delay_switch_for_executing;
40  rtems_id worker_id;
41} test_context;
42
43static test_context test_instance = {
44  .barrier = SMP_BARRIER_CONTROL_INITIALIZER,
45  .main_barrier_state = SMP_BARRIER_STATE_INITIALIZER,
46  .worker_barrier_state = SMP_BARRIER_STATE_INITIALIZER
47};
48
49static void barrier(test_context *ctx, SMP_barrier_State *state)
50{
51  _SMP_barrier_Wait(&ctx->barrier, state, CPU_COUNT);
52}
53
54static void restart_extension(
55  Thread_Control *executing,
56  Thread_Control *restarted
57)
58{
59  rtems_test_assert(executing == restarted);
60}
61
62static void delete_extension(
63  Thread_Control *executing,
64  Thread_Control *deleted
65)
66{
67  rtems_test_assert(executing != deleted);
68}
69
70static void terminate_extension(Thread_Control *executing)
71{
72  test_context *ctx = &test_instance;
73
74  ctx->terminated = true;
75}
76
77static void switch_extension(Thread_Control *executing, Thread_Control *heir)
78{
79  test_context *ctx = &test_instance;
80
81  if (ctx->delay_switch_for_executing == executing) {
82    ctx->delay_switch_for_executing = NULL;
83
84    /* (A) */
85    barrier(ctx, &ctx->worker_barrier_state);
86
87    rtems_counter_delay_nanoseconds(100000000);
88
89    /* Avoid bad profiling statisitics */
90    _Profiling_Thread_dispatch_disable( _Per_CPU_Get(), 0 );
91  }
92}
93
94static void worker_task(rtems_task_argument arg)
95{
96  test_context *ctx = &test_instance;
97
98  rtems_test_assert(arg == ctx->main_arg);
99
100  ctx->worker_arg = arg;
101
102  /* (B) */
103  barrier(ctx, &ctx->worker_barrier_state);
104
105  while (true) {
106    /* Do nothing */
107  }
108}
109
110static void test_restart(void)
111{
112  test_context *ctx = &test_instance;
113  rtems_status_code sc;
114  rtems_id id;
115  rtems_task_argument arg;
116  rtems_resource_snapshot snapshot;
117
118  rtems_resource_snapshot_take(&snapshot);
119
120  sc = rtems_task_create(
121    rtems_build_name('W', 'O', 'R', 'K'),
122    1,
123    RTEMS_MINIMUM_STACK_SIZE,
124    RTEMS_DEFAULT_MODES,
125    RTEMS_DEFAULT_ATTRIBUTES,
126    &id
127  );
128  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
129
130  sc = rtems_task_start(id, worker_task, 0);
131  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
132
133  /* (B) */
134  barrier(ctx, &ctx->main_barrier_state);
135
136  for (arg = 1; arg < 23; ++arg) {
137    ctx->main_arg = arg;
138    ctx->worker_arg = 0;
139
140    sc = rtems_task_restart(id, arg);
141    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
142
143    /* (B) */
144    barrier(ctx, &ctx->main_barrier_state);
145
146    rtems_test_assert(ctx->worker_arg == arg);
147  }
148
149  sc = rtems_task_delete(id);
150  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
151
152  rtems_test_assert(rtems_resource_snapshot_check(&snapshot));
153}
154
155static void test_delete(void)
156{
157  test_context *ctx = &test_instance;
158  rtems_status_code sc;
159  rtems_id id;
160  rtems_task_argument arg;
161  rtems_resource_snapshot snapshot;
162
163  rtems_resource_snapshot_take(&snapshot);
164
165  for (arg = 31; arg < 57; ++arg) {
166    ctx->main_arg = arg;
167    ctx->worker_arg = 0;
168    ctx->terminated = false;
169
170    sc = rtems_task_create(
171      rtems_build_name('W', 'O', 'R', 'K'),
172      1,
173      RTEMS_MINIMUM_STACK_SIZE,
174      RTEMS_DEFAULT_MODES,
175      RTEMS_DEFAULT_ATTRIBUTES,
176      &id
177    );
178    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
179
180    sc = rtems_task_start(id, worker_task, arg);
181    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
182
183    /* (B) */
184    barrier(ctx, &ctx->main_barrier_state);
185
186    rtems_test_assert(ctx->worker_arg == arg);
187    rtems_test_assert(!ctx->terminated);
188
189    sc = rtems_task_delete(id);
190    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
191
192    rtems_test_assert(ctx->terminated);
193
194    rtems_test_assert(rtems_resource_snapshot_check(&snapshot));
195  }
196}
197
198static void delay_ipi_task(rtems_task_argument variant)
199{
200  test_context *ctx = &test_instance;
201  ISR_Level level;
202
203  _ISR_Disable_without_giant(level);
204  (void) level;
205
206  /* (C) */
207  barrier(ctx, &ctx->worker_barrier_state);
208
209  /*
210   * Interrupts are disabled, so the inter-processor interrupt deleting us will
211   * be delayed a bit.
212   */
213  rtems_counter_delay_nanoseconds(100000000);
214
215  if (variant != 0) {
216    _Thread_Disable_dispatch();
217  }
218
219  /*
220   * We get deleted as a side effect of enabling the thread life protection or
221   * later if we enable the thread dispatching.
222   */
223  _Thread_Set_life_protection(true);
224
225  if (variant != 0) {
226    _Thread_Enable_dispatch();
227  }
228
229  rtems_test_assert(0);
230}
231
232static void test_set_life_protection(rtems_task_argument variant)
233{
234  test_context *ctx = &test_instance;
235  rtems_status_code sc;
236  rtems_id id;
237  rtems_resource_snapshot snapshot;
238
239  rtems_resource_snapshot_take(&snapshot);
240
241  sc = rtems_task_create(
242    rtems_build_name('D', 'E', 'L', 'Y'),
243    1,
244    RTEMS_MINIMUM_STACK_SIZE,
245    RTEMS_DEFAULT_MODES,
246    RTEMS_DEFAULT_ATTRIBUTES,
247    &id
248  );
249  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
250
251  sc = rtems_task_start(id, delay_ipi_task, variant);
252  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
253
254  /* (C) */
255  barrier(ctx, &ctx->main_barrier_state);
256
257  sc = rtems_task_delete(id);
258  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
259
260  rtems_test_assert(rtems_resource_snapshot_check(&snapshot));
261}
262
263static void delay_switch_task(rtems_task_argument arg)
264{
265  test_context *ctx = &test_instance;
266  rtems_status_code sc;
267  ISR_Level level;
268
269  _ISR_Disable_without_giant(level);
270  (void) level;
271
272  ctx->delay_switch_for_executing = _Thread_Get_executing();
273
274  /* (D) */
275  barrier(ctx, &ctx->worker_barrier_state);
276
277  sc = rtems_task_delete(RTEMS_SELF);
278  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
279}
280
281static void test_wait_for_execution_stop(void)
282{
283  test_context *ctx = &test_instance;
284  rtems_status_code sc;
285  rtems_id id;
286  rtems_resource_snapshot snapshot;
287
288  rtems_resource_snapshot_take(&snapshot);
289
290  sc = rtems_task_create(
291    rtems_build_name('S', 'W', 'I', 'T'),
292    1,
293    RTEMS_MINIMUM_STACK_SIZE,
294    RTEMS_DEFAULT_MODES,
295    RTEMS_DEFAULT_ATTRIBUTES,
296    &id
297  );
298  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
299
300  sc = rtems_task_start(id, delay_switch_task, 0);
301  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
302
303  /* (D) */
304  barrier(ctx, &ctx->main_barrier_state);
305
306  /* (A) */
307  barrier(ctx, &ctx->main_barrier_state);
308
309  sc = rtems_task_create(
310    rtems_build_name('W', 'A', 'I', 'T'),
311    1,
312    RTEMS_MINIMUM_STACK_SIZE,
313    RTEMS_DEFAULT_MODES,
314    RTEMS_DEFAULT_ATTRIBUTES,
315    &id
316  );
317  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
318
319  sc = rtems_task_delete(id);
320  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
321
322  rtems_test_assert(rtems_resource_snapshot_check(&snapshot));
323}
324
325typedef enum {
326  TEST_OP_SUSPEND,
327  TEST_OP_EVENT,
328  TEST_OP_EVENT_SYSTEM
329} test_op;
330
331static void op_begin_suspend(void)
332{
333  rtems_task_suspend(RTEMS_SELF);
334  rtems_test_assert(0);
335}
336
337static void op_begin_event(void)
338{
339  rtems_event_set events;
340
341  rtems_event_receive(
342    RTEMS_EVENT_0,
343    RTEMS_EVENT_ALL | RTEMS_WAIT,
344    RTEMS_NO_TIMEOUT,
345    &events
346  );
347  rtems_test_assert(0);
348}
349
350static void op_begin_event_system(void)
351{
352  rtems_event_set events;
353
354  rtems_event_system_receive(
355    RTEMS_EVENT_0,
356    RTEMS_EVENT_ALL | RTEMS_WAIT,
357    RTEMS_NO_TIMEOUT,
358    &events
359  );
360  rtems_test_assert(0);
361}
362
363static void (*const test_ops_begin[])(void) = {
364  op_begin_suspend,
365  op_begin_event,
366  op_begin_event_system
367};
368
369static void op_end_suspend(test_context *ctx)
370{
371  rtems_status_code sc;
372
373  sc = rtems_task_resume(ctx->worker_id);
374  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
375}
376
377static void op_end_event(test_context *ctx)
378{
379  rtems_status_code sc;
380
381  sc = rtems_event_send(ctx->worker_id, RTEMS_EVENT_0);
382  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
383}
384
385static void op_end_event_system(test_context *ctx)
386{
387  rtems_status_code sc;
388
389  sc = rtems_event_system_send(ctx->worker_id, RTEMS_EVENT_0);
390  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
391}
392
393static void (*const test_ops_end[])(test_context *) = {
394  op_end_suspend,
395  op_end_event,
396  op_end_event_system
397};
398
399static void op_worker_task(rtems_task_argument arg)
400{
401  test_context *ctx = &test_instance;
402  test_op op = arg;
403  ISR_Level level;
404
405  _ISR_Disable_without_giant(level);
406  (void) level;
407
408  /* (E) */
409  barrier(ctx, &ctx->worker_barrier_state);
410
411  /* (F) */
412  barrier(ctx, &ctx->worker_barrier_state);
413
414  (*test_ops_begin[op])();
415}
416
417static void help_task(rtems_task_argument arg)
418{
419  test_context *ctx = &test_instance;
420  test_op op = arg;
421
422  /* (F) */
423  barrier(ctx, &ctx->main_barrier_state);
424
425  rtems_counter_delay_nanoseconds(100000000);
426
427  (*test_ops_end[op])(ctx);
428
429  rtems_task_suspend(RTEMS_SELF);
430  rtems_test_assert(0);
431}
432
433static void test_operation_with_delete_in_progress(test_op op)
434{
435  test_context *ctx = &test_instance;
436  rtems_status_code sc;
437  rtems_id help_id;
438  rtems_resource_snapshot snapshot;
439
440  rtems_resource_snapshot_take(&snapshot);
441
442  sc = rtems_task_create(
443    rtems_build_name('W', 'O', 'R', 'K'),
444    1,
445    RTEMS_MINIMUM_STACK_SIZE,
446    RTEMS_DEFAULT_MODES,
447    RTEMS_DEFAULT_ATTRIBUTES,
448    &ctx->worker_id
449  );
450  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
451
452  sc = rtems_task_start(ctx->worker_id, op_worker_task, op);
453  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
454
455  /* (E) */
456  barrier(ctx, &ctx->main_barrier_state);
457
458  sc = rtems_task_create(
459    rtems_build_name('H', 'E', 'L', 'P'),
460    2,
461    RTEMS_MINIMUM_STACK_SIZE,
462    RTEMS_DEFAULT_MODES,
463    RTEMS_DEFAULT_ATTRIBUTES,
464    &help_id
465  );
466  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
467
468  sc = rtems_task_start(help_id, help_task, op);
469  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
470
471  sc = rtems_task_delete(ctx->worker_id);
472  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
473
474  sc = rtems_task_delete(help_id);
475  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
476
477  rtems_test_assert(rtems_resource_snapshot_check(&snapshot));
478}
479
480static void Init(rtems_task_argument arg)
481{
482  TEST_BEGIN();
483
484  if (rtems_get_processor_count() >= CPU_COUNT) {
485    test_restart();
486    test_delete();
487    test_set_life_protection(0);
488    test_set_life_protection(1);
489    test_wait_for_execution_stop();
490    test_operation_with_delete_in_progress(TEST_OP_SUSPEND);
491    test_operation_with_delete_in_progress(TEST_OP_EVENT);
492    test_operation_with_delete_in_progress(TEST_OP_EVENT_SYSTEM);
493  }
494
495  TEST_END();
496  rtems_test_exit(0);
497}
498
499#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
500#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
501
502#define CONFIGURE_SMP_APPLICATION
503
504#define CONFIGURE_SMP_MAXIMUM_PROCESSORS CPU_COUNT
505
506#define CONFIGURE_MAXIMUM_TASKS (CPU_COUNT + 1)
507
508#define CONFIGURE_INITIAL_EXTENSIONS \
509  { \
510    .thread_restart = restart_extension, \
511    .thread_delete = delete_extension, \
512    .thread_terminate = terminate_extension, \
513    .thread_switch = switch_extension \
514  }, \
515  RTEMS_TEST_INITIAL_EXTENSION
516
517#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
518
519#define CONFIGURE_INIT
520
521#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.