unit-tests: Add a spinlock test case
authorMartin Willi <martin@revosec.ch>
Wed, 23 Oct 2013 12:14:21 +0000 (14:14 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 6 Nov 2013 09:31:06 +0000 (10:31 +0100)
src/libstrongswan/tests/suites/test_threading.c

index 49b249a..295b647 100644 (file)
@@ -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);