1 #include "gtest/gtest.h"
3 #include "common/simple_spin.h"
7 TEST(SimpleSpin, Test0)
9 std::atomic_flag lock0 = ATOMIC_FLAG_INIT;
10 simple_spin_lock(&lock0);
11 simple_spin_unlock(&lock0);
14 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
15 static uint32_t counter = 0;
17 static void* mythread(void *v)
19 for (int j = 0; j < 1000000; ++j) {
20 simple_spin_lock(&lock);
22 simple_spin_unlock(&lock);
27 TEST(SimpleSpin, Test1)
30 const auto n = 2000000U;
35 ret = pthread_create(&thread1, NULL, mythread, NULL);
37 ret = pthread_create(&thread2, NULL, mythread, NULL);
39 ret = pthread_join(thread1, NULL);
41 ret = pthread_join(thread2, NULL);
43 ASSERT_EQ(n, counter);
46 // Should also work with pass-by-reference:
47 // (Note that we don't care about cross-threading here as-such.)
49 async(std::launch::async, []() {
50 for(int i = 0; n != i; ++i) {
51 simple_spin_lock(lock);
53 simple_spin_unlock(lock);
56 ASSERT_EQ(n, counter);