Home | History | Annotate | Line # | Download | only in gdb.threads
      1 /* Copyright 2023 Free Software Foundation, Inc.
      2 
      3    This program is free software; you can redistribute it and/or modify
      4    it under the terms of the GNU General Public License as published by
      5    the Free Software Foundation; either version 3 of the License, or
      6    (at your option) any later version.
      7 
      8    This program is distributed in the hope that it will be useful,
      9    but WITHOUT ANY WARRANTY; without even the implied warranty of
     10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11    GNU General Public License for more details.
     12 
     13    You should have received a copy of the GNU General Public License
     14    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     15 
     16 #include <dlfcn.h>
     17 #include <pthread.h>
     18 #include <stdlib.h>
     19 
     20 pthread_barrier_t barrier;
     21 
     22 static void
     23 barrier_wait (pthread_barrier_t *b)
     24 {
     25   int res = pthread_barrier_wait (b);
     26   if (res != 0 && res != PTHREAD_BARRIER_SERIAL_THREAD)
     27     abort ();
     28 }
     29 
     30 static void *
     31 thread_worker (void *arg)
     32 {
     33   barrier_wait (&barrier);
     34   return NULL;
     35 }
     36 
     37 void
     38 breakpt (void)
     39 {
     40   /* Nothing.  */
     41 }
     42 
     43 int
     44 main (void)
     45 {
     46   void *handle;
     47   void (*func)(int);
     48   pthread_t thread;
     49 
     50   if (pthread_barrier_init (&barrier, NULL, 2) != 0)
     51     abort ();
     52 
     53   if (pthread_create (&thread, NULL, thread_worker, NULL) != 0)
     54     abort ();
     55 
     56   breakpt ();
     57 
     58   /* Allow the worker thread to complete.  */
     59   barrier_wait (&barrier);
     60 
     61   if (pthread_join (thread, NULL) != 0)
     62     abort ();
     63 
     64   breakpt ();
     65 
     66   /* Now load the shared library.  */
     67   handle = dlopen (SHLIB_NAME, RTLD_LAZY);
     68   if (handle == NULL)
     69     abort ();
     70 
     71   /* Find the function symbol.  */
     72   func = (void (*)(int)) dlsym (handle, "foo");
     73 
     74   /* Call the library function.  */
     75   func (1);
     76 
     77   /* Unload the shared library.  */
     78   if (dlclose (handle) != 0)
     79     abort ();
     80 
     81   breakpt ();
     82 
     83   return 0;
     84 }
     85 
     86