1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <cf3.defs.h>
#include <test.h>
#define NUM_THREADS 100
static void create_children(pthread_t tids[NUM_THREADS]);
static void join_children(pthread_t tids[NUM_THREADS]);
static void increment_shared_var(void);
int SHARED_VAR;
pthread_mutex_t shared_var_mutex = PTHREAD_MUTEX_INITIALIZER;
void test_init_destroy(void)
{
pthread_mutex_t mutex_dynamic;
int res_init = pthread_mutex_init(&mutex_dynamic, NULL);
assert_int_equal(res_init, 0);
int res_destroy = pthread_mutex_destroy(&mutex_dynamic);
assert_int_equal(res_destroy, 0);
}
void test_trylock_impl(pthread_mutex_t *mutex)
{
int res_trylock_unlocked = pthread_mutex_trylock(mutex);
assert_int_equal(res_trylock_unlocked, 0);
int res_trylock_locked = pthread_mutex_trylock(mutex);
if (res_trylock_locked != EBUSY && res_trylock_locked != EDEADLK)
{
/* Some pthread implementations return EDEADLK despite SUS saying otherwise */
fail();
}
int res_unlock = pthread_mutex_unlock(mutex);
assert_int_equal(res_unlock, 0);
}
void test_trylock_dynamic(void)
{
pthread_mutex_t mutex_dynamic;
int res_init = pthread_mutex_init(&mutex_dynamic, NULL);
assert_int_equal(res_init, 0);
test_trylock_impl(&mutex_dynamic);
int res_destroy = pthread_mutex_destroy(&mutex_dynamic);
assert_int_equal(res_destroy, 0);
}
void test_trylock_static(void)
{
pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER;
test_trylock_impl(&mutex_static);
}
void test_trylock_static_errorcheck(void)
{
pthread_mutex_t mutex_static_errorcheck = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
test_trylock_impl(&mutex_static_errorcheck);
}
void test_create(void)
{
SHARED_VAR = 0;
pthread_t tid;
int res_create = pthread_create(&tid, NULL, (void *) increment_shared_var, NULL);
assert_int_equal(res_create, 0);
int res_join = pthread_join(tid, NULL);
assert_int_equal(res_join, 0);
assert_int_equal(SHARED_VAR, 1);
}
static void increment_shared_var(void)
{
#define THREAD_ITERATIONS 1000
int res_lock = pthread_mutex_lock(&shared_var_mutex);
assert_int_equal(res_lock, 0);
for(int i = 0; i < THREAD_ITERATIONS; i++)
{
SHARED_VAR++;
SHARED_VAR--;
}
SHARED_VAR++;
int res_unlock = pthread_mutex_unlock(&shared_var_mutex);
assert_int_equal(res_unlock, 0);
}
void test_lock(void)
{
SHARED_VAR = 0;
pthread_t tids[NUM_THREADS];
create_children(tids);
join_children(tids);
assert_int_equal(SHARED_VAR, NUM_THREADS);
}
static void create_children(pthread_t tids[NUM_THREADS])
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 65536);
for(int i = 0; i < NUM_THREADS; i++)
{
int res_create = pthread_create(&(tids[i]), &attr, (void *) increment_shared_var, NULL);
assert_int_equal(res_create, 0);
}
}
static void join_children(pthread_t tids[NUM_THREADS])
{
for(int i = 0; i < NUM_THREADS; i++)
{
int res_join = pthread_join(tids[i], NULL);
assert_int_equal(res_join, 0);
}
}
int main()
{
PRINT_TEST_BANNER();
const UnitTest tests[] =
{
unit_test(test_init_destroy),
unit_test(test_trylock_dynamic),
unit_test(test_trylock_static),
unit_test(test_trylock_static_errorcheck),
unit_test(test_create),
unit_test(test_lock),
};
return run_tests(tests);
}
|