summaryrefslogtreecommitdiff
path: root/test/monitor/gtest_spinlock.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/monitor/gtest_spinlock.cpp')
-rw-r--r--test/monitor/gtest_spinlock.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/monitor/gtest_spinlock.cpp b/test/monitor/gtest_spinlock.cpp
new file mode 100644
index 0000000..6bf2aa7
--- /dev/null
+++ b/test/monitor/gtest_spinlock.cpp
@@ -0,0 +1,49 @@
+#include <unistd.h>
+#include <pthread.h>
+#include <getopt.h>
+#include <gtest/gtest.h>
+#include "monitor/monitor_private.h"
+
+static volatile long sum = 0;
+static volatile long barrier = 1;
+#define CALC_NUM 10000000
+static void *calc_thread(void *arg)
+{
+ stm_spinlock *lock = (stm_spinlock *)arg;
+ (void)lock;
+ while (barrier)
+ ;
+ for (int i = 0; i < CALC_NUM; i++)
+ {
+ stm_spinlock_lock(lock);
+ sum++;
+ stm_spinlock_unlock(lock);
+ }
+
+ return NULL;
+}
+
+TEST(MONITOR_SPINLOCK, base)
+{
+ pthread_t pid;
+ stm_spinlock *lock = stm_spinlock_new();
+ pthread_create(&pid, NULL, calc_thread, (void *)lock);
+ usleep(5000);
+ barrier = 0;
+ for (int i = 0; i < CALC_NUM; i++)
+ {
+ stm_spinlock_lock(lock);
+ sum++;
+ stm_spinlock_unlock(lock);
+ }
+ pthread_join(pid, NULL);
+ stm_spinlock_free(lock);
+ EXPECT_EQ(sum, CALC_NUM * 2);
+}
+
+int main(int argc, char **argv)
+{
+ testing::InitGoogleTest(&argc, argv);
+ int ret = RUN_ALL_TESTS();
+ return ret;
+} \ No newline at end of file