GNU libmicrohttpd 0.9.77
Loading...
Searching...
No Matches
mhd_threads.c
Go to the documentation of this file.
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2016 Karlson2k (Evgeny Grin)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19*/
20
27#include "mhd_threads.h"
28#ifdef MHD_USE_W32_THREADS
29#include "mhd_limits.h"
30#include <process.h>
31#endif
32#ifdef MHD_USE_THREAD_NAME_
33#ifdef HAVE_STDLIB_H
34#include <stdlib.h>
35#endif /* HAVE_STDLIB_H */
36#ifdef HAVE_PTHREAD_NP_H
37#include <pthread_np.h>
38#endif /* HAVE_PTHREAD_NP_H */
39#endif /* MHD_USE_THREAD_NAME_ */
40#include <errno.h>
41
42
43#ifndef MHD_USE_THREAD_NAME_
44
45#define MHD_set_thread_name_(t, n) (void)
46#define MHD_set_cur_thread_name_(n) (void)
47
48#else /* MHD_USE_THREAD_NAME_ */
49
50#if defined(MHD_USE_POSIX_THREADS)
51#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) || \
52 defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
53# define MHD_USE_THREAD_ATTR_SETNAME 1
54#endif /* HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD || \
55 HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI */
56
57#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || \
58 defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) \
59 || defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
60
68static int
69MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
70 const char *thread_name)
71{
72 if (NULL == thread_name)
73 return 0;
74
75#if defined(HAVE_PTHREAD_SETNAME_NP_GNU)
76 return ! pthread_setname_np (thread_id, thread_name);
77#elif defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD)
78 /* FreeBSD and OpenBSD use different function name and void return type */
79 pthread_set_name_np (thread_id, thread_name);
80 return ! 0;
81#elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
82 /* NetBSD use 3 arguments: second argument is string in printf-like format,
83 * third argument is a single argument for printf();
84 * OSF1 use 3 arguments too, but last one always must be zero (NULL).
85 * MHD doesn't use '%' in thread names, so both form are used in same way.
86 */
87 return ! pthread_setname_np (thread_id, thread_name, 0);
88#endif /* HAVE_PTHREAD_SETNAME_NP_NETBSD */
89}
90
91
92#ifndef __QNXNTO__
98#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (pthread_self (),(n))
99#else /* __QNXNTO__ */
100/* Special case for QNX Neutrino - using zero for thread ID sets name faster. */
101#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (0,(n))
102#endif /* __QNXNTO__ */
103#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN)
104
110#define MHD_set_cur_thread_name_(n) (! (pthread_setname_np ((n))))
111#endif /* HAVE_PTHREAD_SETNAME_NP_DARWIN */
112
113#elif defined(MHD_USE_W32_THREADS)
114#ifndef _MSC_FULL_VER
115/* Thread name available only for VC-compiler */
116#else /* _MSC_FULL_VER */
124static int
125MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
126 const char *thread_name)
127{
128 static const DWORD VC_SETNAME_EXC = 0x406D1388;
129#pragma pack(push,8)
130 struct thread_info_struct
131 {
132 DWORD type; /* Must be 0x1000. */
133 LPCSTR name; /* Pointer to name (in user address space). */
134 DWORD ID; /* Thread ID (-1 = caller thread). */
135 DWORD flags; /* Reserved for future use, must be zero. */
136 } thread_info;
137#pragma pack(pop)
138
139 if (NULL == thread_name)
140 return 0;
141
142 thread_info.type = 0x1000;
143 thread_info.name = thread_name;
144 thread_info.ID = thread_id;
145 thread_info.flags = 0;
146
147 __try
148 { /* This exception is intercepted by debugger */
149 RaiseException (VC_SETNAME_EXC,
150 0,
151 sizeof (thread_info) / sizeof(ULONG_PTR),
152 (ULONG_PTR *) &thread_info);
153 }
154 __except (EXCEPTION_EXECUTE_HANDLER)
155 {}
156
157 return ! 0;
158}
159
160
166#define MHD_set_cur_thread_name_(n) \
167 MHD_set_thread_name_ ((MHD_thread_ID_) -1,(n))
168#endif /* _MSC_FULL_VER */
169#endif /* MHD_USE_W32_THREADS */
170
171#endif /* MHD_USE_THREAD_NAME_ */
172
173
183int
184MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
185 size_t stack_size,
186 MHD_THREAD_START_ROUTINE_ start_routine,
187 void *arg)
188{
189#if defined(MHD_USE_POSIX_THREADS)
190 int res;
191
192 if (0 != stack_size)
193 {
194 pthread_attr_t attr;
195 res = pthread_attr_init (&attr);
196 if (0 == res)
197 {
198 res = pthread_attr_setstacksize (&attr,
199 stack_size);
200 if (0 == res)
201 res = pthread_create (&(thread->handle),
202 &attr,
203 start_routine,
204 arg);
205 pthread_attr_destroy (&attr);
206 }
207 }
208 else
209 res = pthread_create (&(thread->handle),
210 NULL,
211 start_routine,
212 arg);
213
214 if (0 != res)
215 errno = res;
216
217 return ! res;
218#elif defined(MHD_USE_W32_THREADS)
219#if SIZEOF_SIZE_T != SIZEOF_UNSIGNED_INT
220 if (stack_size > UINT_MAX)
221 {
222 errno = EINVAL;
223 return 0;
224 }
225#endif /* SIZEOF_SIZE_T != SIZEOF_UNSIGNED_INT */
226
227 thread->handle = (MHD_thread_handle_)
228 _beginthreadex (NULL,
229 (unsigned int) stack_size,
230 start_routine,
231 arg,
232 0,
233 NULL);
234
235 if ((MHD_thread_handle_) - 1 == thread->handle)
236 return 0;
237
238 return ! 0;
239#endif
240}
241
242
243#ifdef MHD_USE_THREAD_NAME_
244
245#ifndef MHD_USE_THREAD_ATTR_SETNAME
246struct MHD_named_helper_param_
247{
251 MHD_THREAD_START_ROUTINE_ start_routine;
252
256 void *arg;
257
261 const char *name;
262};
263
264
265static MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
266named_thread_starter (void *data)
267{
268 struct MHD_named_helper_param_ *const param =
269 (struct MHD_named_helper_param_ *) data;
270 void *arg;
272
273 if (NULL == data)
274 return (MHD_THRD_RTRN_TYPE_) 0;
275
276 MHD_set_cur_thread_name_ (param->name);
277
278 arg = param->arg;
279 thr_func = param->start_routine;
280 free (data);
281
282 return thr_func (arg);
283}
284
285
286#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */
287
288
299int
300MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
301 const char *thread_name,
302 size_t stack_size,
303 MHD_THREAD_START_ROUTINE_ start_routine,
304 void *arg)
305{
306#if defined(MHD_USE_THREAD_ATTR_SETNAME)
307 int res;
308 pthread_attr_t attr;
309
310 res = pthread_attr_init (&attr);
311 if (0 == res)
312 {
313#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD)
314 /* NetBSD use 3 arguments: second argument is string in printf-like format,
315 * third argument is single argument for printf;
316 * OSF1 use 3 arguments too, but last one always must be zero (NULL).
317 * MHD doesn't use '%' in thread names, so both form are used in same way.
318 */
319 res = pthread_attr_setname_np (&attr,
320 thread_name,
321 0);
322#elif defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
323 res = pthread_attr_setname_np (&attr,
324 thread_name);
325#else
326#error No pthread_attr_setname_np() function.
327#endif
328 if ((res == 0) && (0 != stack_size) )
329 res = pthread_attr_setstacksize (&attr,
330 stack_size);
331 if (0 == res)
332 res = pthread_create (&(thread->handle),
333 &attr,
334 start_routine,
335 arg);
336 pthread_attr_destroy (&attr);
337 }
338 if (0 != res)
339 errno = res;
340
341 return ! res;
342#else /* ! MHD_USE_THREAD_ATTR_SETNAME */
343 struct MHD_named_helper_param_ *param;
344
345 if (NULL == thread_name)
346 {
347 errno = EINVAL;
348 return 0;
349 }
350
351 param = malloc (sizeof (struct MHD_named_helper_param_));
352 if (NULL == param)
353 return 0;
354
355 param->start_routine = start_routine;
356 param->arg = arg;
357 param->name = thread_name;
358
359 /* Set thread name in thread itself to avoid problems with
360 * threads which terminated before name is set in other thread.
361 */
362 if (! MHD_create_thread_ (thread,
363 stack_size,
364 &named_thread_starter,
365 (void *) param))
366 {
367 free (param);
368 return 0;
369 }
370
371 return ! 0;
372#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */
373}
374
375
376#endif /* MHD_USE_THREAD_NAME_ */
#define UINT_MAX
Definition mhd_limits.h:45
int MHD_create_thread_(MHD_thread_handle_ID_ *thread, size_t stack_size, MHD_THREAD_START_ROUTINE_ start_routine, void *arg)
#define MHD_set_cur_thread_name_(n)
Definition mhd_threads.c:44
#define MHD_set_thread_name_(t, n)
Definition mhd_threads.c:43
MHD_THRD_RTRN_TYPE_(MHD_THRD_CALL_SPEC_ * MHD_THREAD_START_ROUTINE_)(void *cls)
#define MHD_create_named_thread_(t, n, s, r, a)
#define NULL
Header for platform-independent threads abstraction.
void * data