summaryrefslogtreecommitdiff
path: root/source/tests/atomic.test.c
blob: ca14959352a3e3311b809fc64c85fdb2946c97fa (plain)
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
#include "../kit/atomic.h"
#include "../kit/threads.h"

#define KIT_TEST_FILE atomic
#include "../kit/test.h"

TEST("atomic store and load") {
  int _Atomic value;
  atomic_store_explicit(&value, 20, memory_order_relaxed);
  REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 20);
}

TEST("atomic exchange") {
  int _Atomic value;
  atomic_store_explicit(&value, 20, memory_order_relaxed);
  REQUIRE(atomic_exchange_explicit(&value, 42,
                                   memory_order_relaxed) == 20);
  REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 42);
}

TEST("atomic fetch add") {
  int _Atomic value;
  atomic_store_explicit(&value, 20, memory_order_relaxed);
  REQUIRE(atomic_fetch_add_explicit(&value, 22,
                                    memory_order_relaxed) == 20);
  REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 42);
}

enum {
  TEST_ATOMIC_THREAD_COUNT = 20,
  TEST_ATOMIC_TICK_COUNT   = 10000,
};

static int test_8_(void *p) {
  ptrdiff_t i;
  int8_t _Atomic *x = (int8_t _Atomic *) p;

  for (i = 0; i < TEST_ATOMIC_TICK_COUNT; i++) {
    atomic_fetch_add_explicit(x, 20, memory_order_relaxed);
    thrd_yield();
    atomic_fetch_add_explicit(x, 22, memory_order_relaxed);
    thrd_yield();
    atomic_fetch_add_explicit(x, -42, memory_order_relaxed);
    thrd_yield();
  }

  return 0;
}

TEST("atomic types") {
  int8_t _Atomic b_1;
  int8_t _Atomic b_2;
  int16_t _Atomic i16;
  int32_t _Atomic i32;
  int64_t _Atomic i64;

  atomic_store_explicit(&b_1, 42, memory_order_relaxed);
  atomic_store_explicit(&b_2, 43, memory_order_relaxed);
  atomic_store_explicit(&i16, 4242, memory_order_relaxed);
  atomic_store_explicit(&i32, 42424242, memory_order_relaxed);
  atomic_store_explicit(&i64, 4242424242424242ll,
                        memory_order_relaxed);

  atomic_fetch_add_explicit(&b_1, -20, memory_order_relaxed);
  atomic_fetch_add_explicit(&b_2, -20, memory_order_relaxed);
  atomic_fetch_add_explicit(&i16, -2020, memory_order_relaxed);
  atomic_fetch_add_explicit(&i32, -20202020, memory_order_relaxed);
  atomic_fetch_add_explicit(&i64, -2020202020202020ll,
                            memory_order_relaxed);

  REQUIRE(atomic_exchange_explicit(&b_1, 0, memory_order_relaxed) ==
          22);
  REQUIRE(atomic_exchange_explicit(&b_2, 0, memory_order_relaxed) ==
          23);
  REQUIRE(atomic_exchange_explicit(&i16, 0, memory_order_relaxed) ==
          2222);
  REQUIRE(atomic_exchange_explicit(&i32, 0, memory_order_relaxed) ==
          22222222);
  REQUIRE(atomic_exchange_explicit(&i64, 0, memory_order_relaxed) ==
          2222222222222222ll);

  REQUIRE(atomic_load_explicit(&b_1, memory_order_relaxed) == 0);
  REQUIRE(atomic_load_explicit(&b_2, memory_order_relaxed) == 0);
  REQUIRE(atomic_load_explicit(&i16, memory_order_relaxed) == 0);
  REQUIRE(atomic_load_explicit(&i32, memory_order_relaxed) == 0);
  REQUIRE(atomic_load_explicit(&i64, memory_order_relaxed) == 0ll);
}

