X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Ftest%2Fsimple_spin.cc;fp=src%2Fceph%2Fsrc%2Ftest%2Fsimple_spin.cc;h=04b4fc07e5f6b6033d0196ac6ab16f6eca571d03;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/test/simple_spin.cc b/src/ceph/src/test/simple_spin.cc new file mode 100644 index 0000000..04b4fc0 --- /dev/null +++ b/src/ceph/src/test/simple_spin.cc @@ -0,0 +1,58 @@ +#include "gtest/gtest.h" + +#include "common/simple_spin.h" + +#include + +TEST(SimpleSpin, Test0) +{ + std::atomic_flag lock0 = ATOMIC_FLAG_INIT; + simple_spin_lock(&lock0); + simple_spin_unlock(&lock0); +} + +static std::atomic_flag lock = ATOMIC_FLAG_INIT; +static uint32_t counter = 0; + +static void* mythread(void *v) +{ + for (int j = 0; j < 1000000; ++j) { + simple_spin_lock(&lock); + counter++; + simple_spin_unlock(&lock); + } + return NULL; +} + +TEST(SimpleSpin, Test1) +{ + counter = 0; + const auto n = 2000000U; + + int ret; + pthread_t thread1; + pthread_t thread2; + ret = pthread_create(&thread1, NULL, mythread, NULL); + ASSERT_EQ(0, ret); + ret = pthread_create(&thread2, NULL, mythread, NULL); + ASSERT_EQ(0, ret); + ret = pthread_join(thread1, NULL); + ASSERT_EQ(0, ret); + ret = pthread_join(thread2, NULL); + ASSERT_EQ(0, ret); + ASSERT_EQ(n, counter); + + + // Should also work with pass-by-reference: + // (Note that we don't care about cross-threading here as-such.) + counter = 0; + async(std::launch::async, []() { + for(int i = 0; n != i; ++i) { + simple_spin_lock(lock); + counter++; + simple_spin_unlock(lock); + } + }); + ASSERT_EQ(n, counter); +} +