summaryrefslogtreecommitdiffstats
path: root/testsuites/smptests/smpatomic03/tasks.c
blob: fdf81ab1fc8aae1e8e94a087b194ffd67d3519b3 (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
/*
 * Copyright (c) 2012 Deng Hengyi.
 *
 *  This test case is to test atomic add operation.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 */

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

#include "system.h"

#include <stdlib.h>
#include <rtems/rtems/atomic.h>

#define TEST_REPEAT 200000

#define ATOMIC_FETCH_ADD_NO_BARRIER(NAME, TYPE, cpuid, mem_bar)\
{                                                        \
  Atomic_##TYPE t = 0, a = 0, b = 0;                     \
  unsigned int i;                                        \
  for (i = 0; i < TEST_REPEAT; i++){                     \
    a = (Atomic_##TYPE)(rand() % ((Atomic_##TYPE)-1 / 2));  \
    b = (Atomic_##TYPE)(rand() % ((Atomic_##TYPE)-1 / 2));  \
    t = a;                                               \
    _Atomic_Fetch_add_##NAME(&t, b, mem_bar);            \
    rtems_test_assert(t == (Atomic_##TYPE)(a + b));      \
  }                                                      \
  locked_printf("\nCPU%d _Atomic_Fetch_add_" #NAME ": SUCCESS\n", cpuid); \
}

rtems_task Test_task(
    rtems_task_argument argument
    )
{
  int               cpu_num;
  char              name[5];
  char             *p;

  /* Get the task name */
  p = rtems_object_get_name( RTEMS_SELF, 5, name );
  rtems_test_assert( p != NULL );

   /* Get the CPU Number */
  cpu_num = bsp_smp_processor_id();

  /* Print that the task is up and running. */
  /* test relaxed barrier */
  ATOMIC_FETCH_ADD_NO_BARRIER(int, Int, cpu_num, ATOMIC_RELAXED_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(long, Long, cpu_num, ATOMIC_RELAXED_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(ptr, Pointer, cpu_num, ATOMIC_RELAXED_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(32, Int32, cpu_num, ATOMIC_RELAXED_BARRIER);

  /* test acquire barrier */
  ATOMIC_FETCH_ADD_NO_BARRIER(int, Int, cpu_num, ATOMIC_ACQUIRE_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(long, Long, cpu_num, ATOMIC_ACQUIRE_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(ptr, Pointer, cpu_num, ATOMIC_ACQUIRE_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(32, Int32, cpu_num, ATOMIC_ACQUIRE_BARRIER);

  /* test release barrier */
  ATOMIC_FETCH_ADD_NO_BARRIER(int, Int, cpu_num, ATOMIC_RELEASE_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(long, Long, cpu_num, ATOMIC_RELEASE_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(ptr, Pointer, cpu_num, ATOMIC_RELEASE_BARRIER);

  ATOMIC_FETCH_ADD_NO_BARRIER(32, Int32, cpu_num, ATOMIC_RELEASE_BARRIER);

//  ATOMIC_FETCH_ADD_NO_BARRIER(64, cpu_num);

  /* Set the flag that the task is up and running */
  TaskRan[cpu_num] = true;

  /* Drop into a loop which will keep this task on
   * running on the cpu.
   */
  while(1);
}