unit-tests: Add a spinlock test case

This commit is contained in:
Martin Willi
2013-11-06 10:31:06 +01:00
parent 478dc0257c
commit b1bfe59560
@@ -24,6 +24,7 @@
#include <threading/condvar.h>
#include <threading/rwlock.h>
#include <threading/rwlock_condvar.h>
#include <threading/spinlock.h>
#include <threading/thread_value.h>
/*******************************************************************************
@@ -183,6 +184,50 @@ START_TEST(test_mutex)
}
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)
{
mutex->lock(mutex);
@@ -1229,6 +1274,10 @@ Suite *threading_suite_create()
tcase_add_test(tc, test_mutex);
suite_add_tcase(s, tc);
tc = tcase_create("spinlock");
tcase_add_test(tc, test_spinlock);
suite_add_tcase(s, tc);
tc = tcase_create("condvar");
tcase_add_test(tc, test_condvar);
tcase_add_test(tc, test_condvar_recursive);