aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_threads.h
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-11-21 23:10:18 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-11-22 14:39:18 +0300
commit11e15b46f05352932e2682d7664648f717aa979a (patch)
tree6e544f346fde1df8ca26cf347d8033a7b025754d /src/microhttpd/mhd_threads.h
parent23324164fb6fcc421a0bf081c50a00ce3f66f9b7 (diff)
downloadlibmicrohttpd-11e15b46f05352932e2682d7664648f717aa979a.tar.gz
libmicrohttpd-11e15b46f05352932e2682d7664648f717aa979a.zip
mhd_threads: Fixed thread ID data races on pthreads
Diffstat (limited to 'src/microhttpd/mhd_threads.h')
-rw-r--r--src/microhttpd/mhd_threads.h32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/microhttpd/mhd_threads.h b/src/microhttpd/mhd_threads.h
index 1a06c64a..3778ad09 100644
--- a/src/microhttpd/mhd_threads.h
+++ b/src/microhttpd/mhd_threads.h
@@ -91,18 +91,34 @@
91 typedef DWORD MHD_thread_ID_; 91 typedef DWORD MHD_thread_ID_;
92#endif 92#endif
93 93
94/* Depending on implementation, pthread_create() MAY set thread ID into
95 * provided pointer and after it start thread OR start thread and after
96 * if set thread ID. In latter case, to avoid data races, additional
97 * pthread_self() call is required in thread routine. Is some platform
98 * is known for setting thread ID BEFORE starting thread macro
99 * MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD could be defined
100 * to save some resources. */
94#if defined(MHD_USE_POSIX_THREADS) 101#if defined(MHD_USE_POSIX_THREADS)
102# ifdef MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD
95 union _MHD_thread_handle_ID_ 103 union _MHD_thread_handle_ID_
96 { 104 {
97 MHD_thread_handle_ handle; 105 MHD_thread_handle_ handle; /**< To be used in other threads */
98 MHD_thread_ID_ ID; 106 MHD_thread_ID_ ID; /**< To be used in thread itself */
99 }; 107 };
100 typedef union _MHD_thread_handle_ID_ MHD_thread_handle_ID_; 108 typedef union _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
109# else /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
110 struct _MHD_thread_handle_ID_
111 {
112 MHD_thread_handle_ handle; /**< To be used in other threads */
113 MHD_thread_ID_ ID; /**< To be used in thread itself */
114 };
115 typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
116# endif /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
101#elif defined(MHD_USE_W32_THREADS) 117#elif defined(MHD_USE_W32_THREADS)
102 struct _MHD_thread_handle_ID_ 118 struct _MHD_thread_handle_ID_
103 { 119 {
104 MHD_thread_handle_ handle; 120 MHD_thread_handle_ handle; /**< To be used in other threads */
105 MHD_thread_ID_ ID; 121 MHD_thread_ID_ ID; /**< To be used in thread itself */
106 }; 122 };
107 typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_; 123 typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
108#endif 124#endif
@@ -140,11 +156,19 @@
140#endif 156#endif
141 157
142#if defined(MHD_USE_POSIX_THREADS) 158#if defined(MHD_USE_POSIX_THREADS)
159# ifdef MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD
143/** 160/**
144 * Initialise thread ID. 161 * Initialise thread ID.
145 * @param thread_handle_ID_ptr pointer to thread handle-ID 162 * @param thread_handle_ID_ptr pointer to thread handle-ID
146 */ 163 */
147#define MHD_thread_init_(thread_handle_ID_ptr) (void)0 164#define MHD_thread_init_(thread_handle_ID_ptr) (void)0
165# else /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
166/**
167 * Initialise thread ID.
168 * @param thread_handle_ID_ptr pointer to thread handle-ID
169 */
170#define MHD_thread_init_(thread_handle_ID_ptr) ((thread_handle_ID_ptr)->ID=pthread_self())
171# endif /* ! MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD */
148#elif defined(MHD_USE_W32_THREADS) 172#elif defined(MHD_USE_W32_THREADS)
149/** 173/**
150 * Initialise thread ID. 174 * Initialise thread ID.