execute, spawn-pipe: Make multithread-safe on native Windows.
[gnulib.git] / lib / pthread-thread.c
blobb0ba8700f0179c572e497725a0111724ef8080ae
1 /* Creating and controlling POSIX threads.
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert, 2010, and Bruno Haible <bruno@clisp.org>, 2019. */
19 #include <config.h>
21 /* Specification. */
22 #include <pthread.h>
24 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
25 # include "windows-thread.h"
26 #else
27 # include <stdlib.h>
28 #endif
30 typedef void * (* pthread_main_function_t) (void *);
32 #if ((defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS) || !HAVE_PTHREAD_H
34 int
35 pthread_attr_init (pthread_attr_t *attr)
37 *attr = PTHREAD_CREATE_JOINABLE;
38 return 0;
41 int
42 pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachstatep)
44 *detachstatep = *attr & (PTHREAD_CREATE_JOINABLE | PTHREAD_CREATE_DETACHED);
45 return 0;
48 int
49 pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate)
51 if (!(detachstate == PTHREAD_CREATE_JOINABLE
52 || detachstate == PTHREAD_CREATE_DETACHED))
53 return EINVAL;
54 *attr ^= (*attr ^ detachstate)
55 & (PTHREAD_CREATE_JOINABLE | PTHREAD_CREATE_DETACHED);
56 return 0;
59 int
60 pthread_attr_destroy (pthread_attr_t *attr _GL_UNUSED)
62 return 0;
65 #endif
67 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
68 /* Use Windows threads. */
70 int
71 pthread_create (pthread_t *threadp, const pthread_attr_t *attr,
72 pthread_main_function_t mainfunc, void *arg)
74 unsigned int glwthread_attr =
75 (attr != NULL
76 && (*attr & (PTHREAD_CREATE_JOINABLE | PTHREAD_CREATE_DETACHED))
77 != PTHREAD_CREATE_JOINABLE
78 ? GLWTHREAD_ATTR_DETACHED
79 : 0);
80 return glwthread_thread_create (threadp, glwthread_attr, mainfunc, arg);
83 pthread_t
84 pthread_self (void)
86 return glwthread_thread_self ();
89 int
90 pthread_equal (pthread_t thread1, pthread_t thread2)
92 return thread1 == thread2;
95 int
96 pthread_detach (pthread_t thread)
98 return glwthread_thread_detach (thread);
102 pthread_join (pthread_t thread, void **valuep)
104 return glwthread_thread_join (thread, valuep);
107 void
108 pthread_exit (void *value)
110 glwthread_thread_exit (value);
113 #elif HAVE_PTHREAD_H
114 /* Provide workarounds for POSIX threads. */
116 # if PTHREAD_CREATE_IS_INLINE
118 pthread_create (pthread_t *threadp, const pthread_attr_t *attr,
119 pthread_main_function_t mainfunc, void *arg)
120 # undef pthread_create
122 return pthread_create (threadp, attr, mainfunc, arg);
126 pthread_attr_init (pthread_attr_t *attr)
127 # undef pthread_attr_init
129 return pthread_attr_init (attr);
132 # endif
134 #else
135 /* Provide a dummy implementation for single-threaded applications. */
138 pthread_create (pthread_t *threadp, const pthread_attr_t *attr,
139 pthread_main_function_t mainfunc, void *arg)
141 /* The maximum number of threads is reached. Do not create a thread. */
142 return EAGAIN;
145 pthread_t
146 pthread_self (void)
148 return 42;
152 pthread_equal (pthread_t thread1, pthread_t thread2)
154 return thread1 == thread2;
158 pthread_detach (pthread_t thread)
160 /* There are no joinable threads. */
161 return EINVAL;
165 pthread_join (pthread_t thread, void **valuep)
167 /* There are no joinable threads. */
168 return EINVAL;
171 void
172 pthread_exit (void *value)
174 /* There is just one thread, so the process exits. */
175 exit (0);
178 #endif