unit-tests: Add a spinlock test case
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
#include <threading/condvar.h>
|
#include <threading/condvar.h>
|
||||||
#include <threading/rwlock.h>
|
#include <threading/rwlock.h>
|
||||||
#include <threading/rwlock_condvar.h>
|
#include <threading/rwlock_condvar.h>
|
||||||
|
#include <threading/spinlock.h>
|
||||||
#include <threading/thread_value.h>
|
#include <threading/thread_value.h>
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
@@ -183,6 +184,50 @@ START_TEST(test_mutex)
|
|||||||
}
|
}
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spinlock for testing
|
||||||
|
*/
|
||||||
|
static spinlock_t *spinlock;
|
||||||
|
|
||||||
|
static void *spinlock_run(void *data)
|
||||||
|
{
|
||||||
|
int i, *locked = (int*)data;
|
||||||
|
|
||||||
|
barrier_wait(barrier);
|
||||||
|
|
||||||
|
for (i = 0; i < 1000; i++)
|
||||||
|
{
|
||||||
|
spinlock->lock(spinlock);
|
||||||
|
(*locked)++;
|
||||||
|
ck_assert_int_eq(*locked, 1);
|
||||||
|
(*locked)--;
|
||||||
|
spinlock->unlock(spinlock);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
START_TEST(test_spinlock)
|
||||||
|
{
|
||||||
|
thread_t *threads[THREADS];
|
||||||
|
int i, locked = 0;
|
||||||
|
|
||||||
|
barrier = barrier_create(THREADS);
|
||||||
|
spinlock = spinlock_create();
|
||||||
|
|
||||||
|
for (i = 0; i < THREADS; i++)
|
||||||
|
{
|
||||||
|
threads[i] = thread_create(spinlock_run, &locked);
|
||||||
|
}
|
||||||
|
for (i = 0; i < THREADS; i++)
|
||||||
|
{
|
||||||
|
threads[i]->join(threads[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
spinlock->destroy(spinlock);
|
||||||
|
barrier_destroy(barrier);
|
||||||
|
}
|
||||||
|
END_TEST
|
||||||
|
|
||||||
static void *condvar_run(void *data)
|
static void *condvar_run(void *data)
|
||||||
{
|
{
|
||||||
mutex->lock(mutex);
|
mutex->lock(mutex);
|
||||||
@@ -1229,6 +1274,10 @@ Suite *threading_suite_create()
|
|||||||
tcase_add_test(tc, test_mutex);
|
tcase_add_test(tc, test_mutex);
|
||||||
suite_add_tcase(s, tc);
|
suite_add_tcase(s, tc);
|
||||||
|
|
||||||
|
tc = tcase_create("spinlock");
|
||||||
|
tcase_add_test(tc, test_spinlock);
|
||||||
|
suite_add_tcase(s, tc);
|
||||||
|
|
||||||
tc = tcase_create("condvar");
|
tc = tcase_create("condvar");
|
||||||
tcase_add_test(tc, test_condvar);
|
tcase_add_test(tc, test_condvar);
|
||||||
tcase_add_test(tc, test_condvar_recursive);
|
tcase_add_test(tc, test_condvar_recursive);
|
||||||
|
|||||||
Reference in New Issue
Block a user