aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_threads.c
blob: 3a63cff118f79e8f0086c7c72beca29353aa7104 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
  This file is part of libmicrohttpd
  Copyright (C) 2016 Karlson2k (Evgeny Grin)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

/**
 * @file microhttpd/mhd_threads.c
 * @brief  Implementation for thread functions
 * @author Karlson2k (Evgeny Grin)
 */

#include "mhd_threads.h"
#ifdef MHD_USE_W32_THREADS
#include "mhd_limits.h"
#include <process.h>
#endif
#ifdef MHD_USE_THREAD_NAME_
#include <stdlib.h>
#ifdef HAVE_PTHREAD_NP_H
#include <pthread_np.h>
#endif /* HAVE_PTHREAD_NP_H */
#endif /* MHD_USE_THREAD_NAME_ */
#include <errno.h>


#ifndef MHD_USE_THREAD_NAME_

#define MHD_set_thread_name_(t, n) (void)
#define MHD_set_cur_thread_name_(n) (void)

#else  /* MHD_USE_THREAD_NAME_ */

#if defined(MHD_USE_POSIX_THREADS)
#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) || \
  defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
#  define MHD_USE_THREAD_ATTR_SETNAME 1
#endif \
  /* HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD || HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI */

#if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || \
  defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) \
  || defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)

/**
 * Set thread name
 *
 * @param thread_id ID of thread
 * @param thread_name name to set
 * @return non-zero on success, zero otherwise
 */
static int
MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
                      const char *thread_name)
{
  if (NULL == thread_name)
    return 0;

#if defined(HAVE_PTHREAD_SETNAME_NP_GNU)
  return ! pthread_setname_np (thread_id, thread_name);
#elif defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD)
  /* FreeBSD and OpenBSD use different name and void return type */
  pthread_set_name_np (thread_id, thread_name);
  return ! 0;
#elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
  /* NetBSD use 3 arguments: second argument is string in printf-like format,
   *                         third argument is single argument for printf;
   * OSF1 use 3 arguments too, but last one always must be zero (NULL).
   * MHD doesn't use '%' in thread names, so both form are used in same way.
   */
  return ! pthread_setname_np (thread_id, thread_name, 0);
#endif /* HAVE_PTHREAD_SETNAME_NP_NETBSD */
}


#ifndef __QNXNTO__
/**
 * Set current thread name
 * @param n name to set
 * @return non-zero on success, zero otherwise
 */
#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (pthread_self (),(n))
#else  /* __QNXNTO__ */
/* Special case for QNX Neutrino - using zero for thread ID sets name faster. */
#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (0,(n))
#endif /* __QNXNTO__ */
#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN)

/**
 * Set current thread name
 * @param n name to set
 * @return non-zero on success, zero otherwise
 */
#define MHD_set_cur_thread_name_(n) (! (pthread_setname_np ((n))))
#endif /* HAVE_PTHREAD_SETNAME_NP_DARWIN */

#elif defined(MHD_USE_W32_THREADS)
#ifndef _MSC_FULL_VER
/* Thread name available only for VC-compiler */
#else  /* _MSC_FULL_VER */
/**
 * Set thread name
 *
 * @param thread_id ID of thread, -1 for current thread
 * @param thread_name name to set
 * @return non-zero on success, zero otherwise
 */
static int
MHD_set_thread_name_ (const MHD_thread_ID_ thread_id,
                      const char *thread_name)
{
  static const DWORD VC_SETNAME_EXC = 0x406D1388;
#pragma pack(push,8)
  struct thread_info_struct
  {
    DWORD type;   /* Must be 0x1000. */
    LPCSTR name;  /* Pointer to name (in user address space). */
    DWORD ID;     /* Thread ID (-1 = caller thread). */
    DWORD flags;  /* Reserved for future use, must be zero. */
  } thread_info;
#pragma pack(pop)

  if (NULL == thread_name)
    return 0;

  thread_info.type  = 0x1000;
  thread_info.name  = thread_name;
  thread_info.ID    = thread_id;
  thread_info.flags = 0;

  __try
  { /* This exception is intercepted by debugger */
    RaiseException (VC_SETNAME_EXC,
                    0,
                    sizeof (thread_info) / sizeof(ULONG_PTR),
                    (ULONG_PTR *) &thread_info);
  }
  __except (EXCEPTION_EXECUTE_HANDLER)
  {}

  return ! 0;
}


/**
 * Set current thread name
 * @param n name to set
 * @return non-zero on success, zero otherwise
 */
#define MHD_set_cur_thread_name_(n) MHD_set_thread_name_ (-1,(n))
#endif /* _MSC_FULL_VER */
#endif /* MHD_USE_W32_THREADS */

#endif /* MHD_USE_THREAD_NAME_ */


