-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy paththread_test.c
112 lines (93 loc) · 2.27 KB
/
thread_test.c
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
#include "sc_thread.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
void *fn(void *arg)
{
printf("%s \n", (char *) arg);
return arg;
}
void test1(void)
{
int rc;
void *ret;
struct sc_thread thread;
sc_thread_init(&thread);
rc = sc_thread_start(&thread, fn, "first");
assert(rc == 0);
rc = sc_thread_join(&thread, &ret);
assert(rc == 0);
assert(strcmp((char *) ret, "first") == 0);
rc = sc_thread_term(&thread);
assert(rc == 0);
rc = sc_thread_term(&thread);
assert(rc == 0);
sc_thread_init(&thread);
rc = sc_thread_start(&thread, fn, "first");
assert(rc == 0);
rc = sc_thread_term(&thread);
assert(rc == 0);
}
#ifdef SC_HAVE_WRAP
bool fail_pthread_attr_init = false;
int __real_pthread_attr_init(pthread_attr_t *__attr);
int __wrap_pthread_attr_init(pthread_attr_t *__attr)
{
if (fail_pthread_attr_init) {
return -1;
}
return __real_pthread_attr_init(__attr);
}
bool fail_pthread_create = false;
int __real_pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg);
int __wrap_pthread_create(pthread_t *newthread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg)
{
if (fail_pthread_create) {
return -1;
}
return __real_pthread_create(newthread, attr, start_routine, arg);
}
bool fail_pthread_join = false;
int __real_pthread_join(pthread_t th, void **thread_return);
int __wrap_pthread_join(pthread_t th, void **thread_return)
{
int rc;
rc = __real_pthread_join(th, thread_return);
if (fail_pthread_join) {
rc = -1;
}
return rc;
}
void fail_test(void)
{
struct sc_thread thread;
sc_thread_init(&thread);
fail_pthread_attr_init = true;
assert(sc_thread_start(&thread, fn, "first") != 0);
assert(sc_thread_err(&thread) != NULL);
fail_pthread_attr_init = false;
assert(sc_thread_start(&thread, fn, "first") == 0);
assert(sc_thread_term(&thread) == 0);
sc_thread_init(&thread);
fail_pthread_create = true;
assert(sc_thread_start(&thread, fn, "first") != 0);
assert(sc_thread_err(&thread) != NULL);
fail_pthread_create = false;
assert(sc_thread_start(&thread, fn, "first") == 0);
fail_pthread_join = true;
assert(sc_thread_term(&thread) == -1);
}
#else
void fail_test(void)
{
}
#endif
int main(void)
{
test1();
fail_test();
return 0;
}