Home | History | Annotate | Line # | Download | only in glthread
      1 /* Multithreading primitives.
      2    Copyright (C) 2005-2022 Free Software Foundation, Inc.
      3 
      4    This file is free software: you can redistribute it and/or modify
      5    it under the terms of the GNU Lesser General Public License as
      6    published by the Free Software Foundation; either version 2.1 of the
      7    License, or (at your option) any later version.
      8 
      9    This file is distributed in the hope that it will be useful,
     10    but WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12    GNU Lesser General Public License for more details.
     13 
     14    You should have received a copy of the GNU Lesser General Public License
     15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
     16 
     17 /* Written by Bruno Haible <bruno (at) clisp.org>, 2005.  */
     18 
     19 #include <config.h>
     20 
     21 /* ========================================================================= */
     22 
     23 #if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS
     24 
     25 /* Use the POSIX threads library.  */
     26 
     27 # include <errno.h>
     28 # include <pthread.h>
     29 # include <stdlib.h>
     30 
     31 # if PTHREAD_IN_USE_DETECTION_HARD
     32 
     33 #  if defined __FreeBSD__ || defined __DragonFly__                 /* FreeBSD */
     34 
     35 /* Test using pthread_key_create.  */
     36 
     37 int
     38 glthread_in_use (void)
     39 {
     40   static int tested;
     41   static int result; /* 1: linked with -lpthread, 0: only with libc */
     42 
     43   if (!tested)
     44     {
     45       pthread_key_t key;
     46       int err = pthread_key_create (&key, NULL);
     47 
     48       if (err == ENOSYS)
     49         result = 0;
     50       else
     51         {
     52           result = 1;
     53           if (err == 0)
     54             pthread_key_delete (key);
     55         }
     56       tested = 1;
     57     }
     58   return result;
     59 }
     60 
     61 #  else                                                     /* Solaris, HP-UX */
     62 
     63 /* Test using pthread_create.  */
     64 
     65 /* The function to be executed by a dummy thread.  */
     66 static void *
     67 dummy_thread_func (void *arg)
     68 {
     69   return arg;
     70 }
     71 
     72 int
     73 glthread_in_use (void)
     74 {
     75   static int tested;
     76   static int result; /* 1: linked with -lpthread, 0: only with libc */
     77 
     78   if (!tested)
     79     {
     80       pthread_t thread;
     81 
     82       if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0)
     83         /* Thread creation failed.  */
     84         result = 0;
     85       else
     86         {
     87           /* Thread creation works.  */
     88           void *retval;
     89           if (pthread_join (thread, &retval) != 0)
     90             abort ();
     91           result = 1;
     92         }
     93       tested = 1;
     94     }
     95   return result;
     96 }
     97 
     98 #  endif
     99 
    100 # endif
    101 
    102 #endif
    103 
    104 /* ========================================================================= */
    105 
    106 /* This declaration is solely to ensure that after preprocessing
    107    this file is never empty.  */
    108 typedef int dummy;
    109