GNU libmicrohttpd 0.9.73
Loading...
Searching...
No Matches
mhd_threads.h
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
36#ifndef MHD_THREADS_H
37#define MHD_THREADS_H 1
38
39#include "mhd_options.h"
40#ifdef HAVE_STDDEF_H
41# include <stddef.h> /* for size_t */
42#else /* ! HAVE_STDDEF_H */
43# include <stdlib.h> /* for size_t */
44#endif /* ! HAVE_STDDEF_H */
45
46#if defined(MHD_USE_POSIX_THREADS)
47# undef HAVE_CONFIG_H
48# include <pthread.h>
49# define HAVE_CONFIG_H 1
50# ifndef MHD_USE_THREADS
51# define MHD_USE_THREADS 1
52# endif
53#elif defined(MHD_USE_W32_THREADS)
54# ifndef WIN32_LEAN_AND_MEAN
55# define WIN32_LEAN_AND_MEAN 1
56# endif /* !WIN32_LEAN_AND_MEAN */
57# include <windows.h>
58# ifndef MHD_USE_THREADS
59# define MHD_USE_THREADS 1
60# endif
61#else
62# error No threading API is available.
63#endif
64
65#ifndef MHD_NO_THREAD_NAMES
66# if defined(MHD_USE_POSIX_THREADS)
67# if defined(HAVE_PTHREAD_SETNAME_NP_GNU) || \
68 defined(HAVE_PTHREAD_SET_NAME_NP_FREEBSD) || \
69 defined(HAVE_PTHREAD_SETNAME_NP_DARWIN) || \
70 defined(HAVE_PTHREAD_SETNAME_NP_NETBSD) || \
71 defined(HAVE_PTHREAD_ATTR_SETNAME_NP_NETBSD) || \
72 defined(HAVE_PTHREAD_ATTR_SETNAME_NP_IBMI)
73# define MHD_USE_THREAD_NAME_
74# endif /* HAVE_PTHREAD_SETNAME_NP */
75# elif defined(MHD_USE_W32_THREADS)
76# ifdef _MSC_FULL_VER
77/* Thread names only available with VC compiler */
78# define MHD_USE_THREAD_NAME_
79# endif /* _MSC_FULL_VER */
80# endif
81#endif
82
83#if defined(MHD_USE_POSIX_THREADS)
84typedef pthread_t MHD_thread_handle_;
85#elif defined(MHD_USE_W32_THREADS)
86typedef HANDLE MHD_thread_handle_;
87#endif
88
89#if defined(MHD_USE_POSIX_THREADS)
90# define MHD_THRD_RTRN_TYPE_ void*
91# define MHD_THRD_CALL_SPEC_
92#elif defined(MHD_USE_W32_THREADS)
93# define MHD_THRD_RTRN_TYPE_ unsigned
94# define MHD_THRD_CALL_SPEC_ __stdcall
95#endif
96
97#if defined(MHD_USE_POSIX_THREADS)
98typedef pthread_t MHD_thread_ID_;
99#elif defined(MHD_USE_W32_THREADS)
100typedef DWORD MHD_thread_ID_;
101#endif
102
103/* Depending on implementation, pthread_create() MAY set thread ID into
104 * provided pointer and after it start thread OR start thread and after
105 * it set thread ID. In the latter case, to avoid data races, additional
106 * pthread_self() call is required in thread routine. If some platform
107 * is known for setting thread ID BEFORE starting thread macro
108 * MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD could be defined
109 * to save some resources. */
110/* * handle - must be valid when other thread knows that particular thread
111 is started.
112 * ID - must be valid when code is executed inside thread */
113#if defined(MHD_USE_POSIX_THREADS)
114# ifdef MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD
115union _MHD_thread_handle_ID_
116{
117 MHD_thread_handle_ handle;
118 MHD_thread_ID_ ID;
119};
120typedef union _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
121# else /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
122struct _MHD_thread_handle_ID_
123{
124 MHD_thread_handle_ handle;
125 MHD_thread_ID_ ID;
126};
127typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
128# endif /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
129#elif defined(MHD_USE_W32_THREADS)
130struct _MHD_thread_handle_ID_
131{
132 MHD_thread_handle_ handle;
133 MHD_thread_ID_ ID;
134};
135typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
136#endif
137
138#if defined(MHD_USE_POSIX_THREADS)
144#define MHD_join_thread_(thread) (! pthread_join ((thread), NULL))
145#elif defined(MHD_USE_W32_THREADS)
151#define MHD_join_thread_(thread) (WAIT_OBJECT_0 == WaitForSingleObject ( \
152 (thread), INFINITE) ? (CloseHandle ( \
153 (thread)), ! 0) : \
154 0)
155#endif
156
157#if defined(MHD_USE_POSIX_THREADS)
163#define MHD_thread_ID_match_current_(pid) \
164 (pthread_equal ((pid).ID, pthread_self ()))
165#elif defined(MHD_USE_W32_THREADS)
171#define MHD_thread_ID_match_current_(pid) (GetCurrentThreadId () == (pid).ID)
172#endif
173
174#if defined(MHD_USE_POSIX_THREADS)
175# ifdef MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD
180#define MHD_thread_init_(thread_handle_ID_ptr) (void) 0
181# else /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
186#define MHD_thread_init_(thread_handle_ID_ptr) ((thread_handle_ID_ptr)->ID = \
187 pthread_self ())
188# endif /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
189#elif defined(MHD_USE_W32_THREADS)
194#define MHD_thread_init_(thread_handle_ID_ptr) ((thread_handle_ID_ptr)->ID = \
195 GetCurrentThreadId ())
196#endif
197
204typedef MHD_THRD_RTRN_TYPE_
205(MHD_THRD_CALL_SPEC_ *MHD_THREAD_START_ROUTINE_)(void *cls);
206
207
219int
220MHD_create_thread_ (MHD_thread_handle_ID_ *thread,
221 size_t stack_size,
222 MHD_THREAD_START_ROUTINE_ start_routine,
223 void *arg);
224
225#ifndef MHD_USE_THREAD_NAME_
226#define MHD_create_named_thread_(t,n,s,r,a) MHD_create_thread_ ((t),(s),(r),(a))
227#else /* MHD_USE_THREAD_NAME_ */
238int
239MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread,
240 const char*thread_name,
241 size_t stack_size,
242 MHD_THREAD_START_ROUTINE_ start_routine,
243 void *arg);
244
245#endif /* MHD_USE_THREAD_NAME_ */
246
247#endif /* ! MHD_THREADS_H */
int MHD_create_thread_(MHD_thread_handle_ID_ *thread, size_t stack_size, MHD_THREAD_START_ROUTINE_ start_routine, void *arg)
Definition: mhd_threads.c:180
MHD_THRD_RTRN_TYPE_(MHD_THRD_CALL_SPEC_ * MHD_THREAD_START_ROUTINE_)(void *cls)
Definition: mhd_threads.h:195
#define MHD_create_named_thread_(t, n, s, r, a)
Definition: mhd_threads.h:216
additional automatic macros for MHD_config.h