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
|
// 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.
// See https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Atomic-Builtins.html
#define NUM_THREADS 8
#define T int
// TEMP: Fastcomp backend doesn't implement these as atomic, so #define these to library
// implementations that are properly atomic. TODO: Implement these in fastcomp.
#define __sync_lock_test_and_set(...) emscripten_atomic_fence()
#define __sync_lock_release(...) emscripten_atomic_fence()
#define Bool int
Bool atomic_bool_cas_u32(T *ptr, T oldVal, T newVal) {
T old = emscripten_atomic_cas_u32(ptr, oldVal, newVal);
return old == oldVal;
}
// Test __sync_val_compare_and_swap.
T nand_and_fetch(T *ptr, T x) {
for (;;) {
T old = emscripten_atomic_load_u32(ptr);
T newVal = ~(old & x);
T old2 = __sync_val_compare_and_swap(ptr, old, newVal);
if (old2 == old) return newVal;
}
}
// Test __sync_bool_compare_and_swap.
T nand_and_fetch_bool(T *ptr, T x) {
for (;;) {
T old = emscripten_atomic_load_u32(ptr);
T newVal = ~(old & x);
Bool success = __sync_bool_compare_and_swap(ptr, old, newVal);
if (success) return newVal;
}
}
volatile int nand_and_fetch_data = 0;
void *thread_nand_and_fetch(void *arg) {
for (int i = 0; i < 999; ++i) { // Odd number of times so that the operation doesn't cancel itself out.
nand_and_fetch((int*)&nand_and_fetch_data, (int)(long)arg);
}
pthread_exit(0);
}
void *thread_nand_and_fetch_bool(void *arg) {
for (int i = 0; i < 999; ++i) { // Odd number of times so that the operation doesn't cancel itself out.
nand_and_fetch_bool((int*)&nand_and_fetch_data, (int)(long)arg);
}
pthread_exit(0);
}
int main() {
pthread_t thread[NUM_THREADS];
{
T x = 5;
T y = 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;
__sync_synchronize(); // This has no effect in this code, but called in here just to test that the compiler generates a valid expression for this.
if (emscripten_has_threading_support()) {
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);
}
}
}
{
T x = 5;
T y = nand_and_fetch_bool(&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;
if (emscripten_has_threading_support()) {
for (int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch_bool, (void*)-1);
for (int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL);
assert(nand_and_fetch_data == -1);
}
}
}
return 0;
}
|