Fix 2008-08-03 change.
[gnulib.git] / lib / glthread / threadlib.c
1 /* Multithreading primitives.
2    Copyright (C) 2005-2008 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program 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 General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2005.  */
19
20 #include <config.h>
21
22 /* ========================================================================= */
23
24 #if USE_POSIX_THREADS
25
26 /* Use the POSIX threads library.  */
27
28 # if PTHREAD_IN_USE_DETECTION_HARD
29
30 /* The function to be executed by a dummy thread.  */
31 static void *
32 dummy_thread_func (void *arg)
33 {
34   return arg;
35 }
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_t thread;
46
47       if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0)
48         /* Thread creation failed.  */
49         result = 0;
50       else
51         {
52           /* Thread creation works.  */
53           void *retval;
54           if (pthread_join (thread, &retval) != 0)
55             abort ();
56           result = 1;
57         }
58       tested = 1;
59     }
60   return result;
61 }
62
63 # endif
64
65 #endif
66
67 /* ========================================================================= */