Skip to content

Commit a1d63d9

Browse files
committed
Have threads wait for parent to test thread-specific pointers before
exiting.
1 parent 422d481 commit a1d63d9

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/tools/thread/thread_test.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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.12 2004/04/05 02:22:14 momjian Exp $
9+
* $PostgreSQL: pgsql/src/tools/thread/thread_test.c,v 1.13 2004/04/05 05:43:06 momjian Exp $
1010
*
1111
* This program tests to see if your standard libc functions use
1212
* pthread_setspecific()/pthread_getspecific() to be thread-safe.
@@ -40,6 +40,9 @@ char myhostname[MAXHOSTNAMELEN];
4040
volatile int errno1_set = 0;
4141
volatile int errno2_set = 0;
4242

43+
volatile int thread1_done = 0;
44+
volatile int thread2_done = 0;
45+
4346
char *strerror_p1;
4447
char *strerror_p2;
4548

@@ -49,9 +52,8 @@ struct passwd *passwd_p2;
4952
struct hostent *hostent_p1;
5053
struct hostent *hostent_p2;
5154

52-
pthread_mutex_t singlethread_lock1 = PTHREAD_MUTEX_INITIALIZER;
53-
pthread_mutex_t singlethread_lock2 = PTHREAD_MUTEX_INITIALIZER;
54-
55+
pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
56+
5557
int main(int argc, char *argv[])
5658
{
5759
pthread_t thread1,
@@ -73,10 +75,15 @@ int main(int argc, char *argv[])
7375
Make sure you have added any needed 'THREAD_CPPFLAGS' and 'THREAD_LIBS'\n\
7476
defines to your template/$port file before compiling this program.\n\n"
7577
);
78+
79+
/* Hold lock until we are ready for the child threads to exit. */
80+
pthread_mutex_lock(&init_mutex);
81+
7682
pthread_create(&thread1, NULL, (void * (*)(void *)) func_call_1, NULL);
7783
pthread_create(&thread2, NULL, (void * (*)(void *)) func_call_2, NULL);
78-
pthread_join(thread1, NULL);
79-
pthread_join(thread2, NULL);
84+
85+
while (thread1_done == 0 || thread2_done == 0)
86+
getpid(); /* force system call */
8087

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

@@ -94,7 +101,12 @@ defines to your template/$port file before compiling this program.\n\n"
94101
printf("GETHOSTBYNAME_THREADSAFE=yes\n");
95102
else
96103
printf("GETHOSTBYNAME_THREADSAFE=no\n");
104+
105+
pthread_mutex_unlock(&init_mutex); /* let children exit */
97106

107+
pthread_join(thread1, NULL); /* clean up children */
108+
pthread_join(thread2, NULL);
109+
98110
return 0;
99111
}
100112

@@ -110,7 +122,11 @@ void func_call_1(void) {
110122
fprintf(stderr, "could not generate failure for create file in /tmp, exiting\n");
111123
exit(1);
112124
}
113-
/* wait for other thread to set errno */
125+
/*
126+
* Wait for other thread to set errno.
127+
* We can't use thread-specific locking here because it might
128+
* affect errno.
129+
*/
114130
errno1_set = 1;
115131
while (errno2_set == 0)
116132
getpid(); /* force system call */
@@ -144,6 +160,10 @@ void func_call_1(void) {
144160
printf("Your gethostbyname() changes the static memory area between calls\n");
145161
hostent_p1 = NULL; /* force thread-safe failure report */
146162
}
163+
164+
thread1_done = 1;
165+
pthread_mutex_lock(&init_mutex); /* wait for parent to test */
166+
pthread_mutex_unlock(&init_mutex);
147167
}
148168

149169

@@ -157,7 +177,11 @@ void func_call_2(void) {
157177
fprintf(stderr, "Read-only open succeeded without create, exiting\n");
158178
exit(1);
159179
}
160-
/* wait for other thread to set errno */
180+
/*
181+
* Wait for other thread to set errno.
182+
* We can't use thread-specific locking here because it might
183+
* affect errno.
184+
*/
161185
errno2_set = 1;
162186
while (errno1_set == 0)
163187
getpid(); /* force system call */
@@ -191,4 +215,8 @@ void func_call_2(void) {
191215
printf("Your gethostbyname() changes the static memory area between calls\n");
192216
hostent_p2 = NULL; /* force thread-safe failure report */
193217
}
218+
219+
thread2_done = 1;
220+
pthread_mutex_lock(&init_mutex); /* wait for parent to test */
221+
pthread_mutex_unlock(&init_mutex);
194222
}

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