1 /* Creating and controlling POSIX threads.
2 Copyright (C) 2010-2021 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 3, or (at your option)
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. */
24 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
25 # include "windows-thread.h"
30 typedef void * (* pthread_main_function_t) (void *);
32 #if ((defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS) || !HAVE_PTHREAD_H
35 pthread_attr_init (pthread_attr_t *attr)
37 *attr = PTHREAD_CREATE_JOINABLE;
42 pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachstatep)
44 *detachstatep = *attr & (PTHREAD_CREATE_JOINABLE | PTHREAD_CREATE_DETACHED);
49 pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate)
51 if (!(detachstate == PTHREAD_CREATE_JOINABLE
52 || detachstate == PTHREAD_CREATE_DETACHED))
54 *attr ^= (*attr ^ detachstate)
55 & (PTHREAD_CREATE_JOINABLE | PTHREAD_CREATE_DETACHED);
60 pthread_attr_destroy (pthread_attr_t *attr _GL_UNUSED)
67 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
68 /* Use Windows threads. */
71 pthread_create (pthread_t *threadp, const pthread_attr_t *attr,
72 pthread_main_function_t mainfunc, void *arg)
74 unsigned int glwthread_attr =
76 && (*attr & (PTHREAD_CREATE_JOINABLE | PTHREAD_CREATE_DETACHED))
77 != PTHREAD_CREATE_JOINABLE
78 ? GLWTHREAD_ATTR_DETACHED
80 return glwthread_thread_create (threadp, glwthread_attr, mainfunc, arg);
86 return glwthread_thread_self ();
90 pthread_equal (pthread_t thread1, pthread_t thread2)
92 return thread1 == thread2;
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);
108 pthread_exit (void *value)
110 glwthread_thread_exit (value);
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);
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. */
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. */
165 pthread_join (pthread_t thread, void **valuep)
167 /* There are no joinable threads. */
172 pthread_exit (void *value)
174 /* There is just one thread, so the process exits. */