unit-tests: Add a rwlock test case
authorMartin Willi <martin@revosec.ch>
Tue, 22 Oct 2013 15:24:59 +0000 (17:24 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 6 Nov 2013 09:31:05 +0000 (10:31 +0100)
src/libstrongswan/tests/suites/test_threading.c

index 1704496..37b2bd0 100644 (file)
@@ -22,6 +22,7 @@
 #include <threading/thread.h>
 #include <threading/mutex.h>
 #include <threading/condvar.h>
+#include <threading/rwlock.h>
 #include <threading/thread_value.h>
 
 /*******************************************************************************
@@ -449,6 +450,63 @@ START_TEST(test_condvar_cancel)
 }
 END_TEST
 
+/**
+ * RWlock for different tests
+ */
+static rwlock_t *rwlock;
+
+static void *rwlock_run(refcount_t *refs)
+{
+       rwlock->read_lock(rwlock);
+       ref_get(refs);
+       sched_yield();
+       ignore_result(ref_put(refs));
+       rwlock->unlock(rwlock);
+
+       if (rwlock->try_write_lock(rwlock))
+       {
+               ck_assert_int_eq(*refs, 0);
+               sched_yield();
+               rwlock->unlock(rwlock);
+       }
+
+       rwlock->write_lock(rwlock);
+       ck_assert_int_eq(*refs, 0);
+       sched_yield();
+       rwlock->unlock(rwlock);
+
+       rwlock->read_lock(rwlock);
+       rwlock->read_lock(rwlock);
+       ref_get(refs);
+       sched_yield();
+       ignore_result(ref_put(refs));
+       rwlock->unlock(rwlock);
+       rwlock->unlock(rwlock);
+
+       return NULL;
+}
+
+START_TEST(test_rwlock)
+{
+       thread_t *threads[THREADS];
+       refcount_t refs = 0;
+       int i;
+
+       rwlock = rwlock_create(RWLOCK_TYPE_DEFAULT);
+
+       for (i = 0; i < THREADS; i++)
+       {
+               threads[i] = thread_create((void*)rwlock_run, &refs);
+       }
+       for (i = 0; i < THREADS; i++)
+       {
+               threads[i]->join(threads[i]);
+       }
+
+       rwlock->destroy(rwlock);
+}
+END_TEST
+
 static void *join_run(void *data)
 {
        /* force some context switches */
@@ -955,6 +1013,10 @@ Suite *threading_suite_create()
        tcase_add_test(tc, test_condvar_cancel);
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("rwlock");
+       tcase_add_test(tc, test_rwlock);
+       suite_add_tcase(s, tc);
+
        tc = tcase_create("thread joining");
        tcase_add_test(tc, test_join);
        tcase_add_test(tc, test_join_exit);