VirtualBox

source: kBuild/vendor/grep/current/gnulib-tests/pthread-thread.c@ 3630

Last change on this file since 3630 was 3529, checked in by bird, 3 years ago

Imported grep 3.7 from grep-3.7.tar.gz (sha256: c22b0cf2d4f6bbe599c902387e8058990e1eee99aef333a203829e5fd3dbb342), applying minimal auto-props.

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1/* Creating and controlling POSIX threads.
2 Copyright (C) 2010-2021 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17/* Written by Paul Eggert, 2010, and Bruno Haible <[email protected]>, 2019. */
18
19#include <config.h>
20
21/* Specification. */
22#include <pthread.h>
23
24#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
25# include "windows-thread.h"
26#else
27# include <stdlib.h>
28#endif
29
30typedef void * (* pthread_main_function_t) (void *);
31
32#if ((defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS) || !HAVE_PTHREAD_H
33
34int
35pthread_attr_init (pthread_attr_t *attr)
36{
37 *attr = PTHREAD_CREATE_JOINABLE;
38 return 0;
39}
40
41int
42pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachstatep)
43{
44 *detachstatep = *attr & (PTHREAD_CREATE_JOINABLE | PTHREAD_CREATE_DETACHED);
45 return 0;
46}
47
48int
49pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate)
50{
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;
57}
58
59int
60pthread_attr_destroy (pthread_attr_t *attr _GL_UNUSED)
61{
62 return 0;
63}
64
65#endif
66
67#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
68/* Use Windows threads. */
69
70int
71pthread_create (pthread_t *threadp, const pthread_attr_t *attr,
72 pthread_main_function_t mainfunc, void *arg)
73{
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);
81}
82
83pthread_t
84pthread_self (void)
85{
86 return glwthread_thread_self ();
87}
88
89int
90pthread_equal (pthread_t thread1, pthread_t thread2)
91{
92 return thread1 == thread2;
93}
94
95int
96pthread_detach (pthread_t thread)
97{
98 return glwthread_thread_detach (thread);
99}
100
101int
102pthread_join (pthread_t thread, void **valuep)
103{
104 return glwthread_thread_join (thread, valuep);
105}
106
107void
108pthread_exit (void *value)
109{
110 glwthread_thread_exit (value);
111}
112
113#elif HAVE_PTHREAD_H
114/* Provide workarounds for POSIX threads. */
115
116# if PTHREAD_CREATE_IS_INLINE
117int
118pthread_create (pthread_t *threadp, const pthread_attr_t *attr,
119 pthread_main_function_t mainfunc, void *arg)
120# undef pthread_create
121{
122 return pthread_create (threadp, attr, mainfunc, arg);
123}
124
125int
126pthread_attr_init (pthread_attr_t *attr)
127# undef pthread_attr_init
128{
129 return pthread_attr_init (attr);
130}
131
132# endif
133
134#else
135/* Provide a dummy implementation for single-threaded applications. */
136
137int
138pthread_create (pthread_t *threadp, const pthread_attr_t *attr,
139 pthread_main_function_t mainfunc, void *arg)
140{
141 /* The maximum number of threads is reached. Do not create a thread. */
142 return EAGAIN;
143}
144
145pthread_t
146pthread_self (void)
147{
148 return 42;
149}
150
151int
152pthread_equal (pthread_t thread1, pthread_t thread2)
153{
154 return thread1 == thread2;
155}
156
157int
158pthread_detach (pthread_t thread)
159{
160 /* There are no joinable threads. */
161 return EINVAL;
162}
163
164int
165pthread_join (pthread_t thread, void **valuep)
166{
167 /* There are no joinable threads. */
168 return EINVAL;
169}
170
171void
172pthread_exit (void *value)
173{
174 /* There is just one thread, so the process exits. */
175 exit (0);
176}
177
178#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette