summaryrefslogtreecommitdiff
path: root/tests/test-simple-atomic.c
blob: 04baf054b1c4592f421e472b7a3926c462fdd02e (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
224
225
226
227
228
229
230
231
232
233
234
/* Test of simple atomic operations for multithreading.
   Copyright (C) 2021 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2021.  */

#include <config.h>

#include "simple-atomic.h"

#if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS

/* Whether to help the scheduler through explicit yield().
   Uncomment this to see if the operating system has a fair scheduler.  */
#define EXPLICIT_YIELD 1

/* Number of simultaneous threads.  */
#define THREAD_COUNT 4

/* Number of operations performed in each thread.
   With a smaller count, say 100, we often get an "OK" result even with the racy
   implementation.  */
#define REPEAT_COUNT 1000

#if EXPLICIT_YIELD
# define yield() gl_thread_yield ()
#else
# define yield()
#endif

#include "glthread/thread.h"
#include "glthread/yield.h"

#include "macros.h"

/* Counters for each thread.  */
static unsigned int counter[THREAD_COUNT][5];

/* A variable of type 'unsigned int'.  */
static unsigned int int_variable;

static void *
int_mutator_thread (void *arg)
{
  int *pcounter = (int *) arg;
  int repeat;

  for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
    {
      if (atomic_compare_and_swap (&int_variable, 0, 10) == 0)
        pcounter[0]++;
      yield ();

      if (atomic_compare_and_swap (&int_variable, 14, 17) == 14)
        pcounter[1]++;
      yield ();

      if (atomic_compare_and_swap (&int_variable, 20, 0) == 20)
        pcounter[2]++;
      yield ();

      if (atomic_compare_and_swap (&int_variable, 10, 14) == 10)
        pcounter[3]++;
      yield ();

      if (atomic_compare_and_swap (&int_variable, 17, 20) == 17)
        pcounter[4]++;
      yield ();
    }

  return NULL;
}

/* A variable of type 'uintptr_t'.  */
static uintptr_t ptr_variable;

static void *
ptr_mutator_thread (void *arg)
{
  int *pcounter = (int *) arg;
  int repeat;

  for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
    {
      if (atomic_compare_and_swap_ptr (&ptr_variable, 0, 10) == 0)
        pcounter[0]++;
      yield ();

      if (atomic_compare_and_swap_ptr (&ptr_variable, 14, 17) == 14)
        pcounter[1]++;
      yield ();

      if (atomic_compare_and_swap_ptr (&ptr_variable, 20, 0) == 20)
        pcounter[2]++;
      yield ();

      if (atomic_compare_and_swap_ptr (&ptr_variable, 10, 14) == 10)
        pcounter[3]++;
      yield ();

      if (atomic_compare_and_swap_ptr (&ptr_variable, 17, 20) == 17)
        pcounter[4]++;
      yield ();
    }

  return NULL;
}

int
main ()
{
  /* Check simple uses of atomic_compare_and_swap.  */
  {
    unsigned int x[3] = { 0xDEADBEEFU, 11, 0xDEADBEEFU };

    ASSERT (atomic_compare_and_swap (&x[1], 0, 17) == 11);
    ASSERT (x[1] == 11);

    ASSERT (atomic_compare_and_swap (&x[1], 4, 11) == 11);
    ASSERT (x[1] == 11);

    ASSERT (atomic_compare_and_swap (&x[1], 11, 15) == 11);
    ASSERT (x[1] == 15);

    ASSERT (x[0] == 0xDEADBEEFU);
    ASSERT (x[2] == 0xDEADBEEFU);
  }

  /* Check simple uses of atomic_compare_and_swap_ptr.  */
  {
    uintptr_t v1 = ~(uintptr_t)0 / 3;
    uintptr_t v2 = ~(uintptr_t)0 / 5 * 4;
    uintptr_t v3 = ~(uintptr_t)0 / 7 * 3;
    uintptr_t x[3] = { 0xDEADBEEFU, v1, 0xDEADBEEFU };

    ASSERT (atomic_compare_and_swap_ptr (&x[1], 0, v3) == v1);
    ASSERT (x[1] == v1);

    ASSERT (atomic_compare_and_swap_ptr (&x[1], 4, v1) == v1);
    ASSERT (x[1] == v1);

    ASSERT (atomic_compare_and_swap_ptr (&x[1], v1, v2) == v1);
    ASSERT (x[1] == v2);

    ASSERT (x[0] == 0xDEADBEEFU);
    ASSERT (x[2] == 0xDEADBEEFU);
  }

  /* Check atomicity of atomic_compare_and_swap.  */
  {
    void * (*funcs[2]) (void *) = { int_mutator_thread, ptr_mutator_thread };
    int f;
    for (f = 0; f < 2; f++)
      {
        void * (*func) (void *) = funcs[f];
        int i, j;
        gl_thread_t threads[THREAD_COUNT];

        /* Initialization.  */
        for (i = 0; i < THREAD_COUNT; i++)
          for (j = 0; j < 5; j++)
            counter[i][j] = 0;

        /* Spawn the threads.  */
        for (i = 0; i < THREAD_COUNT; i++)
          threads[i] = gl_thread_create (func, &counter[i][0]);

        /* Wait for the threads to terminate.  */
        for (i = 0; i < THREAD_COUNT; i++)
          gl_thread_join (threads[i], NULL);

        /* Sum up the work that the threads have done.  */
        unsigned int sum[5];
        for (j = 0; j < 5; j++)
          {
            sum[j] = 0;
            for (i = 0; i < THREAD_COUNT; i++)
              sum[j] += counter[i][j];
          }

        /* If things went atomically, the threads have moved the variable's
           value through the cycle  0 -> 10 -> 14 -> 17 -> 20 -> 0 ...  a large
           number of times.
           sum[0] is the number of transitions 0 -> 10.
           sum[3] is the number of transitions 10 -> 14.
           sum[1] is the number of transitions 14 -> 17.
           sum[4] is the number of transitions 17 -> 20.
           sum[2] is the number of transitions 20 -> 0.
           Since the cycle started at 0 and ends anywhere (namely, when all
           threads when through their loop REPEAT_COUNT times), the sequence
             sum[0], sum[3], sum[1], sum[4], sum[2], sum[0] - 1
           must be monotonically decreasing.

           If things did not go atomically, the counters don't exhibit this
           pattern.  */
        printf ("Counters: %u %u %u %u %u\n",
                sum[0], sum[3], sum[1], sum[4], sum[2]);
        ASSERT ((sum[0] == sum[3] || sum[0] == sum[3] + 1)
                && (sum[3] == sum[1] || sum[3] == sum[1] + 1)
                && (sum[1] == sum[4] || sum[1] == sum[4] + 1)
                && (sum[4] == sum[2] || sum[4] == sum[2] + 1)
                && (sum[2] + 1 == sum[0] || sum[2] == sum[0]));
      }
  }

  return 0;
}

#else

/* No multithreading available.  */

#include <stdio.h>

int
main ()
{
  fputs ("Skipping test: multithreading not enabled\n", stderr);
  return 77;
}

#endif

Return to:

Send suggestions and report system problems to the System administrator.