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.c165
1 files changed, 85 insertions, 80 deletions
diff --git a/src/microhttpd/mhd_threads.c b/src/microhttpd/mhd_threads.c
index 6578e4b1..6be4cb44 100644
--- a/src/microhttpd/mhd_threads.c
+++ b/src/microhttpd/mhd_threads.c
@@ -46,12 +46,14 @@
46#else /* MHD_USE_THREAD_NAME_ */ 46#else /* MHD_USE_THREAD_NAME_ */
47 47
48#if defined(MHD_USE_POSIX_THREADS) 48#if defined(MHD_USE_POSIX_THREADS)
49#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) || defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI) 49#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) || \
50 defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
50# define MHD_USE_THREAD_ATTR_SETNAME 1 51# define MHD_USE_THREAD_ATTR_SETNAME 1
51#endif /* HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD || HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI */ 52#endif /* HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD || HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI */
52 53
53#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) \ 54#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || \
54 || defined(HAVE_PTHREAD_SETNAME_NP_NETBSD) 55 defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) \
56 || defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
55 57
56/** 58/**
57 * Set thread name 59 * Set thread name
@@ -61,25 +63,25 @@
61 * @return non-zero on success, zero otherwise 63 * @return non-zero on success, zero otherwise
62 */ 64 */
63static int 65static int
64MHD_set_thread_name_(const MHD_thread_ID_ thread_id, 66MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
65 const char *thread_name) 67 const char *thread_name)
66{ 68{
67 if (NULL == thread_name) 69 if (NULL == thread_name)
68 return 0; 70 return 0;
69 71
70#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) 72#if defined(HAVE_PTHREAD_SETNAME_NP_GNU)
71 return !pthread_setname_np (thread_id, thread_name); 73 return ! pthread_setname_np (thread_id, thread_name);
72#elif defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) 74#elif defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD)
73 /* FreeBSD and OpenBSD use different name and void return type */ 75 /* FreeBSD and OpenBSD use different name and void return type */
74 pthread_set_name_np (thread_id, thread_name); 76 pthread_set_name_np (thread_id, thread_name);
75 return !0; 77 return ! 0;
76#elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD) 78#elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
77 /* NetBSD use 3 arguments: second argument is string in printf-like format, 79 /* NetBSD use 3 arguments: second argument is string in printf-like format,
78 * third argument is single argument for printf; 80 * third argument is single argument for printf;
79 * OSF1 use 3 arguments too, but last one always must be zero (NULL). 81 * OSF1 use 3 arguments too, but last one always must be zero (NULL).
80 * MHD doesn't use '%' in thread names, so both form are used in same way. 82 * MHD doesn't use '%' in thread names, so both form are used in same way.
81 */ 83 */
82 return !pthread_setname_np (thread_id, thread_name, 0); 84 return ! pthread_setname_np (thread_id, thread_name, 0);
83#endif /* HAVE_PTHREAD_SETNAME_NP_NETBSD */ 85#endif /* HAVE_PTHREAD_SETNAME_NP_NETBSD */
84} 86}
85 87
@@ -90,10 +92,10 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
90 * @param n name to set 92 * @param n name to set
91 * @return non-zero on success, zero otherwise 93 * @return non-zero on success, zero otherwise
92 */ 94 */
93#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_(pthread_self(),(n)) 95#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (pthread_self (),(n))
94#else /* __QNXNTO__ */ 96#else /* __QNXNTO__ */
95/* Special case for QNX Neutrino - using zero for thread ID sets name faster. */ 97/* Special case for QNX Neutrino - using zero for thread ID sets name faster. */
96#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_(0,(n)) 98#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (0,(n))
97#endif /* __QNXNTO__ */ 99#endif /* __QNXNTO__ */
98#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN) 100#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN)
99 101
@@ -102,7 +104,7 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
102 * @param n name to set 104 * @param n name to set
103 * @return non-zero on success, zero otherwise 105 * @return non-zero on success, zero otherwise
104 */ 106 */
105#define MHD_set_cur_thread_name_(n) (!(pthread_setname_np((n)))) 107#define MHD_set_cur_thread_name_(n) (! (pthread_setname_np ((n))))
106#endif /* HAVE_PTHREAD_SETNAME_NP_DARWIN */ 108#endif /* HAVE_PTHREAD_SETNAME_NP_DARWIN */
107 109
108#elif defined(MHD_USE_W32_THREADS) 110#elif defined(MHD_USE_W32_THREADS)
@@ -117,8 +119,8 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
117 * @return non-zero on success, zero otherwise 119 * @return non-zero on success, zero otherwise
118 */ 120 */
119static int 121static int
120MHD_set_thread_name_(const MHD_thread_ID_ thread_id, 122MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
121 const char *thread_name) 123 const char *thread_name)
122{ 124{
123 static const DWORD VC_SETNAME_EXC = 0x406D1388; 125 static const DWORD VC_SETNAME_EXC = 0x406D1388;
124#pragma pack(push,8) 126#pragma pack(push,8)
@@ -149,7 +151,7 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
149 __except (EXCEPTION_EXECUTE_HANDLER) 151 __except (EXCEPTION_EXECUTE_HANDLER)
150 {} 152 {}
151 153
152 return !0; 154 return ! 0;
153} 155}
154 156
155 157
@@ -158,7 +160,7 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id,
158 * @param n name to set 160 * @param n name to set
159 * @return non-zero on success, zero otherwise 161 * @return non-zero on success, zero otherwise
160 */ 162 */
161#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_(-1,(n)) 163#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (-1,(n))
162#endif /* _MSC_FULL_VER */ 164#endif /* _MSC_FULL_VER */
163#endif /* MHD_USE_W32_THREADS */ 165#endif /* MHD_USE_W32_THREADS */
164 166
@@ -184,21 +186,21 @@ MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
184 int res; 186 int res;
185 187
186 if (0 != stack_size) 188 if (0 != stack_size)
189 {
190 pthread_attr_t attr;
191 res = pthread_attr_init (&attr);
192 if (0 == res)
187 { 193 {
188 pthread_attr_t attr; 194 res = pthread_attr_setstacksize (&attr,
189 res = pthread_attr_init (&attr); 195 stack_size);
190 if (0 == res) 196 if (0 == res)
191 { 197 res = pthread_create (&(thread->handle),
192 res = pthread_attr_setstacksize (&attr, 198 &attr,
193 stack_size); 199 start_routine,
194 if (0 == res) 200 arg);
195 res = pthread_create (&(thread->handle), 201 pthread_attr_destroy (&attr);
196 &attr,
197 start_routine,
198 arg);
199 pthread_attr_destroy (&attr);
200 }
201 } 202 }
203 }
202 else 204 else
203 res = pthread_create (&(thread->handle), 205 res = pthread_create (&(thread->handle),
204 NULL, 206 NULL,
@@ -208,28 +210,28 @@ MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
208 if (0 != res) 210 if (0 != res)
209 errno = res; 211 errno = res;
210 212
211 return !res; 213 return ! res;
212#elif defined(MHD_USE_W32_THREADS) 214#elif defined(MHD_USE_W32_THREADS)
213#if SIZE_MAX != UINT_MAX 215#if SIZE_MAX != UINT_MAX
214 if (stack_size > UINT_MAX) 216 if (stack_size > UINT_MAX)
215 { 217 {
216 errno = EINVAL; 218 errno = EINVAL;
217 return 0; 219 return 0;
218 } 220 }
219#endif /* SIZE_MAX != UINT_MAX */ 221#endif /* SIZE_MAX != UINT_MAX */
220 222
221 thread->handle = (MHD_thread_handle_) 223 thread->handle = (MHD_thread_handle_)
222 _beginthreadex (NULL, 224 _beginthreadex (NULL,
223 (unsigned int) stack_size, 225 (unsigned int) stack_size,
224 start_routine, 226 start_routine,
225 arg, 227 arg,
226 0, 228 0,
227 NULL); 229 NULL);
228 230
229 if ((MHD_thread_handle_)-1 == thread->handle) 231 if ((MHD_thread_handle_) - 1 == thread->handle)
230 return 0; 232 return 0;
231 233
232 return !0; 234 return ! 0;
233#endif 235#endif
234} 236}
235 237
@@ -258,21 +260,21 @@ struct MHD_named_helper_param_
258static MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_ 260static MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
259named_thread_starter (void *data) 261named_thread_starter (void *data)
260{ 262{
261 struct MHD_named_helper_param_ * const param = 263 struct MHD_named_helper_param_ *const param =
262 (struct MHD_named_helper_param_ *) data; 264 (struct MHD_named_helper_param_ *) data;
263 void * arg; 265 void *arg;
264 MHD_THREAD_START_ROUTINE_ thr_func; 266 MHD_THREAD_START_ROUTINE_ thr_func;
265 267
266 if (NULL == data) 268 if (NULL == data)
267 return (MHD_THRD_RTRN_TYPE_)0; 269 return (MHD_THRD_RTRN_TYPE_) 0;
268 270
269 MHD_set_cur_thread_name_ (param->name); 271 MHD_set_cur_thread_name_ (param->name);
270 272
271 arg = param->arg; 273 arg = param->arg;
272 thr_func = param->start_routine; 274 thr_func = param->start_routine;
273 free(data); 275 free (data);
274 276
275 return thr_func(arg); 277 return thr_func (arg);
276} 278}
277#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */ 279#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */
278 280
@@ -289,7 +291,7 @@ named_thread_starter (void *data)
289 */ 291 */
290int 292int
291MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread, 293MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
292 const char* thread_name, 294 const char*thread_name,
293 size_t stack_size, 295 size_t stack_size,
294 MHD_THREAD_START_ROUTINE_ start_routine, 296 MHD_THREAD_START_ROUTINE_ start_routine,
295 void *arg) 297 void *arg)
@@ -300,41 +302,44 @@ MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
300 302
301 res = pthread_attr_init (&attr); 303 res = pthread_attr_init (&attr);
302 if (0 == res) 304 if (0 == res)
303 { 305 {
304#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) 306#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD)
305 /* NetBSD use 3 arguments: second argument is string in printf-like format, 307 /* NetBSD use 3 arguments: second argument is string in printf-like format,
306 * third argument is single argument for printf; 308 * third argument is single argument for printf;
307 * OSF1 use 3 arguments too, but last one always must be zero (NULL). 309 * OSF1 use 3 arguments too, but last one always must be zero (NULL).
308 * MHD doesn't use '%' in thread names, so both form are used in same way. 310 * MHD doesn't use '%' in thread names, so both form are used in same way.
309 */ 311 */
310 res = pthread_attr_setname_np (&attr, thread_name, 0); 312 res = pthread_attr_setname_np (&attr,
313 thread_name,
314 0);
311#elif defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI) 315#elif defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
312 res = pthread_attr_setname_np (&attr, thread_name); 316 res = pthread_attr_setname_np (&attr,
317 thread_name);
313#else 318#else
314#error No pthread_attr_setname_np() function. 319#error No pthread_attr_setname_np() function.
315#endif 320#endif
316 if (res == 0 && 0 != stack_size) 321 if ((res == 0) &&(0 != stack_size) )
317 res = pthread_attr_setstacksize (&attr, 322 res = pthread_attr_setstacksize (&attr,
318 stack_size); 323 stack_size);
319 if (0 == res) 324 if (0 == res)
320 res = pthread_create (&(thread->handle), 325 res = pthread_create (&(thread->handle),
321 &attr, 326 &attr,
322 start_routine, 327 start_routine,
323 arg); 328 arg);
324 pthread_attr_destroy (&attr); 329 pthread_attr_destroy (&attr);
325 } 330 }
326 if (0 != res) 331 if (0 != res)
327 errno = res; 332 errno = res;
328 333
329 return !res; 334 return ! res;
330#else /* ! MHD_USE_THREAD_ATTR_SETNAME */ 335#else /* ! MHD_USE_THREAD_ATTR_SETNAME */
331 struct MHD_named_helper_param_ *param; 336 struct MHD_named_helper_param_ *param;
332 337
333 if (NULL == thread_name) 338 if (NULL == thread_name)
334 { 339 {
335 errno = EINVAL; 340 errno = EINVAL;
336 return 0; 341 return 0;
337 } 342 }
338 343
339 param = malloc (sizeof (struct MHD_named_helper_param_)); 344 param = malloc (sizeof (struct MHD_named_helper_param_));
340 if (NULL == param) 345 if (NULL == param)
@@ -347,16 +352,16 @@ MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
347 /* Set thread name in thread itself to avoid problems with 352 /* Set thread name in thread itself to avoid problems with
348 * threads which terminated before name is set in other thread. 353 * threads which terminated before name is set in other thread.
349 */ 354 */
350 if (! MHD_create_thread_(thread, 355 if (! MHD_create_thread_ (thread,
351 stack_size, 356 stack_size,
352 &named_thread_starter, 357 &named_thread_starter,
353 (void*)param)) 358 (void*) param))
354 { 359 {
355 free (param); 360 free (param);
356 return 0; 361 return 0;
357 } 362 }
358 363
359 return !0; 364 return ! 0;
360#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */ 365#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */
361} 366}
362 367