Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit66b42d3

Browse files
committed
Improve thread test program. Test only functions that need testing.
1 parentfc7fd50 commit66b42d3

File tree

2 files changed

+120
-91
lines changed

2 files changed

+120
-91
lines changed

‎src/tools/thread/Makefile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
#
55
# Copyright (C) 2003 by PostgreSQL Global Development Team
66
#
7-
# $PostgreSQL: pgsql/src/tools/thread/Makefile,v 1.4 2004/04/2318:15:55 momjian Exp $
7+
# $PostgreSQL: pgsql/src/tools/thread/Makefile,v 1.5 2004/04/2320:35:50 momjian Exp $
88
#
99
#-------------------------------------------------------------------------
1010

1111
subdir = tools/thread
1212
top_builddir = ../../..
1313
include$(top_builddir)/src/Makefile.global
1414

15-
ifeq ($(THREAD_SUPPORT), no)
16-
$(error Your platform does not support threads)
17-
endif
18-
ifeq ($(THREAD_SUPPORT), )
19-
$(error You have not configured your template/$$port file. See the README)
20-
endif
21-
2215
overrideCFLAGS +=$(PTHREAD_CFLAGS)
2316

2417
LDFLAGS +=$(PTHREAD_LIBS)

‎src/tools/thread/thread_test.c

Lines changed: 119 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*-------------------------------------------------------------------------
22
*
33
* test_thread_funcs.c
4-
*libc thread test program
4+
*libc thread test program
55
*
66
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
*$PostgreSQL: pgsql/src/tools/thread/thread_test.c,v 1.20 2004/04/2318:15:55 momjian Exp $
9+
*$PostgreSQL: pgsql/src/tools/thread/thread_test.c,v 1.21 2004/04/2320:35:50 momjian Exp $
1010
*
1111
*This program tests to see if your standard libc functions use
1212
*pthread_setspecific()/pthread_getspecific() to be thread-safe.
@@ -33,243 +33,279 @@
3333

3434
#include"postgres.h"
3535

36-
voidfunc_call_1(void);
37-
voidfunc_call_2(void);
36+
voidfunc_call_1(void);
37+
voidfunc_call_2(void);
3838

39-
charmyhostname[MAXHOSTNAMELEN];
40-
41-
volatileinterrno1_set=0;
42-
volatileinterrno2_set=0;
39+
pthread_mutex_tinit_mutex=PTHREAD_MUTEX_INITIALIZER;
4340

4441
volatileintthread1_done=0;
4542
volatileintthread2_done=0;
4643

47-
char*strerror_p1;
48-
char*strerror_p2;
44+
volatileinterrno1_set=0;
45+
volatileinterrno2_set=0;
4946

47+
#ifndefHAVE_STRERROR_R
48+
char*strerror_p1;
49+
char*strerror_p2;
50+
boolstrerror_threadsafe= false;
51+
#endif
52+
53+
#ifndefHAVE_GETPWUID_R
5054
structpasswd*passwd_p1;
5155
structpasswd*passwd_p2;
56+
boolgetpwuid_threadsafe= false;
57+
#endif
5258

59+
#if !defined(HAVE_GETADDRINFO)&& !defined(HAVE_GETHOSTBYNAME_R)
5360
structhostent*hostent_p1;
5461
structhostent*hostent_p2;
62+
charmyhostname[MAXHOSTNAMELEN];
63+
boolgethostbyname_threadsafe= false;
64+
#endif
5565

56-
boolgethostbyname_threadsafe= false;
57-
boolgetpwuid_threadsafe= false;
58-
boolstrerror_threadsafe= false;
59-
boolplatform_is_threadsafe= true;
60-
61-
pthread_mutex_tinit_mutex=PTHREAD_MUTEX_INITIALIZER;
66+
boolplatform_is_threadsafe= true;
6267