/**
 * Create a thread and set the attributes according to our options.
 *
 * @param thread        handle to initialize
 * @param stack_size    size of stack for new thread, 0 for default
 * @param start_routine main function of thread
 * @param arg argument  for start_routine
 * @return non-zero on success; zero otherwise (with errno set)
 */
int
MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
                    size_t stack_size,
                    MHD_THREAD_START_ROUTINE_ start_routine,
                    void *arg)
{
#if defined(MHD_USE_POSIX_THREADS)
  int res;

  if (0 != stack_size)
  {
    pthread_attr_t attr;
    res = pthread_attr_init (&attr);
    if (0 == res)
    {
      res = pthread_attr_setstacksize (&attr,
                                       stack_size);
      if (0 == res)
        res = pthread_create (&(thread->handle),
                              &attr,
                              start_routine,
                              arg);
      pthread_attr_destroy (&attr);
    }
  }
  else
    res = pthread_create (&(thread->handle),
                          NULL,
                          start_routine,
                          arg);

  if (0 != res)
    errno = res;

  return ! res;
#elif defined(MHD_USE_W32_THREADS)
#if SIZE_MAX != UINT_MAX
  if (stack_size > UINT_MAX)
  {
    errno = EINVAL;
    return 0;
  }
#endif /* SIZE_MAX != UINT_MAX */

  thread->handle = (MHD_thread_handle_)
                   _beginthreadex (NULL,
                                   (unsigned int) stack_size,
                                   start_routine,
                                   arg,
                                   0,
                                   NULL);

  if ((MHD_thread_handle_) - 1 == thread->handle)
    return 0;

  return ! 0;
#endif
}


#ifdef MHD_USE_THREAD_NAME_

#ifndef MHD_USE_THREAD_ATTR_SETNAME
struct MHD_named_helper_param_
{
  /**
   * Real thread start routine
   */
  MHD_THREAD_START_ROUTINE_ start_routine;

  /**
   * Argument for thread start routine
   */
  void *arg;

  /**
   * Name for thread
   */
  const char *name;
};


static MHD_THRD_RTRN_TYPE_ MHD_THRD_CALL_SPEC_
named_thread_starter (void *data)
{
  struct MHD_named_helper_param_ *const param =
    (struct MHD_named_helper_param_ *) data;
  void *arg;
  MHD_THREAD_START_ROUTINE_ thr_func;

  if (NULL == data)
    return (MHD_THRD_RTRN_TYPE_) 0;

  MHD_set_cur_thread_name_ (param->name);

  arg = param->arg;
  thr_func = param->start_routine;
  free (data);

  return thr_func (arg);
}


#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */


/**
 * Create a named thread and set the attributes according to our options.
 *
 * @param thread        handle to initialize
 * @param thread_name   name for new thread
 * @param stack_size    size of stack for new thread, 0 for default
 * @param start_routine main function of thread
 * @param arg argument  for start_routine
 * @return non-zero on success; zero otherwise (with errno set)
 */
int
MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
                          const char*thread_name,
                          size_t stack_size,
                          MHD_THREAD_START_ROUTINE_ start_routine,
                          void *arg)
{
#if defined(MHD_USE_THREAD_ATTR_SETNAME)
  int res;
  pthread_attr_t attr;

  res = pthread_attr_init (&attr);
  if (0 == res)
  {
#if defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD)
    /* NetBSD use 3 arguments: second argument is string in printf-like format,
     *                         third argument is single argument for printf;
     * OSF1 use 3 arguments too, but last one always must be zero (NULL).
     * MHD doesn't use '%' in thread names, so both form are used in same way.
     */
    res = pthread_attr_setname_np (&attr,
                                   thread_name,
                                   0);
#elif defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
    res = pthread_attr_setname_np (&attr,
                                   thread_name);
#else
#error No pthread_attr_setname_np() function.
#endif
    if ((res == 0) && (0 != stack_size) )
      res = pthread_attr_setstacksize (&attr,
                                       stack_size);
    if (0 == res)
      res = pthread_create (&(thread->handle),
                            &attr,
                            start_routine,
                            arg);
    pthread_attr_destroy (&attr);
  }
  if (0 != res)
    errno = res;

  return ! res;
#else  /* ! MHD_USE_THREAD_ATTR_SETNAME */
  struct MHD_named_helper_param_ *param;

  if (NULL == thread_name)
  {
    errno = EINVAL;
    return 0;
  }

  param = malloc (sizeof (struct MHD_named_helper_param_));
  if (NULL == param)
    return 0;

  param->start_routine = start_routine;
  param->arg = arg;
  param->name = thread_name;

  /* Set thread name in thread itself to avoid problems with
   * threads which terminated before name is set in other thread.
   */
  if (! MHD_create_thread_ (thread,
                            stack_size,
                            &named_thread_starter,
                            (void*) param))
  {
    free (param);
    return 0;
  }

  return ! 0;
#endif /* ! MHD_USE_THREAD_ATTR_SETNAME */
}


#endif /* MHD_USE_THREAD_NAME_ */