Changeset e127c4c in rtems


Ignore:
Timestamp:
Aug 29, 2013, 8:03:38 AM (6 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, master
Children:
1215fd4
Parents:
79d03e3
git-author:
Sebastian Huber <sebastian.huber@…> (08/29/13 08:03:38)
git-committer:
Sebastian Huber <sebastian.huber@…> (08/29/13 08:05:16)
Message:

smptests/smpatomic08: Fix race conditions

Location:
testsuites/smptests/smpatomic08
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • testsuites/smptests/smpatomic08/init.c

    r79d03e3 re127c4c  
    8484} test_context;
    8585
    86 typedef void (*test_case_init)(test_context *ctx);
    87 
    88 typedef void (*test_case)(test_context *ctx, size_t worker_index);
     86typedef struct {
     87  void (*init)(test_context *ctx);
     88  void (*body)(test_context *ctx, size_t worker_index);
     89  void (*fini)(test_context *ctx);
     90} test_case;
    8991
    9092static test_context test_instance = {
     
    103105}
    104106
    105 static void test_init_atomic_add(test_context *ctx)
     107static void test_atomic_add_init(test_context *ctx)
    106108{
    107109  _Atomic_Init_uint(&ctx->global_uint, 0);
    108110}
    109111
    110 static void test_atomic_add_report(test_context *ctx)
     112static void test_atomic_add_body(test_context *ctx, size_t worker_index)
     113{
     114  uint_fast32_t counter = 0;
     115
     116  while (!stop(ctx)) {
     117    ++counter;
     118    _Atomic_Fetch_add_uint(&ctx->global_uint, 1, ATOMIC_ORDER_RELAXED);
     119  }
     120
     121  ctx->per_worker_uint[worker_index] = counter;
     122}
     123
     124static void test_atomic_add_fini(test_context *ctx)
    111125{
    112126  uint_fast32_t expected_counter = 0;
     
    139153}
    140154
    141 static void test_atomic_add(test_context *ctx, size_t worker_index)
     155static void test_atomic_flag_init(test_context *ctx)
     156{
     157  _Atomic_Flag_clear(&ctx->global_flag, ATOMIC_ORDER_RELEASE);
     158  ctx->flag_counter = 0;
     159}
     160
     161static void test_atomic_flag_body(test_context *ctx, size_t worker_index)
    142162{
    143163  uint_fast32_t counter = 0;
    144164
    145165  while (!stop(ctx)) {
     166    while (_Atomic_Flag_test_and_set(&ctx->global_flag, ATOMIC_ORDER_ACQUIRE)) {
     167      /* Wait */
     168    }
     169
    146170    ++counter;
    147     _Atomic_Fetch_add_uint(&ctx->global_uint, 1, ATOMIC_ORDER_RELAXED);
     171    ++ctx->flag_counter;
     172
     173    _Atomic_Flag_clear(&ctx->global_flag, ATOMIC_ORDER_RELEASE);
    148174  }
    149175
    150176  ctx->per_worker_uint[worker_index] = counter;
    151 
    152   if (is_master_worker(worker_index)) {
    153     test_atomic_add_report(ctx);
    154   }
    155 }
    156 
    157 static void test_atomic_flag_report(test_context *ctx)
     177}
     178
     179static void test_atomic_flag_fini(test_context *ctx)
    158180{
    159181  uint_fast32_t expected_counter = 0;
     
    175197  }
    176198
    177   actual_counter = _Atomic_Load_uint(&ctx->global_uint, ATOMIC_ORDER_RELAXED);
     199  actual_counter = ctx->flag_counter;
    178200
    179201  printf(
     
    186208}
    187209
    188 static void test_init_atomic_flag(test_context *ctx)
    189 {
    190   _Atomic_Flag_clear(&ctx->global_flag, ATOMIC_ORDER_RELEASE);
    191   ctx->flag_counter = 0;
    192 }
    193 
    194 static void test_atomic_flag(test_context *ctx, size_t worker_index)
    195 {
    196   uint_fast32_t counter = 0;
    197 
    198   while (!stop(ctx)) {
    199     while (!_Atomic_Flag_test_and_set(&ctx->global_flag, ATOMIC_ORDER_ACQUIRE)) {
    200       /* Wait */
    201     }
    202 
    203     ++counter;
    204     ++ctx->flag_counter;
    205 
    206     _Atomic_Flag_clear(&ctx->global_flag, ATOMIC_ORDER_RELEASE);
    207   }
    208 
    209   ctx->per_worker_uint[worker_index] = counter;
    210 
    211   if (is_master_worker(worker_index)) {
    212     test_atomic_flag_report(ctx);
    213   }
    214 }
    215 
    216 static const test_case_init test_cases_init[] = {
    217   test_init_atomic_add,
    218   test_init_atomic_flag
     210static const test_case test_cases[] = {
     211  { test_atomic_add_init, test_atomic_add_body, test_atomic_add_fini },
     212  { test_atomic_flag_init, test_atomic_flag_body, test_atomic_flag_fini }
    219213};
    220214
    221 #define TEST_COUNT RTEMS_ARRAY_SIZE(test_cases_init)
    222 
    223 static const test_case test_cases[TEST_COUNT] = {
    224   test_atomic_add,
    225   test_atomic_flag
    226 };
     215#define TEST_COUNT RTEMS_ARRAY_SIZE(test_cases)
    227216
    228217static void stop_worker_timer(rtems_id timer_id, void *arg)
     
    254243
    255244  for (test = 0; test < TEST_COUNT; ++test) {
     245    const test_case *tc = &test_cases[test];
     246
    256247    if (is_master_worker(worker_index)) {
    257248      start_worker_stop_timer(ctx);
    258       (*test_cases_init[test])(ctx);
     249      (*tc->init)(ctx);
    259250    }
    260251
    261252    _SMP_barrier_Wait(&ctx->barrier, &bs, ctx->worker_count);
    262253
    263     (*test_cases[test])(ctx, worker_index);
     254    (*tc->body)(ctx, worker_index);
    264255
    265256    _SMP_barrier_Wait(&ctx->barrier, &bs, ctx->worker_count);
     257
     258    if (is_master_worker(worker_index)) {
     259      (*tc->fini)(ctx);
     260    }
    266261  }
    267262}
  • testsuites/smptests/smpatomic08/smpatomic08.scn

    r79d03e3 re127c4c  
    11*** TEST SMPATOMIC 8 ***
    22=== atomic add test case ==
    3 atomic add worker 0 counter: 23435
    4 atomic add worker 1 counter: 374134
    5 global counter: expected = 397569, actual = 397569
     3atomic add worker 0 counter: 18583
     4atomic add worker 1 counter: 36324
     5global counter: expected = 54907, actual = 54907
     6=== atomic flag test case ===
     7atomic flag worker 0 counter: 7388
     8atomic flag worker 1 counter: 17280
     9global flag counter: expected = 24668, actual = 24668
    610*** END OF TEST SMPATOMIC 8 ***
Note: See TracChangeset for help on using the changeset viewer.