Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tmtsafe.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "sftest.h"
21
22
#if vt_threaded
23
24
#include <vthread.h>
25
26
#define N_STR 1000
27
28
static Sfio_t* Sf;
29
static char Bigz[10*N_STR];
30
static char* Str[26] =
31
{ "aaaaaaaaa",
32
"bbbbbbbbb",
33
"ccccccccc",
34
"ddddddddd",
35
"eeeeeeeee",
36
"fffffffff",
37
"ggggggggg",
38
"hhhhhhhhh",
39
"iiiiiiiii",
40
"jjjjjjjjj",
41
"kkkkkkkkk",
42
"lllllllll",
43
"mmmmmmmmm",
44
"nnnnnnnnn",
45
"ooooooooo",
46
"ppppppppp",
47
"qqqqqqqqq",
48
"rrrrrrrrr",
49
"sssssssss",
50
"ttttttttt",
51
"uuuuuuuuu",
52
"vvvvvvvvv",
53
"wwwwwwwww",
54
"xxxxxxxxx",
55
"yyyyyyyyy",
56
"zzzzzzzzz"
57
};
58
59
static int Inverted; /* failure means success */
60
61
#if _STD_C
62
void* writesmall(void* arg)
63
#else
64
void* writesmall(arg)
65
void* arg;
66
#endif
67
{
68
char* s;
69
int n;
70
71
s = Str[integralof(arg)];
72
for(n = 0; n < N_STR; ++n)
73
{ if(sfputr(Sf, s, '\n') != 10)
74
{ if(Inverted)
75
tsuccess("sfputr failed as expected");
76
else terror("sfputr failed");
77
}
78
}
79
80
return arg;
81
}
82
83
#if _STD_C
84
void* writebig(void* arg)
85
#else
86
void* writebig(arg)
87
void* arg;
88
#endif
89
{
90
int r = (rand()%3) + 1; sleep(r);
91
92
if(sfwrite(Sf,Bigz,sizeof(Bigz)) != sizeof(Bigz))
93
terror("Writing bigz");
94
95
return arg;
96
}
97
98
#if __STD_C
99
void sighandler(int sig)
100
#else
101
void sighandler(sig)
102
int sig;
103
#endif
104
{
105
tmesg("\tSignal %d.\n", sig);
106
texit(0);
107
}
108
109
tmain()
110
{
111
int count[26];
112
char* s;
113
int i, k, n;
114
Vthread_t* thread[26];
115
116
/* make the big z string */
117
for(i = 0, s = Bigz; i < N_STR; ++i, s += 10)
118
strcpy(s, "zzzzzzzzz\n");
119
120
signal(SIGQUIT,sighandler);
121
signal(SIGINT,sighandler);
122
123
do_inverted: /* get back to here when trying to make things fail */
124
125
if(!Inverted)
126
tmesg("\tTesting thread-safe streams.\n");
127
else tmesg("\tTesting unsafe streams: if hung, send INTR or QUIT.\n");
128
129
/* spin threads writing small chunks */
130
Sf = sfopen(NIL(Sfio_t*),tstfile("sf", 0), Inverted ? "w+" : "mw+");
131
132
for(i = 0; i < 26; ++i)
133
{ if(!(thread[i] = vtopen(0, 0)) )
134
terror("Creating thread handle[%d]", i);
135
if(vtrun(thread[i], writesmall, (Void_t*)i) < 0)
136
terror("Running thread [%d]", i);
137
}
138
139
for(i = 0; i < 26; ++i)
140
{ count[i] = 0;
141
vtwait(thread[i]);
142
}
143
144
if(sfseek(Sf,(Sfoff_t)0,SEEK_SET) != (Sfoff_t)0)
145
{ if(Inverted)
146
tsuccess("Rewinding failed as expected");
147
else terror("Rewinding");
148
}
149
150
for(n = 0;; ++n)
151
{ if(!(s = sfgetr(Sf,'\n',1)) )
152
break;
153
154
i = s[0] - 'a';
155
if(i < 0 || i >= 26 || sfvalue(Sf) != 10)
156
{ if(Inverted)
157
tsuccess("Bad data as expected");
158
else terror("Bad data s='%s' n=%d", s, n);
159
}
160
if(strcmp(s, Str[i]) != 0)
161
{ if(Inverted)
162
tsuccess("Bad str as expected");
163
else terror("Bad str s='%s' i=%d Str[i]='%s' n=%d",
164
s, i, Str[i], n);
165
}
166
167
count[i] += 1;
168
}
169
170
for(i = 0; i < 26; ++i)
171
{ if(count[i] != N_STR)
172
{ if(Inverted)
173
tsuccess("Bad count as expected");
174
else terror("Bad count[%d] = %d", i, count[i]);
175
}
176
}
177
178
/* spin threads with one writing a big chunk */
179
Sf = sfopen(Sf,tstfile("sf", 0), Inverted ? "w+" : "mw+");
180
181
for(i = 0; i < 25; ++i)
182
{ if(!(thread[i] = vtopen(0, 0)) )
183
terror("Creating thread %d", i);
184
if(vtrun(thread[i], writesmall, (void*)i) < 0)
185
terror("Running thread %d", i);
186
}
187
sleep(1);
188
if(!(thread[i] = vtopen(0,0)))
189
terror("Creating big thread z");
190
if(vtrun(thread[i],writebig,(void*)i) < 0)
191
terror("Running big thread z");
192
193
for(i = 0; i < 26; ++i)
194
{ count[i] = 0;
195
vtwait(thread[i]);
196
}
197
198
if(sfseek(Sf,(Sfoff_t)0,SEEK_SET) != (Sfoff_t)0)
199
{ if(Inverted)
200
tsuccess("Rewinding failed as expected");
201
else terror("Rewinding");
202
}
203
204
for(n = 0; ; ++n)
205
{ if(!(s = sfgetr(Sf,'\n',1)) )
206
break;
207
208
i = s[0] - 'a';
209
if(i < 0 || i >= 26 || sfvalue(Sf) != 10)
210
{ if(Inverted)
211
tsuccess("Bad data as expected");
212
else terror("Bad data s='%s' n=%d", s, n);
213
}
214
if(strcmp(s, Str[i]) != 0)
215
{ if(Inverted)
216
tsuccess("Bad str as expected");
217
else terror("Bad str s='%s' i=%d Str[i]='%s' n=%d",
218
s, i, Str[i], n);
219
}
220
count[i] += 1;
221
222
if(i == 25) /* the 'z' */
223
{ for(k = 1; k < N_STR; ++k, ++n)
224
{ if(!(s = sfgetr(Sf,'\n',1)) )
225
{ if(Inverted)
226
tsuccess("Premature eof as expected");
227
else terror("Premature eof n=%d", n);
228
}
229
if(strcmp(s, Str[25]) != 0)
230
{ if(Inverted)
231
tsuccess("Bad str as expected");
232
else terror("Bad str s='%s' n=%d", s, n);
233
}
234
count[i] += 1;
235
}
236
}
237
}
238
239
for(i = 0; i < 26; ++i)
240
{ if(count[i] != N_STR)
241
{ if(Inverted)
242
tsuccess("Bad count as expected");
243
else terror("Bad count[%d] = %d", i, count[i]);
244
}
245
}
246
247
if(!Inverted)
248
{ Inverted = 1;
249
goto do_inverted;
250
}
251
else tmesg("\tUnsafe streams work ok on this platform!\n");
252
253
texit(0);
254
}
255
256
#else
257
258
tmain()
259
{
260
texit(0);
261
}
262
263
#endif
264
265