Home | History | Annotate | Line # | Download | only in gdb.threads
      1 /* This testcase is part of GDB, the GNU debugger.
      2 
      3    Copyright 2015-2024 Free Software Foundation, Inc.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #define _GNU_SOURCE
     19 #include <unistd.h>
     20 #include <stdlib.h>
     21 #include <pthread.h>
     22 #include <assert.h>
     23 
     24 #define NUM_THREADS 3
     25 
     26 struct thread_data
     27 {
     28   const char *name;
     29   pthread_barrier_t *barrier;
     30 };
     31 
     32 static void *
     33 thread_func (void *varg)
     34 {
     35   struct thread_data *arg = (struct thread_data *) varg;
     36   int res;
     37 
     38   res = pthread_setname_np (pthread_self (), arg->name);
     39   assert (res == 0);
     40 
     41   pthread_barrier_wait (arg->barrier);
     42 
     43   pthread_barrier_wait (arg->barrier);
     44 
     45   return NULL;
     46 }
     47 
     48 static void
     49 all_threads_ready (void)
     50 {
     51 }
     52 
     53 int
     54 main (int argc, char **argv)
     55 {
     56   pthread_t threads[NUM_THREADS];
     57   struct thread_data args[NUM_THREADS];
     58   pthread_barrier_t barrier;
     59   int i, res;
     60   const char *names[] = { "carrot", "potato", "celery" };
     61 
     62   alarm (20);
     63 
     64   /* Make sure that NAMES contains NUM_THREADS elements.  */
     65   assert (sizeof (names) / sizeof (names[0]) == NUM_THREADS);
     66 
     67   res = pthread_barrier_init (&barrier, NULL, NUM_THREADS + 1);
     68   assert (res == 0);;
     69 
     70   res = pthread_setname_np (pthread_self (), "main");
     71   assert (res == 0);
     72 
     73   for (i = 0; i < NUM_THREADS; i++)
     74     {
     75       struct thread_data *arg = &args[i];
     76 
     77       arg->name = names[i];
     78       arg->barrier = &barrier;
     79 
     80       res = pthread_create (&threads[i], NULL, thread_func, arg);
     81       assert (res == 0);
     82     }
     83 
     84   pthread_barrier_wait (&barrier);
     85 
     86   all_threads_ready ();
     87 
     88   pthread_barrier_wait (&barrier);
     89 
     90   for (i = 0; i < NUM_THREADS; i++)
     91     {
     92       res = pthread_join (threads[i], NULL);
     93       assert (res == 0);
     94     }
     95 
     96   return 0;
     97 }
     98