TEST("atomic byte concurrency") {
  ptrdiff_t i;

  int8_t _Atomic foo;
  int8_t _Atomic bar;

  atomic_store_explicit(&foo, 42, memory_order_relaxed);
  atomic_store_explicit(&bar, 43, memory_order_relaxed);

  thrd_t threads[TEST_ATOMIC_THREAD_COUNT];
  for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++)
    REQUIRE_EQ(thrd_create(threads + i, test_8_,
                           (void *) ((i % 2) ? &foo : &bar)),
               thrd_success);
  for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++) thrd_join(threads[i], NULL);

  REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42);
  REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43);
}

static int test_16_(void *p) {
  ptrdiff_t i;

  int16_t _Atomic *x = (int16_t _Atomic *) p;

  for (i = 0; i < TEST_ATOMIC_TICK_COUNT; i++) {
    atomic_fetch_add_explicit(x, 2020, memory_order_relaxed);
    thrd_yield();
    atomic_fetch_add_explicit(x, 2222, memory_order_relaxed);
    thrd_yield();
    atomic_fetch_add_explicit(x, -4242, memory_order_relaxed);
    thrd_yield();
  }

  return 0;
}

TEST("atomic int16 concurrency") {
  ptrdiff_t i;

  int16_t _Atomic foo;
  int16_t _Atomic bar;

  atomic_store_explicit(&foo, 42, memory_order_relaxed);
  atomic_store_explicit(&bar, 43, memory_order_relaxed);

  thrd_t threads[TEST_ATOMIC_THREAD_COUNT];
  for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++)
    REQUIRE_EQ(thrd_create(threads + i, test_16_,
                           (void *) ((i % 2) ? &foo : &bar)),
               thrd_success);
  for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++) thrd_join(threads[i], NULL);

  REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42);
  REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43);
}

static int test_32_(void *p) {
  ptrdiff_t i;

  int32_t _Atomic *x = (int32_t _Atomic *) p;

  for (i = 0; i < TEST_ATOMIC_TICK_COUNT; i++) {
    atomic_fetch_add_explicit(x, 202020, memory_order_relaxed);
    thrd_yield();
    atomic_fetch_add_explicit(x, 222222, memory_order_relaxed);
    thrd_yield();
    atomic_fetch_add_explicit(x, -424242, memory_order_relaxed);
    thrd_yield();
  }

  return 0;
}

TEST("atomic int32 concurrency") {
  ptrdiff_t i;

  int32_t _Atomic foo;
  int32_t _Atomic bar;

  atomic_store_explicit(&foo, 42, memory_order_relaxed);
  atomic_store_explicit(&bar, 43, memory_order_relaxed);

  thrd_t threads[TEST_ATOMIC_THREAD_COUNT];
  for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++)
    REQUIRE_EQ(thrd_create(threads + i, test_32_,
                           (void *) ((i % 2) ? &foo : &bar)),
               thrd_success);
  for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++) thrd_join(threads[i], NULL);

  REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42);
  REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43);
}

static int test_64_(void *p) {
  ptrdiff_t i;

  int64_t _Atomic *x = (int64_t _Atomic *) p;

  for (i = 0; i < TEST_ATOMIC_TICK_COUNT; i++) {
    atomic_fetch_add_explicit(x, 20202020202020ll,
                              memory_order_relaxed);
    thrd_yield();
    atomic_fetch_add_explicit(x, 22222222222222ll,
                              memory_order_relaxed);
    thrd_yield();
    atomic_fetch_add_explicit(x, -42424242424242ll,
                              memory_order_relaxed);
    thrd_yield();
  }

  return 0;
}

TEST("atomic int64 concurrency") {
  ptrdiff_t i;

  int64_t _Atomic foo;
  int64_t _Atomic bar;

  atomic_store_explicit(&foo, 42, memory_order_relaxed);
  atomic_store_explicit(&bar, 43, memory_order_relaxed);

  thrd_t threads[TEST_ATOMIC_THREAD_COUNT];
  for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++)
    REQUIRE_EQ(thrd_create(threads + i, test_64_,
                           (void *) ((i % 2) ? &foo : &bar)),
               thrd_success);
  for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++) thrd_join(threads[i], NULL);

  REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42);
  REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43);
}

#undef KIT_TEST_FILE