Re: thread_test.c problems - Mailing list pgsql-general

From Bruce Momjian
Subject Re: thread_test.c problems
Date
Msg-id 200404050543.i355hMp20676@candle.pha.pa.us
Whole thread Raw
In response to Re: thread_test.c problems  (Bruce Momjian <pgman@candle.pha.pa.us>)
Responses Re: thread_test.c problems
List pgsql-general
Bruce Momjian wrote:
> > I did a cvs update, make, then rebuilt and tested thread_test.  I ran it
> > about 120 times and got "GETHOSTBYNAME_THREADSAFE=yes" almost all of the
> > time.  I got "GETHOSTBYNAME_THREADSAFE=no" three times.  The other two were
> > always "yes".
>
> Yep, I can reproduce this on BSD/OS too, so it must be something wrong
> with my program.  I see:
>
>     $ for X in `jot 1000`; do thread_test |grep -10 '=no' && echo $X;
>       done|less
>     Make sure you have added any needed 'THREAD_CPPFLAGS' and 'THREAD_LIBS'
>     defines to your template/$port file before compiling this program.
>
>     Add this to your template/$port file:
>
>     STRERROR_THREADSAFE=yes
>     GETPWUID_THREADSAFE=no
>     GETHOSTBYNAME_THREADSAFE=no
>     919
>
> I bet the problem is that I am accessing thread-specific pointers after
> the thread exits.  It is possible thread 1 completes before thread2 gets
> to the getpwuid and gethostbyname tests, and then reuses the
> thread-specific pointer.  Let me work on a patch and email it to you in
> a few minutes.

OK, new patch applied that causes all threads to wait until the parent
checks their thread-specific pointers. I ran 1000 tests and all passed.
Hopefully it will good for you too.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/tools/thread/thread_test.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/tools/thread/thread_test.c,v
retrieving revision 1.12
diff -c -c -r1.12 thread_test.c
*** src/tools/thread/thread_test.c    5 Apr 2004 02:22:14 -0000    1.12
--- src/tools/thread/thread_test.c    5 Apr 2004 05:40:13 -0000
***************
*** 40,45 ****
--- 40,48 ----
  volatile int errno1_set = 0;
  volatile int errno2_set = 0;

+ volatile int thread1_done = 0;
+ volatile int thread2_done = 0;
+
  char *strerror_p1;
  char *strerror_p2;

***************
*** 49,57 ****
  struct hostent *hostent_p1;
  struct hostent *hostent_p2;

! pthread_mutex_t singlethread_lock1 = PTHREAD_MUTEX_INITIALIZER;
! pthread_mutex_t singlethread_lock2 = PTHREAD_MUTEX_INITIALIZER;
!
  int main(int argc, char *argv[])
  {
      pthread_t        thread1,
--- 52,59 ----
  struct hostent *hostent_p1;
  struct hostent *hostent_p2;

! pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
!
  int main(int argc, char *argv[])
  {
      pthread_t        thread1,
***************
*** 73,82 ****
  Make sure you have added any needed 'THREAD_CPPFLAGS' and 'THREAD_LIBS'\n\
  defines to your template/$port file before compiling this program.\n\n"
  );
      pthread_create(&thread1, NULL, (void * (*)(void *)) func_call_1, NULL);
      pthread_create(&thread2, NULL, (void * (*)(void *)) func_call_2, NULL);
!     pthread_join(thread1, NULL);
!     pthread_join(thread2, NULL);

      printf("Add this to your template/$port file:\n\n");

--- 75,89 ----
  Make sure you have added any needed 'THREAD_CPPFLAGS' and 'THREAD_LIBS'\n\
  defines to your template/$port file before compiling this program.\n\n"
  );
+
+     /* Hold lock until we are ready for the child threads to exit. */
+     pthread_mutex_lock(&init_mutex);
+
      pthread_create(&thread1, NULL, (void * (*)(void *)) func_call_1, NULL);
      pthread_create(&thread2, NULL, (void * (*)(void *)) func_call_2, NULL);
!
!     while (thread1_done == 0 || thread2_done == 0)
!         getpid();    /* force system call */

      printf("Add this to your template/$port file:\n\n");

***************
*** 94,100 ****
--- 101,112 ----
          printf("GETHOSTBYNAME_THREADSAFE=yes\n");
      else
          printf("GETHOSTBYNAME_THREADSAFE=no\n");
+
+     pthread_mutex_unlock(&init_mutex);    /* let children exit  */

+     pthread_join(thread1, NULL);    /* clean up children */
+     pthread_join(thread2, NULL);
+
      return 0;
  }

***************
*** 110,116 ****
              fprintf(stderr, "could not generate failure for create file in /tmp, exiting\n");
              exit(1);
      }
!     /* wait for other thread to set errno */
      errno1_set = 1;
      while (errno2_set == 0)
          getpid();    /* force system call */
--- 122,132 ----
              fprintf(stderr, "could not generate failure for create file in /tmp, exiting\n");
              exit(1);
      }
!     /*
!      *    Wait for other thread to set errno.
!      *    We can't use thread-specific locking here because it might
!      *    affect errno.
!      */
      errno1_set = 1;
      while (errno2_set == 0)
          getpid();    /* force system call */
***************
*** 144,149 ****
--- 160,169 ----
          printf("Your gethostbyname() changes the static memory area between calls\n");
          hostent_p1 = NULL;    /* force thread-safe failure report */
      }
+
+     thread1_done = 1;
+     pthread_mutex_lock(&init_mutex);    /* wait for parent to test */
+     pthread_mutex_unlock(&init_mutex);
  }


***************
*** 157,163 ****
              fprintf(stderr, "Read-only open succeeded without create, exiting\n");
              exit(1);
      }
!     /* wait for other thread to set errno */
      errno2_set = 1;
      while (errno1_set == 0)
          getpid();    /* force system call */
--- 177,187 ----
              fprintf(stderr, "Read-only open succeeded without create, exiting\n");
              exit(1);
      }
!     /*
!      *    Wait for other thread to set errno.
!      *    We can't use thread-specific locking here because it might
!      *    affect errno.
!      */
      errno2_set = 1;
      while (errno1_set == 0)
          getpid();    /* force system call */
***************
*** 191,194 ****
--- 215,222 ----
          printf("Your gethostbyname() changes the static memory area between calls\n");
          hostent_p2 = NULL;    /* force thread-safe failure report */
      }
+
+     thread2_done = 1;
+     pthread_mutex_lock(&init_mutex);    /* wait for parent to test */
+     pthread_mutex_unlock(&init_mutex);
  }

pgsql-general by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: thread_test.c problems
Next
From: mike
Date:
Subject: Re: Problem restoring Database