49 lines
1.0 KiB
C++
49 lines
1.0 KiB
C++
#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;
|
|
} |