commit 11e15b46f05352932e2682d7664648f717aa979a
parent 23324164fb6fcc421a0bf081c50a00ce3f66f9b7
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Tue, 21 Nov 2017 23:10:18 +0300
mhd_threads: Fixed thread ID data races on pthreads
Diffstat:
1 file changed, 28 insertions(+), 4 deletions(-)
diff --git a/src/microhttpd/mhd_threads.h b/src/microhttpd/mhd_threads.h
@@ -91,18 +91,34 @@
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
+ * if set thread ID. In latter case, to avoid data races, additional
+ * pthread_self() call is required in thread routine. Is 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. */
#if defined(MHD_USE_POSIX_THREADS)
+# ifdef MHD_PTHREAD_CREATE__SET_ID_BEFORE_START_THREAD
union _MHD_thread_handle_ID_
{
- MHD_thread_handle_ handle;
- MHD_thread_ID_ 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;
- MHD_thread_ID_ 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
@@ -140,11 +156,19 @@
#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.