gnulib: update to 776af40e0
[deliverable/binutils-gdb.git] / gnulib / import / glthread / threadlib.c
CommitLineData
5abebf3c 1/* Multithreading primitives.
9c9d63b1 2 Copyright (C) 2005-2021 Free Software Foundation, Inc.
5abebf3c
CB
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 3, 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
c0c3707f 15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
5abebf3c
CB
16
17/* Written by Bruno Haible <bruno@clisp.org>, 2005. */
18
19#include <config.h>
20
21/* ========================================================================= */
22
c0c3707f 23#if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS
5abebf3c
CB
24
25/* Use the POSIX threads library. */
26
9c9d63b1 27# include <errno.h>
5abebf3c
CB
28# include <pthread.h>
29# include <stdlib.h>
30
31# if PTHREAD_IN_USE_DETECTION_HARD
32
9c9d63b1
PM
33# if defined __FreeBSD__ || defined __DragonFly__ /* FreeBSD */
34
35/* Test using pthread_key_create. */
36
37int
38glthread_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
5abebf3c
CB
65/* The function to be executed by a dummy thread. */
66static void *
67dummy_thread_func (void *arg)
68{
69 return arg;
70}
71
72int
73glthread_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
9c9d63b1
PM
98# endif
99
5abebf3c
CB
100# endif
101
102#endif
103
104/* ========================================================================= */
105
106/* This declaration is solely to ensure that after preprocessing
107 this file is never empty. */
108typedef int dummy;
This page took 0.085382 seconds and 4 git commands to generate.