libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 4fc783ecbd230c5c8e8ed0de1818b252070ef979
parent 3e7a55e9685eb8bf51b6307883b366b2dd38948a
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Sun, 27 Aug 2017 20:33:34 +0300

mhd_threads: added thread ID support

Diffstat:
Msrc/microhttpd/daemon.c | 8++++----
Msrc/microhttpd/internal.h | 4++--
Msrc/microhttpd/mhd_threads.c | 28++++++++++++----------------
Msrc/microhttpd/mhd_threads.h | 26++++++++++++++++++++++++--
4 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c @@ -2962,7 +2962,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon) if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && (! pos->thread_joined) && - (! MHD_join_thread_ (pos->pid)) ) + (! MHD_join_thread_ (pos->pid.handle)) ) MHD_PANIC (_("Failed to join a thread\n")); #ifdef UPGRADE_SUPPORT cleanup_upgraded_connection (pos); @@ -6131,7 +6131,7 @@ close_all_connections (struct MHD_Daemon *daemon) if (! pos->thread_joined) { MHD_mutex_unlock_chk_ (&daemon->cleanup_connection_mutex); - if (! MHD_join_thread_ (pos->pid)) + if (! MHD_join_thread_ (pos->pid.handle)) MHD_PANIC (_("Failed to join a thread\n")); MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); pos->thread_joined = true; @@ -6218,7 +6218,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) /* Start harvesting. */ for (i = 0; i < daemon->worker_pool_size; ++i) { - if (! MHD_join_thread_ (daemon->worker_pool[i].pid)) + if (! MHD_join_thread_ (daemon->worker_pool[i].pid.handle)) MHD_PANIC (_("Failed to join a thread\n")); #ifdef EPOLL_SUPPORT if (-1 != daemon->worker_pool[i].epoll_fd) @@ -6252,7 +6252,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) SHUT_RDWR); } #endif - if (! MHD_join_thread_ (daemon->pid)) + if (! MHD_join_thread_ (daemon->pid.handle)) { MHD_PANIC (_("Failed to join a thread\n")); } diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h @@ -755,7 +755,7 @@ struct MHD_Connection * Thread handle for this connection (if we are using * one thread per connection). */ - MHD_thread_handle_ pid; + MHD_thread_handle_ID_ pid; /** * Size of @e read_buffer (in bytes). This value indicates @@ -1442,7 +1442,7 @@ struct MHD_Daemon /** * The select thread handle (if we have internal select) */ - MHD_thread_handle_ pid; + MHD_thread_handle_ID_ pid; /** * Mutex for per-IP connection counts. diff --git a/src/microhttpd/mhd_threads.c b/src/microhttpd/mhd_threads.c @@ -38,14 +38,6 @@ #include <errno.h> - -#if defined(MHD_USE_POSIX_THREADS) -typedef pthread_t MHD_thread_ID_; -#elif defined(MHD_USE_W32_THREADS) -typedef DWORD MHD_thread_ID_; -#endif - - #ifndef MHD_USE_THREAD_NAME_ #define MHD_set_thread_name_(t, n) (void) @@ -183,7 +175,7 @@ MHD_set_thread_name_(const MHD_thread_ID_ thread_id, * @return non-zero on success; zero otherwise (with errno set) */ int -MHD_create_thread_ (MHD_thread_handle_ *thread, +MHD_create_thread_ (MHD_thread_handle_ID_ *thread, size_t stack_size, MHD_THREAD_START_ROUTINE_ start_routine, void *arg) @@ -200,7 +192,7 @@ MHD_create_thread_ (MHD_thread_handle_ *thread, res = pthread_attr_setstacksize (&attr, stack_size); if (0 == res) - res = pthread_create (thread, + res = pthread_create (&(thread->handle), &attr, start_routine, arg); @@ -208,7 +200,7 @@ MHD_create_thread_ (MHD_thread_handle_ *thread, } } else - res = pthread_create (thread, + res = pthread_create (&(thread->handle), NULL, start_routine, arg); @@ -218,6 +210,7 @@ MHD_create_thread_ (MHD_thread_handle_ *thread, return !res; #elif defined(MHD_USE_W32_THREADS) + unsigned int thread_ID; #if SIZE_MAX != UINT_MAX if (stack_size > UINT_MAX) { @@ -226,15 +219,18 @@ MHD_create_thread_ (MHD_thread_handle_ *thread, } #endif /* SIZE_MAX != UINT_MAX */ - *thread = (HANDLE) _beginthreadex (NULL, + thread->handle = (MHD_thread_handle_) + _beginthreadex (NULL, (unsigned int) stack_size, start_routine, arg, 0, - NULL); - if ((MHD_thread_handle_)-1 == (*thread)) + &thread_ID); + + if ((MHD_thread_handle_)-1 == thread->handle) return 0; + thread->ID = (MHD_thread_ID_)thread_ID; return !0; #endif } @@ -294,7 +290,7 @@ named_thread_starter (void *data) * @return non-zero on success; zero otherwise (with errno set) */ int -MHD_create_named_thread_ (MHD_thread_handle_ *thread, +MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread, const char* thread_name, size_t stack_size, MHD_THREAD_START_ROUTINE_ start_routine, @@ -323,7 +319,7 @@ MHD_create_named_thread_ (MHD_thread_handle_ *thread, res = pthread_attr_setstacksize (&attr, stack_size); if (0 == res) - res = pthread_create (thread, + res = pthread_create (&(thread->handle), &attr, start_routine, arg); diff --git a/src/microhttpd/mhd_threads.h b/src/microhttpd/mhd_threads.h @@ -86,6 +86,28 @@ #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 + +#if defined(MHD_USE_POSIX_THREADS) + union _MHD_thread_handle_ID_ + { + MHD_thread_handle_ handle; + MHD_thread_ID_ ID; + }; + typedef union _MHD_thread_handle_ID_ MHD_thread_handle_ID_; +#elif defined(MHD_USE_W32_THREADS) + struct _MHD_thread_handle_ID_ + { + MHD_thread_handle_ handle; + MHD_thread_ID_ ID; + }; + 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 @@ -123,7 +145,7 @@ typedef MHD_THRD_RTRN_TYPE_ * @return non-zero on success; zero otherwise */ int -MHD_create_thread_ (MHD_thread_handle_ *thread, +MHD_create_thread_ (MHD_thread_handle_ID_ *thread, size_t stack_size, MHD_THREAD_START_ROUTINE_ start_routine, void *arg); @@ -142,7 +164,7 @@ MHD_create_thread_ (MHD_thread_handle_ *thread, * @return non-zero on success; zero otherwise */ int -MHD_create_named_thread_ (MHD_thread_handle_ *thread, +MHD_create_named_thread_ (MHD_thread_handle_ID_ *thread, const char* thread_name, size_t stack_size, MHD_THREAD_START_ROUTINE_ start_routine,