rebase onto develop-2.0
This commit is contained in:
49
test/monitor/gtest_spinlock.cpp
Normal file
49
test/monitor/gtest_spinlock.cpp
Normal file
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user