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