aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_threads.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/mhd_threads.c')
-rw-r--r--src/microhttpd/mhd_threads.c76
1 files changed, 58 insertions, 18 deletions
diff --git a/src/microhttpd/mhd_threads.c b/src/microhttpd/mhd_threads.c
index 13cccf4c..057bf376 100644
--- a/src/microhttpd/mhd_threads.c
+++ b/src/microhttpd/mhd_threads.c
@@ -38,6 +38,7 @@
38#endif /* HAVE_PTHREAD_NP_H */ 38#endif /* HAVE_PTHREAD_NP_H */
39#endif /* MHD_USE_THREAD_NAME_ */ 39#endif /* MHD_USE_THREAD_NAME_ */
40#include <errno.h> 40#include <errno.h>
41#include "mhd_assert.h"
41 42
42 43
43#ifndef MHD_USE_THREAD_NAME_ 44#ifndef MHD_USE_THREAD_NAME_
@@ -66,7 +67,7 @@
66 * @return non-zero on success, zero otherwise 67 * @return non-zero on success, zero otherwise
67 */ 68 */
68static int 69static int
69MHD_set_thread_name_ (const MHD_thread_ID_ thread_id, 70MHD_set_thread_name_ (const MHD_thread_ID_native_ thread_id,
70 const char *thread_name) 71 const char *thread_name)
71{ 72{
72 if (NULL == thread_name) 73 if (NULL == thread_name)
@@ -122,7 +123,7 @@ MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
122 * @return non-zero on success, zero otherwise 123 * @return non-zero on success, zero otherwise
123 */ 124 */
124static int 125static int
125MHD_set_thread_name_ (const MHD_thread_ID_ thread_id, 126MHD_set_thread_name_ (const MHD_thread_ID_native_ thread_id,
126 const char *thread_name) 127 const char *thread_name)
127{ 128{
128 static const DWORD VC_SETNAME_EXC = 0x406D1388; 129 static const DWORD VC_SETNAME_EXC = 0x406D1388;
@@ -164,7 +165,7 @@ MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
164 * @return non-zero on success, zero otherwise 165 * @return non-zero on success, zero otherwise
165 */ 166 */
166#define MHD_set_cur_thread_name_(n) \ 167#define MHD_set_cur_thread_name_(n) \
167 MHD_set_thread_name_ ((MHD_thread_ID_) -1,(n)) 168 MHD_set_thread_name_ ((MHD_thread_ID_native_) -1,(n))
168#endif /* _MSC_FULL_VER */ 169#endif /* _MSC_FULL_VER */
169#endif /* MHD_USE_W32_THREADS */ 170#endif /* MHD_USE_W32_THREADS */
170 171
@@ -176,20 +177,29 @@ MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
176 * 177 *
177 * If thread is created, thread handle must be freed by MHD_join_thread_(). 178 * If thread is created, thread handle must be freed by MHD_join_thread_().
178 * 179 *
179 * @param thread handle to initialize 180 * @param handle_id handle to initialise
180 * @param stack_size size of stack for new thread, 0 for default 181 * @param stack_size size of stack for new thread, 0 for default
181 * @param start_routine main function of thread 182 * @param start_routine main function of thread
182 * @param arg argument for start_routine 183 * @param arg argument for start_routine
183 * @return non-zero on success; zero otherwise (with errno set) 184 * @return non-zero on success; zero otherwise (with errno set)
184 */ 185 */
185int 186int
186MHD_create_thread_ (MHD_thread_handle_ID_ *thread, 187MHD_create_thread_ (MHD_thread_handle_ID_ *handle_id,
187 size_t stack_size, 188 size_t stack_size,
188 MHD_THREAD_START_ROUTINE_ start_routine, 189 MHD_THREAD_START_ROUTINE_ start_routine,
189 void *arg) 190 void *arg)
190{ 191{
191#if defined(MHD_USE_POSIX_THREADS) 192#if defined(MHD_USE_POSIX_THREADS)
192 int res; 193 int res;
194#if defined(MHD_thread_handle_ID_get_native_handle_ptr_)
195 pthread_t *const new_tid_ptr =
196 MHD_thread_handle_ID_get_native_handle_ptr_ (handle_id);
197#else /* ! MHD_thread_handle_ID_get_native_handle_ptr_ */
198 pthread_t new_tid;
199 pthread_t *const new_tid_ptr = &new_tid;
200#endif /* ! MHD_thread_handle_ID_get_native_handle_ptr_ */
201
202 mhd_assert (! MHD_thread_handle_ID_is_valid_handle_ (*handle_id));
193 203
194 if (0 != stack_size) 204 if (0 != stack_size)
195 { 205 {
@@ -200,7 +210,7 @@ MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
200 res = pthread_attr_setstacksize (&attr, 210 res = pthread_attr_setstacksize (&attr,
201 stack_size); 211 stack_size);
202 if (0 == res) 212 if (0 == res)
203 res = pthread_create (&(thread->handle), 213 res = pthread_create (new_tid_ptr,
204 &attr, 214 &attr,
205 start_routine, 215 start_routine,
206 arg); 216 arg);
@@ -208,18 +218,28 @@ MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
208 } 218 }
209 } 219 }
210 else 220 else
211 res = pthread_create (&(thread->handle), 221 res = pthread_create (new_tid_ptr,
212 NULL, 222 NULL,
213 start_routine, 223 start_routine,
214 arg); 224 arg);
215 225
216 if (0 != res) 226 if (0 != res)
227 {
217 errno = res; 228 errno = res;
229 MHD_thread_handle_ID_set_invalid_ (handle_id);
230 }
231#if ! defined(MHD_thread_handle_ID_get_native_handle_ptr_)
232 else
233 MHD_thread_handle_ID_set_native_handle_ (handle_id, new_tid);
234#endif /* ! MHD_thread_handle_ID_set_current_thread_ID_ */
218 235
219 return ! res; 236 return ! res;
220#elif defined(MHD_USE_W32_THREADS) 237#elif defined(MHD_USE_W32_THREADS)
221 uintptr_t thr_handle; 238 uintptr_t thr_handle;
222#if SIZEOF_SIZE_T != SIZEOF_UNSIGNED_INT 239#if SIZEOF_SIZE_T != SIZEOF_UNSIGNED_INT
240
241 mhd_assert (! MHD_thread_handle_ID_is_valid_handle_ (*handle_id));
242
223 if (stack_size > UINT_MAX) 243 if (stack_size > UINT_MAX)
224 { 244 {
225 errno = EINVAL; 245 errno = EINVAL;
@@ -232,11 +252,13 @@ MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
232 arg, 252 arg,
233 0, 253 0,
234 NULL); 254 NULL);
235 thread->handle = (MHD_thread_handle_) thr_handle; 255 if ((MHD_thread_handle_native_) 0 == (MHD_thread_handle_native_) thr_handle)
236
237 if ((MHD_thread_handle_) 0 == thread->handle)
238 return 0; 256 return 0;
239 257
258 MHD_thread_handle_ID_set_native_handle_ (handle_id, \
259 (MHD_thread_handle_native_) \
260 thr_handle);
261
240 return ! 0; 262 return ! 0;
241#endif 263#endif
242} 264}
@@ -291,7 +313,7 @@ named_thread_starter (void *data)
291/** 313/**
292 * Create a named thread and set the attributes according to our options. 314 * Create a named thread and set the attributes according to our options.
293 * 315 *
294 * @param thread handle to initialize 316 * @param handle_id handle to initialise
295 * @param thread_name name for new thread 317 * @param thread_name name for new thread
296 * @param stack_size size of stack for new thread, 0 for default 318 * @param stack_size size of stack for new thread, 0 for default
297 * @param start_routine main function of thread 319 * @param start_routine main function of thread
@@ -299,7 +321,7 @@ named_thread_starter (void *data)
299 * @return non-zero on success; zero otherwise (with errno set) 321 * @return non-zero on success; zero otherwise (with errno set)
300 */ 322 */
301int 323int
302MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread, 324MHD_create_named_thread_ (MHD_thread_handle_ID_ *handle_id,
303 const char *thread_name, 325 const char *thread_name,
304 size_t stack_size, 326 size_t stack_size,
305 MHD_THREAD_START_ROUTINE_ start_routine, 327 MHD_THREAD_START_ROUTINE_ start_routine,
@@ -308,15 +330,22 @@ MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
308#if defined(MHD_USE_THREAD_ATTR_SETNAME) 330#if defined(MHD_USE_THREAD_ATTR_SETNAME)
309 int res; 331 int res;
310 pthread_attr_t attr; 332 pthread_attr_t attr;
333#if defined(MHD_thread_handle_ID_get_native_handle_ptr_)
334 pthread_t *const new_tid_ptr =
335 MHD_thread_handle_ID_get_native_handle_ptr_ (handle_id);
336#else /* ! MHD_thread_handle_ID_get_native_handle_ptr_ */
337 pthread_t new_tid;
338 pthread_t *const new_tid_ptr = &new_tid;
339#endif /* ! MHD_thread_handle_ID_get_native_handle_ptr_ */
311 340
312 res = pthread_attr_init (&attr); 341 res = pthread_attr_init (&attr);
313 if (0 == res) 342 if (0 == res)
314 { 343 {
315#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) 344#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD)
316 /* NetBSD use 3 arguments: second argument is string in printf-like format, 345 /* NetBSD uses 3 arguments: second argument is string in printf-like format,
317 * third argument is single argument for printf; 346 * third argument is single argument for printf;
318 * OSF1 use 3 arguments too, but last one always must be zero (NULL). 347 * OSF1 uses 3 arguments too, but last one always must be zero (NULL).
319 * MHD doesn't use '%' in thread names, so both form are used in same way. 348 * MHD doesn't use '%' in thread names, so both forms are used in same way.
320 */ 349 */
321 res = pthread_attr_setname_np (&attr, 350 res = pthread_attr_setname_np (&attr,
322 thread_name, 351 thread_name,
@@ -331,14 +360,21 @@ MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
331 res = pthread_attr_setstacksize (&attr, 360 res = pthread_attr_setstacksize (&attr,
332 stack_size); 361 stack_size);
333 if (0 == res) 362 if (0 == res)
334 res = pthread_create (&(thread->handle), 363 res = pthread_create (new_tid_ptr,
335 &attr, 364 &attr,
336 start_routine, 365 start_routine,
337 arg); 366 arg);
338 pthread_attr_destroy (&attr); 367 pthread_attr_destroy (&attr);
339 } 368 }
340 if (0 != res) 369 if (0 != res)
370 {
341 errno = res; 371 errno = res;
372 MHD_thread_handle_ID_set_invalid_ (handle_id);
373 }
374#if ! defined(MHD_thread_handle_ID_get_native_handle_ptr_)
375 else
376 MHD_thread_handle_ID_set_native_handle_ (handle_id, new_tid);
377#endif /* ! MHD_thread_handle_ID_set_current_thread_ID_ */
342 378
343 return ! res; 379 return ! res;
344#else /* ! MHD_USE_THREAD_ATTR_SETNAME */ 380#else /* ! MHD_USE_THREAD_ATTR_SETNAME */
@@ -361,12 +397,16 @@ MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
361 /* Set thread name in thread itself to avoid problems with 397 /* Set thread name in thread itself to avoid problems with
362 * threads which terminated before name is set in other thread. 398 * threads which terminated before name is set in other thread.
363 */ 399 */
364 if (! MHD_create_thread_ (thread, 400 if (! MHD_create_thread_ (handle_id,
365 stack_size, 401 stack_size,
366 &named_thread_starter, 402 &named_thread_starter,
367 (void *) param)) 403 (void *) param))
368 { 404 {
405 int err_num;
406
407 err_num = errno;
369 free (param); 408 free (param);
409 errno = err_num;
370 return 0; 410 return 0;
371 } 411 }
372 412