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
|
/*
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.h
* @brief Header for platform-independent threads abstraction
* @author Karlson2k (Evgeny Grin)
*
* Provides basic abstraction for threads.
* Any functions can be implemented as macro on some platforms
* unless explicitly marked otherwise.
* Any function argument can be skipped in macro, so avoid
* variable modification in function parameters.
*
* @warning Unlike pthread functions, most of functions return
* nonzero on success.
*/
#ifndef MHD_THREADS_H
#define MHD_THREADS_H 1
#include "mhd_options.h"
#ifdef HAVE_STDDEF_H
# include <stddef.h> /* for size_t */
#elif defined(HAVE_STDLIB_H)
# include <stdlib.h> /* for size_t */
#else /* ! HAVE_STDLIB_H */
# include <stdio.h> /* for size_t */
#endif /* ! HAVE_STDLIB_H */
#if defined(MHD_USE_POSIX_THREADS)
# undef HAVE_CONFIG_H
# include <pthread.h>
# define HAVE_CONFIG_H 1
# ifndef MHD_USE_THREADS
# define MHD_USE_THREADS 1
# endif
#elif defined(MHD_USE_W32_THREADS)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
# endif /* !WIN32_LEAN_AND_MEAN */
# include <windows.h>
# ifndef MHD_USE_THREADS
# define MHD_USE_THREADS 1
# endif
#else
# error No threading API is available.
#endif
#ifndef MHD_NO_THREAD_NAMES
# if defined(MHD_USE_POSIX_THREADS)
# if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || \
defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) || \
defined(HAVE_PTHREAD_SETNAME_NP_DARWIN) || \
defined(HAVE_PTHREAD_SETNAME_NP_NETBSD) || \
defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) || \
defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
# define MHD_USE_THREAD_NAME_
# endif /* HAVE_PTHREAD_SETNAME_NP */
# elif defined(MHD_USE_W32_THREADS)
# ifdef _MSC_FULL_VER
/* Thread names only available with VC compiler */
# define MHD_USE_THREAD_NAME_
# endif /* _MSC_FULL_VER */
# endif
#endif
#if defined(MHD_USE_POSIX_THREADS)
typedef pthread_t MHD_thread_handle_;
#elif defined(MHD_USE_W32_THREADS)
typedef HANDLE MHD_thread_handle_;
#endif
#if defined(MHD_USE_POSIX_THREADS)
# define MHD_THRD_RTRN_TYPE_ void*
# define MHD_THRD_CALL_SPEC_
#elif defined(MHD_USE_W32_THREADS)
# define MHD_THRD_RTRN_TYPE_ unsigned
# define MHD_THRD_CALL_SPEC_ __stdcall
#endif
#if defined(MHD_USE_POSIX_THREADS)
typedef pthread_t MHD_thread_ID_;
#elif defined(MHD_USE_W32_THREADS)
typedef DWORD MHD_thread_ID_;
#endif
/* Depending on implementation, pthread_create() MAY set thread ID into
* provided pointer and after it start thread OR start thread and after
* it set thread ID. In the latter case, to avoid data races, additional
* pthread_self() call is required in thread routine. If some platform
* is known for setting thread ID BEFORE starting thread macro
* MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD could be defined
* to save some resources. */
/* * handle - must be valid when other thread knows that particular thread
is started.
* ID - must be valid when code is executed inside thread */
#if defined(MHD_USE_POSIX_THREADS)
# ifdef MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD
union _MHD_thread_handle_ID_
{
MHD_thread_handle_ handle; /**< To be used in other threads */
MHD_thread_ID_ ID; /**< To be used in thread itself */
};
typedef union _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
# else /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
struct _MHD_thread_handle_ID_
{
MHD_thread_handle_ handle; /**< To be used in other threads */
MHD_thread_ID_ ID; /**< To be used in thread itself */
};
typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
# endif /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
#elif defined(MHD_USE_W32_THREADS)
struct _MHD_thread_handle_ID_
{
MHD_thread_handle_ handle; /**< To be used in other threads */
MHD_thread_ID_ ID; /**< To be used in thread itself */
};
typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
#endif
#if defined(MHD_USE_POSIX_THREADS)
/**
* Wait until specified thread is ended and free thread handle on success.
* @param thread handle to watch
* @return nonzero on success, zero otherwise
*/
#define MHD_join_thread_(thread) (! pthread_join ((thread), NULL))
#elif defined(MHD_USE_W32_THREADS)
/**
* Wait until specified thread is ended and free thread handle on success.
* @param thread handle to watch
* @return nonzero on success, zero otherwise
*/
#define MHD_join_thread_(thread) \
( (WAIT_OBJECT_0 == WaitForSingleObject ( (thread), INFINITE)) ? \
(CloseHandle ( (thread)), ! 0) : 0 )
#endif
#if defined(MHD_USE_POSIX_THREADS)
/**
* Check whether provided thread ID match current thread.
* @param ID thread ID to match
* @return nonzero on match, zero otherwise
*/
#define MHD_thread_ID_match_current_(pid) \
(pthread_equal ((pid).ID, pthread_self ()))
#elif defined(MHD_USE_W32_THREADS)
/**
* Check whether provided thread ID match current thread.
* @param ID thread ID to match
* @return nonzero on match, zero otherwise
*/
#define MHD_thread_ID_match_current_(pid) (GetCurrentThreadId () == (pid).ID)
#endif
#if defined(MHD_USE_POSIX_THREADS)
# ifdef MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD
/**
* Initialise thread ID.
* @param thread_handle_ID_ptr pointer to thread handle-ID
*/
#define MHD_thread_init_(thread_handle_ID_ptr) (void) 0
# else /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
/**
* Initialise thread ID.
* @param thread_handle_ID_ptr pointer to thread handle-ID
*/
#define MHD_thread_init_(thread_handle_ID_ptr) ((thread_handle_ID_ptr)->ID = \
pthread_self ())
# endif /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
#elif defined(MHD_USE_W32_THREADS)
/**
* Initialise thread ID.
* @param thread_handle_ID_ptr pointer to thread handle-ID
*/
#define MHD_thread_init_(thread_handle_ID_ptr) ((thread_handle_ID_ptr)->ID = \
GetCurrentThreadId ())
#endif
/**
* Signature of main function for a thread.
*
* @param cls closure argument for the function
* @return termination code from the thread
*/
typedef MHD_THRD_RTRN_TYPE_
(MHD_THRD_CALL_SPEC_ *MHD_THREAD_START_ROUTINE_)(void *cls);
/**
* Create a thread and set the attributes according to our options.
*
* If thread is created, thread handle must be freed by MHD_join_thread_().
*
* @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);
#ifndef MHD_USE_THREAD_NAME_
#define MHD_create_named_thread_(t,n,s,r,a) MHD_create_thread_ ((t),(s),(r),(a))
#else /* MHD_USE_THREAD_NAME_ */
/**
* 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
*/
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);
#endif /* MHD_USE_THREAD_NAME_ */
#endif /* ! MHD_THREADS_H */
|