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
|
|
|
|
*
|
2003-11-30 03:52:15 +08:00
|
|
|
* $PostgreSQL: pgsql/src/tools/thread/thread_test.c,v 1.5 2003/11/29 19:52:14 pgsql 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.
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-11-24 21:16:22 +08:00
|
|
|
#ifdef ENABLE_THREAD_SAFETY
|
2003-09-04 03:30:31 +08:00
|
|
|
#include <pthread.h>
|
2003-10-25 04:48:10 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
void func_call_1(void);
|
|
|
|
void func_call_2(void);
|
|
|
|
|
2003-09-27 23:32:48 +08:00
|
|
|
#ifndef HAVE_GETADDRINFO
|
2003-09-04 03:30:31 +08:00
|
|
|
struct hostent *hostent_p1;
|
|
|
|
struct hostent *hostent_p2;
|
2003-09-27 23:32:48 +08:00
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
|
|
|
|
struct passwd *passwd_p1;
|
|
|
|
struct passwd *passwd_p2;
|
|
|
|
|
|
|
|
char *strerror_p1;
|
|
|
|
char *strerror_p2;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pthread_t thread1,
|
|
|
|
thread2;
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-09-27 23:32:48 +08:00
|
|
|
printf("\
|
|
|
|
Make sure you have added any needed 'THREAD_CPPFLAGS' and 'THREAD_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
|
|
|
);
|
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);
|
2003-09-04 03:30:31 +08:00
|
|
|
pthread_join(thread1, NULL);
|
|
|
|
pthread_join(thread2, NULL);
|
|
|
|
|
2003-09-27 23:32:48 +08:00
|
|
|
#ifndef HAVE_GETADDRINFO
|
2003-09-04 03:36:31 +08:00
|
|
|
if (hostent_p1 == hostent_p2)
|
|
|
|
printf("Your gethostbyname() is _not_ thread-safe\n");
|
2003-09-27 23:32:48 +08:00
|
|
|
#endif
|
2003-09-04 03:36:31 +08:00
|
|
|
if (passwd_p1 == passwd_p2)
|
|
|
|
printf("Your getpwuid() is _not_ thread-safe\n");
|
|
|
|
if (strerror_p1 == strerror_p2)
|
|
|
|
printf("Your strerror() is _not_ thread-safe\n");
|
|
|
|
|
2003-09-27 23:32:48 +08:00
|
|
|
if (
|
|
|
|
#ifndef HAVE_GETADDRINFO
|
|
|
|
hostent_p1 != hostent_p2 &&
|
|
|
|
#endif
|
2003-09-04 03:30:31 +08:00
|
|
|
passwd_p1 != passwd_p2 &&
|
|
|
|
strerror_p1 != strerror_p2)
|
2003-09-27 23:32:48 +08:00
|
|
|
{
|
|
|
|
printf("All your non-*_r functions are thread-safe.\n");
|
2003-09-28 00:24:45 +08:00
|
|
|
printf("Add this to your template/$port file:\n\n");
|
|
|
|
printf("NEED_REENTRANT_FUNCS=no\n");
|
2003-09-27 23:32:48 +08:00
|
|
|
}
|
2003-09-04 03:30:31 +08:00
|
|
|
else
|
2003-09-27 23:32:48 +08:00
|
|
|
{
|
|
|
|
printf("Not all non-*_r functions are thread-safe.\n");
|
2003-09-28 00:24:45 +08:00
|
|
|
printf("Add this to your template/$port file:\n\n");
|
|
|
|
printf("NEED_REENTRANT_FUNCS=yes\n");
|
2003-09-27 23:32:48 +08:00
|
|
|
}
|
2003-09-04 03:30:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void func_call_1(void) {
|
|
|
|
void *p;
|
|
|
|
|
2003-09-27 23:32:48 +08:00
|
|
|
#ifndef HAVE_GETADDRINFO
|
2003-09-04 03:30:31 +08:00
|
|
|
hostent_p1 = gethostbyname("yahoo.com");
|
|
|
|
p = gethostbyname("slashdot.org");
|
|
|
|
if (hostent_p1 != p)
|
|
|
|
{
|
|
|
|
printf("Your gethostbyname() changes the static memory area between calls\n");
|
|
|
|
hostent_p1 = NULL; /* force thread-safe failure report */
|
|
|
|
}
|
2003-09-27 23:32:48 +08:00
|
|
|
#endif
|
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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void func_call_2(void) {
|
|
|
|
void *p;
|
|
|
|
|
2003-09-27 23:32:48 +08:00
|
|
|
#ifndef HAVE_GETADDRINFO
|
2003-09-04 03:30:31 +08:00
|
|
|
hostent_p2 = gethostbyname("google.com");
|
|
|
|
p = gethostbyname("postgresql.org");
|
|
|
|
if (hostent_p2 != p)
|
|
|
|
{
|
|
|
|
printf("Your gethostbyname() changes the static memory area between calls\n");
|
|
|
|
hostent_p2 = NULL; /* force thread-safe failure report */
|
|
|
|
}
|
2003-09-27 23:32:48 +08:00
|
|
|
#endif
|
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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
}
|