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.16 2004/04/06 13:55:17 momjian Exp $
9+ *$PostgreSQL: pgsql/src/tools/thread/thread_test.c,v 1.17 2004/04/21 20:51:54 momjian Exp $
1010 *
1111 *This program tests to see if your standard libc functions use
1212 *pthread_setspecific()/pthread_getspecific() to be thread-safe.
3232#include <fcntl.h>
3333#include <errno.h>
3434
35+ #include "postgres.h"
36+
3537void func_call_1 (void );
3638void func_call_2 (void );
3739
@@ -52,6 +54,11 @@ struct passwd *passwd_p2;
5254struct hostent * hostent_p1 ;
5355struct hostent * hostent_p2 ;
5456
57+ bool gethostbyname_threadsafe ;
58+ bool getpwuid_threadsafe ;
59+ bool strerror_threadsafe ;
60+ bool platform_is_threadsafe = true;
61+
5562pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER ;
5663
5764int main (int argc ,char * argv [])
@@ -90,26 +97,93 @@ defines to your template/$port file before compiling this program.\n\n"
9097printf ("Add this to your template/$port file:\n\n" );
9198
9299if (strerror_p1 != strerror_p2 )
100+ {
93101printf ("STRERROR_THREADSAFE=yes\n" );
102+ strerror_threadsafe = true;
103+ }
94104else
105+ {
95106printf ("STRERROR_THREADSAFE=no\n" );
107+ strerror_threadsafe = false;
108+ }
96109
97110if (passwd_p1 != passwd_p2 )
111+ {
98112printf ("GETPWUID_THREADSAFE=yes\n" );
113+ getpwuid_threadsafe = true;
114+ }
99115else
116+ {
100117printf ("GETPWUID_THREADSAFE=no\n" );
118+ getpwuid_threadsafe = false;
119+ }
101120
102121if (hostent_p1 != hostent_p2 )
122+ {
103123printf ("GETHOSTBYNAME_THREADSAFE=yes\n" );
124+ gethostbyname_threadsafe = true;
125+ }
104126else
127+ {
105128printf ("GETHOSTBYNAME_THREADSAFE=no\n" );
129+ gethostbyname_threadsafe = false;
130+ }
106131
107132pthread_mutex_unlock (& init_mutex );/* let children exit */
108133
109134pthread_join (thread1 ,NULL );/* clean up children */
110135pthread_join (thread2 ,NULL );
111136
112- return 0 ;
137+ printf ("\n" );
138+
139+ #ifdef HAVE_STRERROR_R
140+ printf ("Your system has sterror_r(), so it doesn't use strerror().\n" );
141+ #else
142+ printf ("Your system uses strerror().\n" );
143+ if (!strerror_threadsafe )
144+ {
145+ platform_is_threadsafe = false;
146+ printf ("That function is not thread-safe\n" );
147+ }
148+ #endif
149+
150+ #ifdef HAVE_GETPWUID_R
151+ printf ("Your system has getpwuid_r(), so it doesn't use getpwuid().\n" );
152+ #else
153+ printf ("Your system uses getpwuid().\n" );
154+ if (!getpwuid_threadsafe )
155+ {
156+ platform_is_threadsafe = false;
157+ printf ("That function is not thread-safe\n" );
158+ }
159+ #endif
160+
161+ #ifdef HAVE_GETADDRINFO
162+ printf ("Your system has getaddrinfo(), so it doesn't use gethostbyname()\n"
163+ "or gethostbyname_r().\n" );
164+ #else
165+ #ifdef HAVE_GETHOSTBYNAME_R
166+ printf ("Your system has gethostbyname_r(), so it doesn't use gethostbyname().\n" );
167+ #else
168+ printf ("Your system uses gethostbyname().\n" );
169+ if (!gethostbyname_threadsafe )
170+ {
171+ platform_is_threadsafe = false;
172+ printf ("That function is not thread-safe\n" );
173+ }
174+ #endif
175+ #endif
176+
177+ if (!platform_is_threadsafe )
178+ {
179+ printf ("\n** YOUR PLATFORM IS NOT THREADSAFE **\n" );
180+ return 1 ;
181+ }
182+ else
183+ {
184+ printf ("\nYOUR PLATFORM IS THREADSAFE\n" );
185+ return 0 ;
186+ }
113187}
114188
115189void func_call_1 (void ) {