xref: /netbsd-src/external/gpl3/gdb/dist/gnulib/import/glthread/threadlib.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
18dffb485Schristos /* Multithreading primitives.
2*4b169a6bSchristos    Copyright (C) 2005-2022 Free Software Foundation, Inc.
38dffb485Schristos 
4*4b169a6bSchristos    This file is free software: you can redistribute it and/or modify
5*4b169a6bSchristos    it under the terms of the GNU Lesser General Public License as
6*4b169a6bSchristos    published by the Free Software Foundation; either version 2.1 of the
7*4b169a6bSchristos    License, or (at your option) any later version.
88dffb485Schristos 
9*4b169a6bSchristos    This file is distributed in the hope that it will be useful,
108dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
118dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*4b169a6bSchristos    GNU Lesser General Public License for more details.
138dffb485Schristos 
14*4b169a6bSchristos    You should have received a copy of the GNU Lesser General Public License
15*4b169a6bSchristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
168dffb485Schristos 
178dffb485Schristos /* Written by Bruno Haible <bruno@clisp.org>, 2005.  */
188dffb485Schristos 
198dffb485Schristos #include <config.h>
208dffb485Schristos 
218dffb485Schristos /* ========================================================================= */
228dffb485Schristos 
238dffb485Schristos #if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS
248dffb485Schristos 
258dffb485Schristos /* Use the POSIX threads library.  */
268dffb485Schristos 
27*4b169a6bSchristos # include <errno.h>
288dffb485Schristos # include <pthread.h>
298dffb485Schristos # include <stdlib.h>
308dffb485Schristos 
318dffb485Schristos # if PTHREAD_IN_USE_DETECTION_HARD
328dffb485Schristos 
33*4b169a6bSchristos #  if defined __FreeBSD__ || defined __DragonFly__                 /* FreeBSD */
34*4b169a6bSchristos 
35*4b169a6bSchristos /* Test using pthread_key_create.  */
36*4b169a6bSchristos 
37*4b169a6bSchristos int
glthread_in_use(void)38*4b169a6bSchristos glthread_in_use (void)
39*4b169a6bSchristos {
40*4b169a6bSchristos   static int tested;
41*4b169a6bSchristos   static int result; /* 1: linked with -lpthread, 0: only with libc */
42*4b169a6bSchristos 
43*4b169a6bSchristos   if (!tested)
44*4b169a6bSchristos     {
45*4b169a6bSchristos       pthread_key_t key;
46*4b169a6bSchristos       int err = pthread_key_create (&key, NULL);
47*4b169a6bSchristos 
48*4b169a6bSchristos       if (err == ENOSYS)
49*4b169a6bSchristos         result = 0;
50*4b169a6bSchristos       else
51*4b169a6bSchristos         {
52*4b169a6bSchristos           result = 1;
53*4b169a6bSchristos           if (err == 0)
54*4b169a6bSchristos             pthread_key_delete (key);
55*4b169a6bSchristos         }
56*4b169a6bSchristos       tested = 1;
57*4b169a6bSchristos     }
58*4b169a6bSchristos   return result;
59*4b169a6bSchristos }
60*4b169a6bSchristos 
61*4b169a6bSchristos #  else                                                     /* Solaris, HP-UX */
62*4b169a6bSchristos 
63*4b169a6bSchristos /* Test using pthread_create.  */
64*4b169a6bSchristos 
658dffb485Schristos /* The function to be executed by a dummy thread.  */
668dffb485Schristos static void *
dummy_thread_func(void * arg)678dffb485Schristos dummy_thread_func (void *arg)
688dffb485Schristos {
698dffb485Schristos   return arg;
708dffb485Schristos }
718dffb485Schristos 
728dffb485Schristos int
glthread_in_use(void)738dffb485Schristos glthread_in_use (void)
748dffb485Schristos {
758dffb485Schristos   static int tested;
768dffb485Schristos   static int result; /* 1: linked with -lpthread, 0: only with libc */
778dffb485Schristos 
788dffb485Schristos   if (!tested)
798dffb485Schristos     {
808dffb485Schristos       pthread_t thread;
818dffb485Schristos 
828dffb485Schristos       if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0)
838dffb485Schristos         /* Thread creation failed.  */
848dffb485Schristos         result = 0;
858dffb485Schristos       else
868dffb485Schristos         {
878dffb485Schristos           /* Thread creation works.  */
888dffb485Schristos           void *retval;
898dffb485Schristos           if (pthread_join (thread, &retval) != 0)
908dffb485Schristos             abort ();
918dffb485Schristos           result = 1;
928dffb485Schristos         }
938dffb485Schristos       tested = 1;
948dffb485Schristos     }
958dffb485Schristos   return result;
968dffb485Schristos }
978dffb485Schristos 
988dffb485Schristos #  endif
998dffb485Schristos 
1008dffb485Schristos # endif
1018dffb485Schristos 
102*4b169a6bSchristos #endif
103*4b169a6bSchristos 
1048dffb485Schristos /* ========================================================================= */
1058dffb485Schristos 
1068dffb485Schristos /* This declaration is solely to ensure that after preprocessing
1078dffb485Schristos    this file is never empty.  */
1088dffb485Schristos typedef int dummy;
109