Include <pthread.h>.
[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 # include <pthread.h>
29
30 # if PTHREAD_IN_USE_DETECTION_HARD
31
32 /* The function to be executed by a dummy thread.  */
33 static void *
34 dummy_thread_func (void *arg)
35 {
36   return arg;
37 }
38
39 int
40 glthread_in_use (void)
41 {
42   static int tested;
43   static int result; /* 1: linked with -lpthread, 0: only with libc */
44
45   if (!tested)
46     {
47       pthread_t thread;
48
49       if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0)
50         /* Thread creation failed.  */
51         result = 0;
52       else
53         {
54           /* Thread creation works.  */
55           void *retval;
56           if (pthread_join (thread, &retval) != 0)
57             abort ();
58           result = 1;
59         }
60       tested = 1;
61     }
62   return result;
63 }
64
65 # endif
66
67 #endif
68
69 /* ========================================================================= */