File: test_pthread_gcc_atomic_op_and_fetch.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (163 lines) | stat: -rw-r--r-- 5,215 bytes parent folder | download | duplicates (2)
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
// Copyright 2015 The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <stdio.h>
#include <pthread.h>
#include <emscripten.h>
#include <emscripten/threading.h>

// This file tests the old GCC built-in atomic operations of the form __sync_op_and_fetch().
// See https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Atomic-Builtins.html

#define NUM_THREADS 8

#define T int

void *thread_add_and_fetch(void *arg) {
  for (int i = 0; i < 10000; ++i) {
    __sync_add_and_fetch((int*)arg, 1);
  }
  pthread_exit(0);
}

void *thread_sub_and_fetch(void *arg) {
  for (int i = 0; i < 10000; ++i) {
    __sync_sub_and_fetch((int*)arg, 1);
  }
  pthread_exit(0);
}

volatile long or_and_fetch_data = 0;
void *thread_or_and_fetch(void *arg) {
  for (int i = 0; i < 10000; ++i) {
    __sync_or_and_fetch((long*)&or_and_fetch_data, (long)arg);
  }
  pthread_exit(0);
}

volatile long and_and_fetch_data = 0;
void *thread_and_and_fetch(void *arg) {
  for (int i = 0; i < 10000; ++i) {
    __sync_and_and_fetch((long*)&and_and_fetch_data, (long)arg);
  }
  pthread_exit(0);
}

volatile long xor_and_fetch_data = 0;
void *thread_xor_and_fetch(void *arg) {
  for (int i = 0; i < 9999; ++i) // Odd number of times so that the operation doesn't cancel itself out.
    __sync_xor_and_fetch((long*)&xor_and_fetch_data, (long)arg);
  pthread_exit(0);
}

volatile long nand_and_fetch_data = 0;
void *thread_nand_and_fetch(void *arg) {
  for (int i = 0; i < 9999; ++i) { // Odd number of times so that the operation doesn't cancel itself out.
    __sync_nand_and_fetch((long*)&nand_and_fetch_data, (long)arg);
  }
  pthread_exit(0);
}

int main() {
  pthread_t thread[NUM_THREADS];

  {
    printf("__sync_add_and_fetch ..\n");
    T x = 5;
    T y = __sync_add_and_fetch(&x, 10);
    assert(y == 15);
    assert(x == 15);
    volatile int n = 1;
    if (emscripten_has_threading_support())
    {
      for (int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_add_and_fetch, (void*)&n);
      for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
      assert(n == NUM_THREADS*10000+1);
    }
  }
  {
    printf("__sync_sub_and_fetch ..\n");
    T x = 5;
    T y = __sync_sub_and_fetch(&x, 10);
    assert(y == -5);
    assert(x == -5);
    volatile int n = 1;
    if (emscripten_has_threading_support())
    {
      for (int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_sub_and_fetch, (void*)&n);
      for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
      assert(n == 1-NUM_THREADS*10000);
    }
  }
  {
    printf("__sync_or_and_fetch ..\n");
    T x = 5;
    T y = __sync_or_and_fetch(&x, 9);
    assert(y == 13);
    assert(x == 13);
    for (int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
    {
      or_and_fetch_data = (1<<NUM_THREADS);
      if (emscripten_has_threading_support())
      {
        for (int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_or_and_fetch, (void*)(1ll<<i));
        for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
        assert(or_and_fetch_data == (1<<(NUM_THREADS+1))-1);
      }
    }
  }
  {
    printf("__sync_and_and_fetch ..\n");
    T x = 5;
    T y = __sync_and_and_fetch(&x, 9);
    assert(y == 1);
    assert(x == 1);
    for (int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
    {
      and_and_fetch_data = (1<<(NUM_THREADS+1))-1;
      if (emscripten_has_threading_support())
      {
        for (int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_and_and_fetch, (void*)(~(1ll<<i)));
        for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
        assert(and_and_fetch_data == 1<<NUM_THREADS);
      }
    }
  }
  {
    printf("__sync_xor_and_fetch ..\n");
    T x = 5;
    T y = __sync_xor_and_fetch(&x, 9);
    assert(y == 12);
    assert(x == 12);
    for (int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
    {
      xor_and_fetch_data = 1<<NUM_THREADS;
      if (emscripten_has_threading_support())
      {
        for (int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_xor_and_fetch, (void*)(~(1ll<<i)));
        for (int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
        assert(xor_and_fetch_data == (1<<(NUM_THREADS+1))-1);
      }
    }
  }
  {
    printf("__sync_nand_and_fetch ..\n");
    T x = 5;
    T y = __sync_nand_and_fetch(&x, 9);
    assert(y == -2);
    assert(x == -2);
    const int oddNThreads = NUM_THREADS-1;
    for (int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
    {
      nand_and_fetch_data = 0;
      for (int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch, (void*)-1);
      for (int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL);
      assert(nand_and_fetch_data == -1);
    }
  }

  return 0;
}