diff options
Diffstat (limited to 'src/microhttpd/mhd_threads.h')
-rw-r--r-- | src/microhttpd/mhd_threads.h | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/microhttpd/mhd_threads.h b/src/microhttpd/mhd_threads.h new file mode 100644 index 00000000..23f21a28 --- /dev/null +++ b/src/microhttpd/mhd_threads.h | |||
@@ -0,0 +1,151 @@ | |||
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 | |||
21 | /** | ||
22 | * @file microhttpd/mhd_threads.h | ||
23 | * @brief Header for platform-independent threads abstraction | ||
24 | * @author Karlson2k (Evgeny Grin) | ||
25 | * | ||
26 | * Provides basic abstraction for threads. | ||
27 | * Any functions can be implemented as macro on some platforms | ||
28 | * unless explicitly marked otherwise. | ||
29 | * Any function argument can be skipped in macro, so avoid | ||
30 | * variable modification in function parameters. | ||
31 | * | ||
32 | * @warning Unlike pthread functions, most of functions return | ||
33 | * nonzero on success. | ||
34 | */ | ||
35 | |||
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 | #elif defined(MHD_USE_W32_THREADS) | ||
51 | # ifndef WIN32_LEAN_AND_MEAN | ||
52 | # define WIN32_LEAN_AND_MEAN 1 | ||
53 | # endif /* !WIN32_LEAN_AND_MEAN */ | ||
54 | # include <windows.h> | ||
55 | #else | ||
56 | # error No threading API is available. | ||
57 | #endif | ||
58 | |||
59 | #ifndef MHD_NO_THREAD_NAMES | ||
60 | # if defined(MHD_USE_POSIX_THREADS) | ||
61 | # ifdef HAVE_PTHREAD_SETNAME_NP | ||
62 | # define MHD_USE_THREAD_NAME_ | ||
63 | # endif /* HAVE_PTHREAD_SETNAME_NP */ | ||
64 | # elif defined(MHD_USE_W32_THREADS) | ||
65 | # ifdef _MSC_FULL_VER | ||
66 | /* Thread names only available with VC compiler */ | ||
67 | # define MHD_USE_THREAD_NAME_ | ||
68 | # endif /* _MSC_FULL_VER */ | ||
69 | # endif | ||
70 | #endif | ||
71 | |||
72 | #if defined(MHD_USE_POSIX_THREADS) | ||
73 | typedef pthread_t MHD_thread_handle_; | ||
74 | #elif defined(MHD_USE_W32_THREADS) | ||
75 | typedef HANDLE MHD_thread_handle_; | ||
76 | #endif | ||
77 | |||
78 | #if defined(MHD_USE_POSIX_THREADS) | ||
79 | # define MHD_THRD_RTRN_TYPE_ void* | ||
80 | # define MHD_THRD_CALL_SPEC_ | ||
81 | #elif defined(MHD_USE_W32_THREADS) | ||
82 | # define MHD_THRD_RTRN_TYPE_ unsigned | ||
83 | # define MHD_THRD_CALL_SPEC_ __stdcall | ||
84 | #endif | ||
85 | |||
86 | #if defined(MHD_USE_POSIX_THREADS) | ||
87 | /** | ||
88 | * Wait until specified thread is ended and free thread handle on success. | ||
89 | * @param thread handle to watch | ||
90 | * @return nonzero on success, zero otherwise | ||
91 | */ | ||
92 | #define MHD_join_thread_(thread) (!pthread_join((thread), NULL)) | ||
93 | #elif defined(MHD_USE_W32_THREADS) | ||
94 | /** | ||
95 | * Wait until specified thread is ended and free thread handle on success. | ||
96 | * @param thread handle to watch | ||
97 | * @return nonzero on success, zero otherwise | ||
98 | */ | ||
99 | #define MHD_join_thread_(thread) (WAIT_OBJECT_0 == WaitForSingleObject((thread), INFINITE) ? (CloseHandle((thread)), !0) : 0) | ||
100 | #endif | ||
101 | |||
102 | /** | ||
103 | * Signature of main function for a thread. | ||
104 | * | ||
105 | * @param cls closure argument for the function | ||
106 | * @return termination code from the thread | ||
107 | */ | ||
108 | typedef MHD_THRD_RTRN_TYPE_ | ||
109 | (MHD_THRD_CALL_SPEC_ *MHD_THREAD_START_ROUTINE_)(void *cls); | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Create a thread and set the attributes according to our options. | ||
114 | * | ||
115 | * If thread is created, thread handle must be freed by #MHD_join_thread_(). | ||
116 | * | ||
117 | * @param thread handle to initialize | ||
118 | * @param stack_size size of stack for new thread, 0 for default | ||
119 | * @param start_routine main function of thread | ||
120 | * @param arg argument for start_routine | ||
121 | * @return non-zero on success; zero otherwise | ||
122 | */ | ||
123 | int | ||
124 | MHD_create_thread_ (MHD_thread_handle_ *thread, | ||
125 | size_t stack_size, | ||
126 | MHD_THREAD_START_ROUTINE_ start_routine, | ||
127 | void *arg); | ||
128 | |||
129 | #ifndef MHD_USE_THREAD_NAME_ | ||
130 | #define MHD_create_named_thread_(t,n,s,r,a) MHD_create_thread_((t),(s),(r),(a)) | ||
131 | #else /* MHD_USE_THREAD_NAME_ */ | ||
132 | /** | ||
133 | * Create a named thread and set the attributes according to our options. | ||
134 | * | ||
135 | * @param thread handle to initialize | ||
136 | * @param thread_name name for new thread | ||
137 | * @param stack_size size of stack for new thread, 0 for default | ||
138 | * @param start_routine main function of thread | ||
139 | * @param arg argument for start_routine | ||
140 | * @return non-zero on success; zero otherwise | ||
141 | */ | ||
142 | int | ||
143 | MHD_create_named_thread_ (MHD_thread_handle_ *thread, | ||
144 | const char* thread_name, | ||
145 | size_t stack_size, | ||
146 | MHD_THREAD_START_ROUTINE_ start_routine, | ||
147 | void *arg); | ||
148 | |||
149 | #endif /* MHD_USE_THREAD_NAME_ */ | ||
150 | |||
151 | #endif /* ! MHD_THREADS_H */ | ||