]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/simple_spin.cc
bump version to 12.2.4-pve1
[ceph.git] / ceph / src / test / simple_spin.cc
1 #include "gtest/gtest.h"
2
3 #include "common/simple_spin.h"
4
5 #include <future>
6
7 TEST(SimpleSpin, Test0)
8 {
9 std::atomic_flag lock0 = ATOMIC_FLAG_INIT;
10 simple_spin_lock(&lock0);
11 simple_spin_unlock(&lock0);
12 }
13
14 static std::atomic_flag lock = ATOMIC_FLAG_INIT;
15 static uint32_t counter = 0;
16
17 static void* mythread(void *v)
18 {
19 for (int j = 0; j < 1000000; ++j) {
20 simple_spin_lock(&lock);
21 counter++;
22 simple_spin_unlock(&lock);
23 }
24 return NULL;
25 }
26
27 TEST(SimpleSpin, Test1)
28 {
29 counter = 0;
30 const auto n = 2000000U;
31
32 int ret;
33 pthread_t thread1;
34 pthread_t thread2;
35 ret = pthread_create(&thread1, NULL, mythread, NULL);
36 ASSERT_EQ(0, ret);
37 ret = pthread_create(&thread2, NULL, mythread, NULL);
38 ASSERT_EQ(0, ret);
39 ret = pthread_join(thread1, NULL);
40 ASSERT_EQ(0, ret);
41 ret = pthread_join(thread2, NULL);
42 ASSERT_EQ(0, ret);
43 ASSERT_EQ(n, counter);
44
45
46 // Should also work with pass-by-reference:
47 // (Note that we don't care about cross-threading here as-such.)
48 counter = 0;
49 async(std::launch::async, []() {
50 for(int i = 0; n != i; ++i) {
51 simple_spin_lock(lock);
52 counter++;
53 simple_spin_unlock(lock);
54 }
55 });
56 ASSERT_EQ(n, counter);
57 }
58