Skip to content

Commit 66b42d3

Browse files
committed
Improve thread test program. Test only functions that need testing.
1 parent fc7fd50 commit 66b42d3

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/23 18:15:55 momjian Exp $
7+
# $PostgreSQL: pgsql/src/tools/thread/Makefile,v 1.5 2004/04/23 20: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
override CFLAGS += $(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/23 18:15:55 momjian Exp $
9+
* $PostgreSQL: pgsql/src/tools/thread/thread_test.c,v 1.21 2004/04/23 20: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-
void func_call_1(void);
37-
void func_call_2(void);
36+
void func_call_1(void);
37+
void func_call_2(void);
3838

39-
char myhostname[MAXHOSTNAMELEN];
40-
41-
volatile int errno1_set = 0;
42-
volatile int errno2_set = 0;
39+
pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
4340

4441
volatile int thread1_done = 0;
4542
volatile int thread2_done = 0;
4643

47-
char *strerror_p1;
48-
char *strerror_p2;
44+
volatile int errno1_set = 0;
45+
volatile int errno2_set = 0;
4946

47+
#ifndef HAVE_STRERROR_R
48+
char *strerror_p1;
49+
char *strerror_p2;
50+
bool strerror_threadsafe = false;
51+
#endif
52+
53+
#ifndef HAVE_GETPWUID_R
5054
struct passwd *passwd_p1;
5155
struct passwd *passwd_p2;
56+
bool getpwuid_threadsafe = false;
57+
#endif
5258

59+
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
5360
struct hostent *hostent_p1;
5461
struct hostent *hostent_p2;
62+
char myhostname[MAXHOSTNAMELEN];
63+
bool gethostbyname_threadsafe = false;
64+
#endif
5565

56-
bool gethostbyname_threadsafe = false;
57-
bool getpwuid_threadsafe = false;
58-
bool strerror_threadsafe = false;
59-
bool platform_is_threadsafe = true;
60-
61-
pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
66+
bool platform_is_threadsafe = true;
6267

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

6874
if (argc > 1)
6975
{
70-
fprintf(stderr, "Usage: %s\n", argv[0]);
71-
return 1;
76+
fprintf(stderr, "Usage: %s\n", argv[0]);
77+
return 1;
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+
#ifndef HAVE_STRERROR_R
96101
if (strerror_p1 != strerror_p2)
97102
strerror_threadsafe = true;
103+
#endif
98104

105+
#ifndef HAVE_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
#ifdef HAVE_STRERROR_R
113-
printf("Your system has sterror_r(), so it doesn't use strerror().\n");
121+
printf("Your system has sterror_r(); it does 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
#ifdef HAVE_GETPWUID_R
126-
printf("Your system has getpwuid_r(), so it doesn't use getpwuid().\n");
134+
printf("Your system has getpwuid_r(); it does 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
#ifdef HAVE_GETADDRINFO
139-
printf("Your system has getaddrinfo(), so it doesn't use gethostbyname()\n"
140-
" or gethostbyname_r().\n");
147+
printf("Your system has getaddrinfo(); it does not need gethostbyname()\n"
148+
" or gethostbyname_r().\n");
141149
#else
142150
#ifdef HAVE_GETHOSTBYNAME_R
143-
printf("Your system has gethostbyname_r(), so it doesn't use gethostbyname().\n");
151+
printf("Your system has gethostbyname_r(); it does 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-
return 1;
166+
printf("\nYour platform is thread-safe.\n");
167+
return 0;
160168
}
161169
else
162170
{
163-
printf("\nYOUR PLATFORM IS THREADSAFE\n");
164-
return 0;
171+
printf("\n** YOUR PLATFORM IS NOT THREAD-SAFE. **\n");
172+
return 1;
165173
}
166174
}
167175

168-
void func_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+
#ifndef HAVE_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+
#ifndef HAVE_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-
void func_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+
#ifndef HAVE_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+
#ifndef HAVE_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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy