old-cross-binutils/gdb/testsuite/gdb.threads/staticthreads.c
Yao Qi b8b91e982e Fix the race in gdb.threads/staticthreads.exp
The code in gdb.threads/staticthreads.exp about checking the value of
tlsvar in main thread is racy, because when child thread hits
breakpoint, the main thread may not go into pthread_join yet, and
may not be unwind to main.

This patch is to move the line setting breakpoint on after sem_wait,
so that the child thread will hit breakpoint after main thread calls
sem_post.  IOW, when child thread hits breakpoint, the main thread is
in either sem_post or pthread_join.  "up 10" can unwind main thread to
main.

gdb/testsuite:

2014-06-06  Yao Qi  <yao@codesourcery.com>

	* gdb.threads/staticthreads.c (thread_function): Move the line
	setting breakpoint on forward.
	* gdb.threads/staticthreads.exp: Update comments.
2014-06-06 09:45:22 +08:00

81 lines
1.8 KiB
C

/* This test program is part of GDB, The GNU debugger.
Copyright 2004-2014 Free Software Foundation, Inc.
Originally written by Jeff Johnston <jjohnstn@redhat.com>,
contributed by Red Hat
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <limits.h>
#include <errno.h>
sem_t semaphore;
#ifdef HAVE_TLS
__thread int tlsvar;
#endif
void *
thread_function (void *arg)
{
#ifdef HAVE_TLS
tlsvar = 2;
#endif
while (sem_wait (&semaphore) != 0)
{
if (errno != EINTR)
{
perror ("thread_function");
return NULL;
}
}
printf ("Thread executing\n"); /* tlsvar-is-set */
return NULL;
}
int
main (int argc, char **argv)
{
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
if (sem_init (&semaphore, 0, 0) == -1)
{
perror ("semaphore");
return -1;
}
#ifdef HAVE_TLS
tlsvar = 1;
#endif
/* Create a thread, wait for it to complete. */
{
pthread_t thread;
pthread_create (&thread, &attr, thread_function, NULL);
sem_post (&semaphore);
pthread_join (thread, NULL);
}
pthread_attr_destroy (&attr);
return 0;
}