63-
intmain(intargc,char*argv[])
68+
int
69+
main(intargc,char*argv[])
6470
{
65-
pthread_tthread1,
66-
thread2;
71+
pthread_tthread1,
72+
thread2;
6773

6874
if (argc>1)
6975
{
70-
fprintf(stderr,"Usage: %s\n",argv[0]);
71-
return1;
76+
fprintf(stderr,"Usage: %s\n",argv[0]);
77+
return1;
7278
}
7379

80+
#if !defined(HAVE_GETADDRINFO)&& !defined(HAVE_GETHOSTBYNAME_R)
7481
if (gethostname(myhostname,MAXHOSTNAMELEN)!=0)
7582
{
76-
fprintf(stderr,"can not get local hostname, exiting\n");
77-
exit(1);
83+
fprintf(stderr,"can not get local hostname, exiting\n");
84+
exit(1);
7885
}
79-
80-
printf("\
81-
Make sure you have added any needed 'PTHREAD_CFLAGS' and 'PTHREAD_LIBS'\n\
82-
defines to your template/$port file before compiling this program.\n\n"
83-
);
86+
#endif
8487

8588
/* Hold lock until we are ready for the child threads to exit. */
86-
pthread_mutex_lock(&init_mutex);
87-
88-
pthread_create(&thread1,NULL, (void*(*)(void*))func_call_1,NULL);
89-
pthread_create(&thread2,NULL, (void*(*)(void*))func_call_2,NULL);
89+
pthread_mutex_lock(&init_mutex);
90+
91+
pthread_create(&thread1,NULL, (void*(*)(void*))func_call_1,NULL);
92+
pthread_create(&thread2,NULL, (void*(*)(void*))func_call_2,NULL);
9093

9194
while (thread1_done==0||thread2_done==0)
92-
sched_yield();/* if this is a portability problem, remove it */
95+
sched_yield();/* if this is a portability problem,
96+
* remove it */
9397

94-
fprintf(stderr,"errno is thread-safe\n");
95-
98+
fprintf(stderr,"Your errno is thread-safe.\n");
99+
100+
#ifndefHAVE_STRERROR_R
96101
if (strerror_p1!=strerror_p2)
97102
strerror_threadsafe= true;
103+
#endif
98104

105+
#ifndefHAVE_GETPWUID_R
99106
if (passwd_p1!=passwd_p2)
100107
getpwuid_threadsafe= true;
108+
#endif
101109

110+
#if !defined(HAVE_GETADDRINFO)&& !defined(HAVE_GETHOSTBYNAME_R)
102111
if (hostent_p1!=hostent_p2)
103112
gethostbyname_threadsafe= true;
113+
#endif
104114

105115
pthread_mutex_unlock(&init_mutex);/* let children exit */
106-
116+
107117
pthread_join(thread1,NULL);/* clean up children */
108118
pthread_join(thread2,NULL);
109119

110-
printf("\n");
111-
112120
#ifdefHAVE_STRERROR_R
113-
printf("Your system has sterror_r(), so itdoesn't use strerror().\n");
121+
printf("Your system has sterror_r(); itdoes not need strerror().\n");
114122
#else
115123
printf("Your system uses strerror() which is ");
116124
if (strerror_threadsafe)
117125
printf("thread-safe.\n");
118126
else
119127
{
120-
platform_is_threadsafe= false;
121128
printf("not thread-safe.\n");
129+
platform_is_threadsafe= false;
122130
}
123131
#endif
124132

125133
#ifdefHAVE_GETPWUID_R
126-
printf("Your system has getpwuid_r(), so itdoesn't use getpwuid().\n");
134+
printf("Your system has getpwuid_r(); itdoes not need getpwuid().\n");
127135
#else
128136
printf("Your system uses getpwuid() which is ");
129137
if (getpwuid_threadsafe)
130138
printf("thread-safe.\n");
131139
else
132140
{
133-
platform_is_threadsafe= false;
134141
printf("not thread-safe.\n");
142+
platform_is_threadsafe= false;
135143
}
136144
#endif
137145

138146
#ifdefHAVE_GETADDRINFO
139-
printf("Your system has getaddrinfo(), so itdoesn't use gethostbyname()\n"
140-
" or gethostbyname_r().\n");
147+
printf("Your system has getaddrinfo(); itdoes not need gethostbyname()\n"
148+
" or gethostbyname_r().\n");
141149
#else
142150
#ifdefHAVE_GETHOSTBYNAME_R
143-
printf("Your system has gethostbyname_r(), so itdoesn't use gethostbyname().\n");
151+
printf("Your system has gethostbyname_r(); itdoes not need gethostbyname().\n");
144152
#else
145153
printf("Your system uses gethostbyname which is ");
146154
if (gethostbyname_threadsafe)
147155
printf("thread-safe.\n");
148156
else
149157
{
150-
platform_is_threadsafe= false;
151158
printf("not thread-safe.\n");
159+
platform_is_threadsafe= false;
152160
}
153161
#endif
154162
#endif
155163

156-
if (!platform_is_threadsafe)
164+
if (platform_is_threadsafe)
157165
{
158-
printf("\n** YOUR PLATFORM IS NOT THREADSAFE **\n");
159-
return1;
166+
printf("\nYour platform is thread-safe.\n");
167+
return0;
160168
}
161169
else
162170
{
163-
printf("\nYOURPLATFORM ISTHREADSAFE\n");
164-
return0;
171+
printf("\n** YOURPLATFORM ISNOT THREAD-SAFE. **\n");
172+
return1;
165173
}
166174
}
167175

