-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_std.c
153 lines (134 loc) · 4.4 KB
/
test_std.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
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
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
**
** test_std.c: Pth standard test program
*/
/* ``Understanding a problem is knowing why
it is hard to solve it, and why the most
straightforward approaches won't work.''
-- Karl Popper */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include "pth.h"
#define FAILED_IF(expr) \
if (expr) { \
fprintf(stderr, "*** ERROR, TEST FAILED:\n*** errno=%d\n\n", errno); \
exit(1); \
}
static void *t1_func(void *arg)
{
int i;
long val;
val = (long)arg;
FAILED_IF(val != 123)
for (i = 0; i < 100; i++) {
val += 10;
pth_yield(NULL);
}
return (void *)val;
}
static void *t2_func(void *arg)
{
long val;
pth_t tid;
void *rval;
int rc;
val = (long)arg;
if (val < 9) {
val++;
fprintf(stderr, "Spawning thread %ld\n", val);
tid = pth_spawn(PTH_ATTR_DEFAULT, t2_func, (void *)(val));
FAILED_IF(tid == NULL)
rc = pth_join(tid, &rval);
fprintf(stderr, "Joined thread %ld\n", val);
FAILED_IF(rc == FALSE)
rval = (void *)((long)arg * (long)rval);
}
else
rval = arg;
return rval;
}
int main(int argc, char *argv[])
{
fprintf(stderr, "\n=== TESTING GLOBAL LIBRARY API ===\n\n");
{
int version;
fprintf(stderr, "Fetching library version\n");
version = pth_version();
FAILED_IF(version == 0x0)
fprintf(stderr, "version = 0x%X\n", version);
}
fprintf(stderr, "\n=== TESTING BASIC OPERATION ===\n\n");
{
int rc;
fprintf(stderr, "Initializing Pth system (spawns scheduler and main thread)\n");
rc = pth_init();
FAILED_IF(rc == FALSE)
fprintf(stderr, "Killing Pth system for testing purposes\n");
pth_kill();
fprintf(stderr, "Re-Initializing Pth system\n");
rc = pth_init();
FAILED_IF(rc == FALSE)
}
fprintf(stderr, "\n=== TESTING BASIC THREAD OPERATION ===\n\n");
{
pth_attr_t attr;
pth_t tid;
void *val;
int rc;
fprintf(stderr, "Creating attribute object\n");
attr = pth_attr_new();
FAILED_IF(attr == NULL)
rc = pth_attr_set(attr, PTH_ATTR_NAME, "test1");
FAILED_IF(rc == FALSE)
rc = pth_attr_set(attr, PTH_ATTR_PRIO, PTH_PRIO_MAX);
FAILED_IF(rc == FALSE)
fprintf(stderr, "Spawning thread\n");
tid = pth_spawn(attr, t1_func, (void *)(123));
FAILED_IF(tid == NULL)
pth_attr_destroy(attr);
fprintf(stderr, "Joining thread\n");
rc = pth_join(tid, &val);
FAILED_IF(rc == FALSE)
FAILED_IF(val != (void *)(1123))
}
fprintf(stderr, "\n=== TESTING NESTED THREAD OPERATION ===\n\n");
{
pth_t tid;
void *val;
int rc;
fprintf(stderr, "Spawning thread 1\n");
tid = pth_spawn(PTH_ATTR_DEFAULT, t2_func, (void *)(1));
FAILED_IF(tid == NULL)
rc = pth_join(tid, &val);
fprintf(stderr, "Joined thread 1\n");
FAILED_IF(rc == FALSE)
FAILED_IF(val != (void *)(1*2*3*4*5*6*7*8*9))
}
pth_kill();
fprintf(stderr, "\nOK - ALL TESTS SUCCESSFULLY PASSED.\n\n");
exit(0);
}