binutils-gdb/gdb/testsuite/gdb.threads/linux-dp.c
Mark Wielaard 37bc665e4e Remove testsuite compile errors with GCC5.
GCC5 defaults to the GNU11 standard for C and warns by default for
implicit function declarations and implicit return types.
https://gcc.gnu.org/gcc-5/porting_to.html

Fixing these issues in the testsuite turns 9 untested and 17 unsupported
testcases into 417 new passes when compiling with GCC5.

gdb/testsuite/ChangeLog:

        * gdb.arch/i386-bp_permanent.c (standard): New declaration.
        * gdb.base/disp-step-fork.c: Include unistd.h.
        * gdb.base/siginfo-obj.c: Include stdio.h.
        * gdb.base/siginfo-thread.c: Likewise.
        * gdb.mi/non-stop.c: Include unistd.h.
        * gdb.mi/nsthrexec.c: Include stdio.h.
        * gdb.mi/pthreads.c: Include unistd.h.
        * gdb.modula2/unbounded1.c (main): Declare returns int.
        * gdb.reverse/consecutive-reverse.c: Likewise.
        * gdb.threads/create-fail.c: Include unistd.h.
        * gdb.threads/killed.c: Likewise.
        * gdb.threads/linux-dp.c: Likewise.
        * gdb.threads/non-ldr-exc-1.c: Include stdio.h and string.h.
        * gdb.threads/non-ldr-exc-2.c: Likewise.
        * gdb.threads/non-ldr-exc-3.c: Likewise.
        * gdb.threads/non-ldr-exc-4.c: Likewise.
        * gdb.threads/pthreads.c: Include unistd.h.
        (main): Declare returns int.
        * gdb.threads/tls-main.c (foo): New declaration.
        * gdb.threads/watchpoint-fork-mt.c: Define _GNU_SOURCE.
2015-01-25 18:50:56 +01:00

208 lines
4.3 KiB
C

/* linux-dp.c --- dining philosophers, on LinuxThreads
Jim Blandy <jimb@cygnus.com> --- March 1999 */
/* It's okay to edit this file and shift line numbers around. The
tests use gdb_get_line_number to find source locations, so they
don't depend on having certain line numbers in certain places. */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
/* The number of philosophers at the table. */
int num_philosophers;
/* Mutex ordering -
If you want to lock a mutex M, all the mutexes you have locked
already must appear before M on this list.
fork_mutex[0]
fork_mutex[1]
...
fork_mutex[num_philosophers - 1]
stdout_mutex
random_mutex
*/
/* You must hold this mutex while writing to stdout. */
pthread_mutex_t stdout_mutex;
/* You must hold this mutex while calling any of the random number
generation routines. */
pthread_mutex_t random_mutex;
/* array of mutexes, one for each fork; fork_mutex[i] is to the left
of philosopher i. A philosopher is holding fork i iff his/her
thread has locked fork_mutex[i]. */
pthread_mutex_t *fork_mutex;
/* array of threads, one representing each philosopher. */
pthread_t *philosophers;
void *
xmalloc (size_t n)
{
void *p = malloc (n);
if (! p)
{
fprintf (stderr, "out of memory\n");
exit (2);
}
return p;
}
void
shared_printf (char *format, ...)
{
va_list ap;
va_start (ap, format);
pthread_mutex_lock (&stdout_mutex);
vprintf (format, ap);
pthread_mutex_unlock (&stdout_mutex);
va_end (ap);
}
int
shared_random ()
{
int result;
pthread_mutex_lock (&random_mutex);
result = rand ();
pthread_mutex_unlock (&random_mutex);
return result;
}
void
my_usleep (long usecs)
{
struct timeval timeout;
timeout.tv_sec = usecs / 1000000;
timeout.tv_usec = usecs % 1000000;
select (0, 0, 0, 0, &timeout);
}
void
random_delay ()
{
my_usleep ((shared_random () % 2000) * 100);
}
void
print_philosopher (int n, char left, char right)
{
int i;
shared_printf ("%*s%c %d %c\n", (n * 4) + 2, "", left, n, right);
}
void *
philosopher (void *data)
{
int n = * (int *) data;
print_philosopher (n, '_', '_');
#if 1
if (n == num_philosophers - 1)
for (;;)
{
/* The last philosopher is different. He goes for his right
fork first, so there is no cycle in the mutex graph. */
/* Grab the right fork. */
pthread_mutex_lock (&fork_mutex[(n + 1) % num_philosophers]);
print_philosopher (n, '_', '!');
random_delay ();
/* Then grab the left fork. */
pthread_mutex_lock (&fork_mutex[n]);
print_philosopher (n, '!', '!');
random_delay ();
print_philosopher (n, '_', '_');
pthread_mutex_unlock (&fork_mutex[n]);
pthread_mutex_unlock (&fork_mutex[(n + 1) % num_philosophers]);
random_delay ();
}
else
#endif
for (;;)
{
/* Grab the left fork. */
pthread_mutex_lock (&fork_mutex[n]);
print_philosopher (n, '!', '_');
random_delay ();
/* Then grab the right fork. */
pthread_mutex_lock (&fork_mutex[(n + 1) % num_philosophers]);
print_philosopher (n, '!', '!');
random_delay ();
print_philosopher (n, '_', '_');
pthread_mutex_unlock (&fork_mutex[n]);
pthread_mutex_unlock (&fork_mutex[(n + 1) % num_philosophers]);
random_delay ();
}
return (void *) 0;
}
int
main (int argc, char **argv)
{
num_philosophers = 5;
/* Set up the mutexes. */
{
pthread_mutexattr_t ma;
int i;
pthread_mutexattr_init (&ma);
pthread_mutex_init (&stdout_mutex, &ma);
pthread_mutex_init (&random_mutex, &ma);
fork_mutex = xmalloc (num_philosophers * sizeof (fork_mutex[0]));
for (i = 0; i < num_philosophers; i++)
pthread_mutex_init (&fork_mutex[i], &ma);
pthread_mutexattr_destroy (&ma);
}
/* Set off the threads. */
{
int i;
int *numbers = xmalloc (num_philosophers * sizeof (*numbers));
pthread_attr_t ta;
philosophers = xmalloc (num_philosophers * sizeof (*philosophers));
pthread_attr_init (&ta);
for (i = 0; i < num_philosophers; i++)
{
numbers[i] = i;
/* linuxthreads.exp: create philosopher */
pthread_create (&philosophers[i], &ta, philosopher, &numbers[i]);
}
pthread_attr_destroy (&ta);
}
/* linuxthreads.exp: info threads 2 */
sleep (1000000);
/* Drink yourself into oblivion. */
for (;;)
sleep (1000000);
return 0;
}