2003-09-04 03:30:31 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* test_thread_funcs.c
|
|
|
|
* libc thread test program
|
|
|
|
*
|
|
|
|
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2004-04-24 02:15:55 +08:00
|
|
|
* $PostgreSQL: pgsql/src/tools/thread/thread_test.c,v 1.20 2004/04/23 18:15:55 momjian Exp $
|
2003-09-04 03:30:31 +08:00
|
|
|
*
|
|
|
|
* This program tests to see if your standard libc functions use
|
|
|
|
* pthread_setspecific()/pthread_getspecific() to be thread-safe.
|
|
|
|
* See src/port/thread.c for more details.
|
|
|
|
*
|
|
|
|
* This program first tests to see if each function returns a constant
|
|
|
|
* memory pointer within the same thread, then, assuming it does, tests
|
|
|
|
* to see if the pointers are different for different threads. If they
|
|
|
|
* are, the function is thread-safe.
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
2004-03-28 07:02:44 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-04-22 04:51:54 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2003-09-04 03:30:31 +08:00
|
|
|
void func_call_1(void);
|
|
|
|
void func_call_2(void);
|
|
|
|
|
2004-04-05 09:27:58 +08:00
|
|
|
char myhostname[MAXHOSTNAMELEN];
|
|
|
|
|
2004-04-05 10:22:14 +08:00
|
|
|
volatile int errno1_set = 0;
|
|
|
|
volatile int errno2_set = 0;
|
2004-03-28 07:02:44 +08:00
|
|
|
|
2004-04-05 13:43:06 +08:00
|
|
|
volatile int thread1_done = 0;
|
|
|
|
volatile int thread2_done = 0;
|
|
|
|
|
2004-02-12 05:44:06 +08:00
|
|
|
char *strerror_p1;
|
|
|
|
char *strerror_p2;
|
2003-09-04 03:30:31 +08:00
|
|
|
|
|
|
|
struct passwd *passwd_p1;
|
|
|
|
struct passwd *passwd_p2;
|
|
|
|
|
2004-02-12 05:44:06 +08:00
|
|
|
struct hostent *hostent_p1;
|
|
|
|
struct hostent *hostent_p2;
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-04-23 07:58:03 +08:00
|
|
|
bool gethostbyname_threadsafe = false;
|
|
|
|
bool getpwuid_threadsafe = false;
|
|
|
|
bool strerror_threadsafe = false;
|
2004-04-22 04:51:54 +08:00
|
|
|
bool platform_is_threadsafe = true;
|
|
|
|
|
2004-04-05 13:43:06 +08:00
|
|
|
pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2003-09-04 03:30:31 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pthread_t thread1,
|
|
|
|
thread2;
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-04-05 09:27:58 +08:00
|
|
|
if (gethostname(myhostname, MAXHOSTNAMELEN) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "can not get local hostname, exiting\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-09-27 23:32:48 +08:00
|
|
|
printf("\
|
2004-04-24 02:15:55 +08:00
|
|
|
Make sure you have added any needed 'PTHREAD_CFLAGS' and 'PTHREAD_LIBS'\n\
|
2003-09-28 00:24:45 +08:00
|
|
|
defines to your template/$port file before compiling this program.\n\n"
|
2003-09-27 23:32:48 +08:00
|
|
|
);
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
/* Hold lock until we are ready for the child threads to exit. */
|
|
|
|
pthread_mutex_lock(&init_mutex);
|
|
|
|
|
2003-09-04 06:34:08 +08:00
|
|
|
pthread_create(&thread1, NULL, (void * (*)(void *)) func_call_1, NULL);
|
|
|
|
pthread_create(&thread2, NULL, (void * (*)(void *)) func_call_2, NULL);
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
while (thread1_done == 0 || thread2_done == 0)
|
2004-04-05 22:13:45 +08:00
|
|
|
sched_yield(); /* if this is a portability problem, remove it */
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-04-23 07:58:03 +08:00
|
|
|
fprintf(stderr, "errno is thread-safe\n");
|
2004-04-06 21:55:17 +08:00
|
|
|
|
2004-02-12 05:44:06 +08:00
|
|
|
if (strerror_p1 != strerror_p2)
|
2004-04-22 04:51:54 +08:00
|
|
|
strerror_threadsafe = true;
|
2004-02-12 05:44:06 +08:00
|
|
|
|
|
|
|
if (passwd_p1 != passwd_p2)
|
2004-04-22 04:51:54 +08:00
|
|
|
getpwuid_threadsafe = true;
|
2003-09-04 03:30:31 +08:00
|
|
|
|
2004-02-12 05:44:06 +08:00
|
|
|
if (hostent_p1 != hostent_p2)
|
2004-04-22 04:51:54 +08:00
|
|
|
gethostbyname_threadsafe = true;
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&init_mutex); /* let children exit */
|
2004-02-12 05:44:06 +08:00
|
|
|
|
2004-04-05 13:43:06 +08:00
|
|
|
pthread_join(thread1, NULL); /* clean up children */
|
|
|
|
pthread_join(thread2, NULL);
|
|
|
|
|
2004-04-22 04:51:54 +08:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
#ifdef HAVE_STRERROR_R
|
|
|
|
printf("Your system has sterror_r(), so it doesn't use strerror().\n");
|
|
|
|
#else
|
2004-04-22 04:58:56 +08:00
|
|
|
printf("Your system uses strerror() which is ");
|
|
|
|
if (strerror_threadsafe)
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("thread-safe.\n");
|
2004-04-22 04:58:56 +08:00
|
|
|
else
|
2004-04-22 04:51:54 +08:00
|
|
|
{
|
|
|
|
platform_is_threadsafe = false;
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("not thread-safe.\n");
|
2004-04-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_GETPWUID_R
|
|
|
|
printf("Your system has getpwuid_r(), so it doesn't use getpwuid().\n");
|
|
|
|
#else
|
2004-04-22 04:58:56 +08:00
|
|
|
printf("Your system uses getpwuid() which is ");
|
|
|
|
if (getpwuid_threadsafe)
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("thread-safe.\n");
|
2004-04-22 04:58:56 +08:00
|
|
|
else
|
2004-04-22 04:51:54 +08:00
|
|
|
{
|
|
|
|
platform_is_threadsafe = false;
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("not thread-safe.\n");
|
2004-04-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
printf("Your system has getaddrinfo(), so it doesn't use gethostbyname()\n"
|
2004-04-22 04:58:56 +08:00
|
|
|
" or gethostbyname_r().\n");
|
2004-04-22 04:51:54 +08:00
|
|
|
#else
|
|
|
|
#ifdef HAVE_GETHOSTBYNAME_R
|
|
|
|
printf("Your system has gethostbyname_r(), so it doesn't use gethostbyname().\n");
|
|
|
|
#else
|
2004-04-22 04:58:56 +08:00
|
|
|
printf("Your system uses gethostbyname which is ");
|
|
|
|
if (gethostbyname_threadsafe)
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("thread-safe.\n");
|
2004-04-22 04:58:56 +08:00
|
|
|
else
|
2004-04-22 04:51:54 +08:00
|
|
|
{
|
|
|
|
platform_is_threadsafe = false;
|
2004-04-23 07:58:03 +08:00
|
|
|
printf("not thread-safe.\n");
|
2004-04-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!platform_is_threadsafe)
|
|
|
|
{
|
|
|
|
printf("\n** YOUR PLATFORM IS NOT THREADSAFE **\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("\nYOUR PLATFORM IS THREADSAFE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void func_call_1(void) {
|
|
|
|
void *p;
|
2004-03-28 07:02:44 +08:00
|
|
|
|
2004-03-28 10:37:31 +08:00
|
|
|
unlink("/tmp/thread_test.1");
|
|
|
|
/* create, then try to fail on exclusive create open */
|
|
|
|
if (open("/tmp/thread_test.1", O_RDWR | O_CREAT, 0600) < 0 ||
|
|
|
|
open("/tmp/thread_test.1", O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
|
2004-03-28 07:02:44 +08:00
|
|
|
{
|
2004-03-28 10:37:31 +08:00
|
|
|
fprintf(stderr, "Could not create file in /tmp or\n");
|
|
|
|
fprintf(stderr, "could not generate failure for create file in /tmp, exiting\n");
|
2004-03-28 07:02:44 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2004-04-05 13:43:06 +08:00
|
|
|
/*
|
|
|
|
* Wait for other thread to set errno.
|
|
|
|
* We can't use thread-specific locking here because it might
|
|
|
|
* affect errno.
|
|
|
|
*/
|
2004-03-28 07:02:44 +08:00
|
|
|
errno1_set = 1;
|
|
|
|
while (errno2_set == 0)
|
2004-04-05 22:13:45 +08:00
|
|
|
sched_yield();
|
2004-03-28 07:02:44 +08:00
|
|
|
if (errno != EEXIST)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "errno not thread-safe; exiting\n");
|
|
|
|
unlink("/tmp/thread_test.1");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
unlink("/tmp/thread_test.1");
|
|
|
|
|
2004-02-12 05:44:06 +08:00
|
|
|
strerror_p1 = strerror(EACCES);
|
|
|
|
/*
|
|
|
|
* If strerror() uses sys_errlist, the pointer might change for different
|
|
|
|
* errno values, so we don't check to see if it varies within the thread.
|
|
|
|
*/
|
2003-09-04 03:30:31 +08:00
|
|
|
|
|
|
|
passwd_p1 = getpwuid(0);
|
|
|
|
p = getpwuid(1);
|
|
|
|
if (passwd_p1 != p)
|
|
|
|
{
|
|
|
|
printf("Your getpwuid() changes the static memory area between calls\n");
|
|
|
|
passwd_p1 = NULL; /* force thread-safe failure report */
|
|
|
|
}
|
|
|
|
|
2004-04-05 09:27:58 +08:00
|
|
|
/* threads do this in opposite order */
|
|
|
|
hostent_p1 = gethostbyname(myhostname);
|
|
|
|
p = gethostbyname("localhost");
|
2004-02-12 05:44:06 +08:00
|
|
|
if (hostent_p1 != p)
|
|
|
|
{
|
|
|
|
printf("Your gethostbyname() changes the static memory area between calls\n");
|
|
|
|
hostent_p1 = NULL; /* force thread-safe failure report */
|
|
|
|
}
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
thread1_done = 1;
|
|
|
|
pthread_mutex_lock(&init_mutex); /* wait for parent to test */
|
|
|
|
pthread_mutex_unlock(&init_mutex);
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func_call_2(void) {
|
|
|
|
void *p;
|
|
|
|
|
2004-03-28 07:02:44 +08:00
|
|
|
unlink("/tmp/thread_test.2");
|
2004-03-28 10:37:31 +08:00
|
|
|
/* open non-existant file */
|
2004-03-28 07:02:44 +08:00
|
|
|
if (open("/tmp/thread_test.2", O_RDONLY, 0600) >= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Read-only open succeeded without create, exiting\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-04-05 13:43:06 +08:00
|
|
|
/*
|
|
|
|
* Wait for other thread to set errno.
|
|
|
|
* We can't use thread-specific locking here because it might
|
|
|
|
* affect errno.
|
|
|
|
*/
|
2004-03-28 07:02:44 +08:00
|
|
|
errno2_set = 1;
|
|
|
|
while (errno1_set == 0)
|
2004-04-05 22:13:45 +08:00
|
|
|
sched_yield();
|
2004-03-28 07:02:44 +08:00
|
|
|
if (errno != ENOENT)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "errno not thread-safe; exiting\n");
|
|
|
|
unlink("/tmp/thread_test.A");
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-03-28 10:37:31 +08:00
|
|
|
unlink("/tmp/thread_test.2");
|
2004-03-28 07:02:44 +08:00
|
|
|
|
2004-02-12 05:44:06 +08:00
|
|
|
strerror_p2 = strerror(EINVAL);
|
|
|
|
/*
|
|
|
|
* If strerror() uses sys_errlist, the pointer might change for different
|
|
|
|
* errno values, so we don't check to see if it varies within the thread.
|
|
|
|
*/
|
2003-09-04 03:30:31 +08:00
|
|
|
|
|
|
|
passwd_p2 = getpwuid(2);
|
|
|
|
p = getpwuid(3);
|
|
|
|
if (passwd_p2 != p)
|
|
|
|
{
|
|
|
|
printf("Your getpwuid() changes the static memory area between calls\n");
|
|
|
|
passwd_p2 = NULL; /* force thread-safe failure report */
|
|
|
|
}
|
|
|
|
|
2004-04-05 09:27:58 +08:00
|
|
|
/* threads do this in opposite order */
|
|
|
|
hostent_p2 = gethostbyname("localhost");
|
|
|
|
p = gethostbyname(myhostname);
|
2004-02-12 05:44:06 +08:00
|
|
|
if (hostent_p2 != p)
|
|
|
|
{
|
|
|
|
printf("Your gethostbyname() changes the static memory area between calls\n");
|
|
|
|
hostent_p2 = NULL; /* force thread-safe failure report */
|
|
|
|
}
|
2004-04-05 13:43:06 +08:00
|
|
|
|
|
|
|
thread2_done = 1;
|
|
|
|
pthread_mutex_lock(&init_mutex); /* wait for parent to test */
|
|
|
|
pthread_mutex_unlock(&init_mutex);
|
2003-09-04 03:30:31 +08:00
|
|
|
}
|