]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/libstrongswan/tests/suites/test_threading.c
unit-tests: Move test suites to its own subfolder
[thirdparty/strongswan.git] / src / libstrongswan / tests / suites / test_threading.c
CommitLineData
552cc11b 1/*
de42bf35 2 * Copyright (C) 2013 Tobias Brunner
552cc11b
MW
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
552cc11b
MW
17#include <sched.h>
18#include <pthread.h>
19
95e99150
TB
20#include "test_suite.h"
21
de42bf35 22#include <threading/mutex.h>
552cc11b 23
de42bf35
TB
24/*******************************************************************************
25 * recursive mutex test
26 */
552cc11b 27
de42bf35
TB
28#define THREADS 20
29
30static mutex_t *mutex;
552cc11b 31
de42bf35 32static pthread_barrier_t mutex_barrier;
552cc11b 33
de42bf35 34static int mutex_locked = 0;
552cc11b 35
de42bf35 36static void *mutex_run(void *data)
552cc11b
MW
37{
38 int i;
39
40 /* wait for all threads before getting in action */
de42bf35 41 pthread_barrier_wait(&mutex_barrier);
552cc11b
MW
42
43 for (i = 0; i < 100; i++)
44 {
45 mutex->lock(mutex);
46 mutex->lock(mutex);
47 mutex->lock(mutex);
de42bf35 48 mutex_locked++;
552cc11b 49 sched_yield();
de42bf35 50 if (mutex_locked > 1)
552cc11b 51 {
de42bf35 52 fail("two threads locked the mutex concurrently");
7daf5226 53 }
de42bf35 54 mutex_locked--;
552cc11b
MW
55 mutex->unlock(mutex);
56 mutex->unlock(mutex);
57 mutex->unlock(mutex);
58 }
59 return NULL;
60}
61
de42bf35 62START_TEST(test_mutex)
552cc11b 63{
552cc11b 64 pthread_t threads[THREADS];
de42bf35 65 int i;
7daf5226 66
3901937d 67 mutex = mutex_create(MUTEX_TYPE_RECURSIVE);
7daf5226 68
552cc11b
MW
69 for (i = 0; i < 10; i++)
70 {
71 mutex->lock(mutex);
72 mutex->unlock(mutex);
73 }
74 for (i = 0; i < 10; i++)
75 {
76 mutex->lock(mutex);
77 }
78 for (i = 0; i < 10; i++)
79 {
80 mutex->unlock(mutex);
81 }
7daf5226 82
de42bf35 83 pthread_barrier_init(&mutex_barrier, NULL, THREADS);
552cc11b
MW
84 for (i = 0; i < THREADS; i++)
85 {
de42bf35 86 pthread_create(&threads[i], NULL, mutex_run, NULL);
552cc11b
MW
87 }
88 for (i = 0; i < THREADS; i++)
89 {
90 pthread_join(threads[i], NULL);
91 }
de42bf35 92 pthread_barrier_destroy(&mutex_barrier);
7daf5226 93
552cc11b 94 mutex->destroy(mutex);
552cc11b 95}
de42bf35 96END_TEST
552cc11b 97
de42bf35
TB
98Suite *threading_suite_create()
99{
100 Suite *s;
101 TCase *tc;
102
103 s = suite_create("threading");
104
105 tc = tcase_create("recursive mutex");
106 tcase_add_test(tc, test_mutex);
107 suite_add_tcase(s, tc);
108
109 return s;
110}