168-
voidfunc_call_1(void) {
169-
void*p;
170-
176+
void
177+
func_call_1(void)
178+
{
179+
#if !defined(HAVE_GETPWUID_R)|| \
180+
(!defined(HAVE_GETADDRINFO)&& \
181+
!defined(HAVE_GETHOSTBYNAME_R))
182+
void*p;
183+
#endif
184+
171185
unlink("/tmp/thread_test.1");
172186
/* create, then try to fail on exclusive create open */
173187
if (open("/tmp/thread_test.1",O_RDWR |O_CREAT,0600)<0||
174188
open("/tmp/thread_test.1",O_RDWR |O_CREAT |O_EXCL,0600) >=0)
175189
{
176-
fprintf(stderr,"Could not create file in /tmp or\n");
177-
fprintf(stderr,"could not generate failure for create file in /tmp, exiting\n");
178-
exit(1);
190+
fprintf(stderr,"Could not create file in /tmp or\n");
191+
fprintf(stderr,"could not generate failure for create file in /tmp, exiting\n");
192+
exit(1);
179193
}
194+
180195
/*
181-
*Wait for other thread to set errno.
182-
*We can't use thread-specific locking here because it might
183-
*affect errno.
196+
* Wait for other thread to set errno. We can't use thread-specific
197+
* locking here because it might affect errno.
184198
*/
185199
errno1_set=1;
186200
while (errno2_set==0)
187201
sched_yield();
188202
if (errno!=EEXIST)
189203
{
190-
fprintf(stderr,"errno not thread-safe; exiting\n");
191-
unlink("/tmp/thread_test.1");
192-
exit(1);
204+
fprintf(stderr,"errno not thread-safe; exiting\n");
205+
unlink("/tmp/thread_test.1");
206+
exit(1);
193207
}
194208
unlink("/tmp/thread_test.1");
195-
209+
210+
#ifndefHAVE_STRERROR_R
196211
strerror_p1=strerror(EACCES);
212+
197213
/*
198-
*If strerror() uses sys_errlist, the pointer might change for different
199-
*errno values, so we don't check to see if it varies within the thread.
214+
* If strerror() uses sys_errlist, the pointer might change for
215+
* different errno values, so we don't check to see if it varies
216+
* within the thread.
200217
*/
218+
#endif
201219

220+
#ifndefHAVE_GETPWUID_R
202221
passwd_p1=getpwuid(0);
203222
p=getpwuid(1);
204223
if (passwd_p1!=p)
205224
{
206225
printf("Your getpwuid() changes the static memory area between calls\n");
207-
passwd_p1=NULL;/* force thread-safe failure report */
226+
passwd_p1=NULL;/* force thread-safe failure report */
208227
}
228+
#endif
209229

230+
#if !defined(HAVE_GETADDRINFO)&& !defined(HAVE_GETHOSTBYNAME_R)
210231
/* threads do this in opposite order */
211232
hostent_p1=gethostbyname(myhostname);
212233
p=gethostbyname("localhost");
213234
if (hostent_p1!=p)
214235
{
215236
printf("Your gethostbyname() changes the static memory area between calls\n");
216-
hostent_p1=NULL;/* force thread-safe failure report */
237+
hostent_p1=NULL;/* force thread-safe failure report */
217238
}
239+
#endif
218240

219241
thread1_done=1;
220242
pthread_mutex_lock(&init_mutex);/* wait for parent to test */
221243
pthread_mutex_unlock(&init_mutex);
222244
}
223245

224246

225-
voidfunc_call_2(void) {
226-
void*p;
247+
void
248+
func_call_2(void)
249+
{
250+
#if !defined(HAVE_GETPWUID_R)|| \
251+
(!defined(HAVE_GETADDRINFO)&& \
252+
!defined(HAVE_GETHOSTBYNAME_R))
253+
void*p;
254+
#endif
227255

228256
unlink("/tmp/thread_test.2");
229257
/* open non-existant file */
230258
if (open("/tmp/thread_test.2",O_RDONLY,0600) >=0)
231259
{
232-
fprintf(stderr,"Read-only open succeeded without create, exiting\n");
233-
exit(1);
260+
fprintf(stderr,"Read-only open succeeded without create, exiting\n");
261+
exit(1);
234262
}
263+
235264
/*
236-
*Wait for other thread to set errno.
237-
*We can't use thread-specific locking here because it might
238-
*affect errno.
265+
* Wait for other thread to set errno. We can't use thread-specific
266+
* locking here because it might affect errno.
239267
*/
240268
errno2_set=1;
241269
while (errno1_set==0)
242270
sched_yield();
243271
if (errno!=ENOENT)
244272
{
245-
fprintf(stderr,"errno not thread-safe; exiting\n");
246-
unlink("/tmp/thread_test.A");
247-
exit(1);
273+
fprintf(stderr,"errno not thread-safe; exiting\n");
274+
unlink("/tmp/thread_test.A");
275+
exit(1);
248276
}
249277
unlink("/tmp/thread_test.2");
250-
278+
279+
#ifndefHAVE_STRERROR_R
251280
strerror_p2=strerror(EINVAL);
281+
252282
/*
253-
*If strerror() uses sys_errlist, the pointer might change for different
254-
*errno values, so we don't check to see if it varies within the thread.
283+
* If strerror() uses sys_errlist, the pointer might change for
284+
* different errno values, so we don't check to see if it varies
285+
* within the thread.
255286
*/
287+
#endif
256288

289+
#ifndefHAVE_GETPWUID_R
257290
passwd_p2=getpwuid(2);
258291
p=getpwuid(3);
259292
if (passwd_p2!=p)
260293
{
261294
printf("Your getpwuid() changes the static memory area between calls\n");
262-
passwd_p2=NULL;/* force thread-safe failure report */
295+
passwd_p2=NULL;/* force thread-safe failure report */
263296
}
297+
#endif
264298

299+
#if !defined(HAVE_GETADDRINFO)&& !defined(HAVE_GETHOSTBYNAME_R)
265300
/* threads do this in opposite order */
266301
hostent_p2=gethostbyname("localhost");
267302
p=gethostbyname(myhostname);
268303
if (hostent_p2!=p)
269304
{
270305
printf("Your gethostbyname() changes the static memory area between calls\n");
271-
hostent_p2=NULL;/* force thread-safe failure report */
306+
hostent_p2=NULL;/* force thread-safe failure report */
272307
}
308+
#endif
273309

274310
thread2_done=1;
275311
pthread_mutex_lock(&init_mutex);/* wait for parent to test */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp