aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2020-10-08 22:44:33 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2020-10-08 22:44:33 +0300
commit091bf71cb51098586475d44b97bf5e37c4a8824f (patch)
tree95e75cfb96a69a39d0879473cb2e405b70148f3a
parentfa42f6207bf3fe3b4c5f1bee26deb8933878050f (diff)
downloadlibmicrohttpd-091bf71cb51098586475d44b97bf5e37c4a8824f.tar.gz
libmicrohttpd-091bf71cb51098586475d44b97bf5e37c4a8824f.zip
Added asserts to check correct threads for key functions
-rw-r--r--src/microhttpd/connection.c13
-rw-r--r--src/microhttpd/daemon.c40
-rw-r--r--src/microhttpd/mhd_threads.h5
-rw-r--r--src/microhttpd/response.c3
4 files changed, 56 insertions, 5 deletions
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index 2d9dd9a9..7fd52739 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -687,6 +687,9 @@ MHD_connection_close_ (struct MHD_Connection *connection,
687 struct MHD_Daemon *daemon = connection->daemon; 687 struct MHD_Daemon *daemon = connection->daemon;
688 struct MHD_Response *resp = connection->response; 688 struct MHD_Response *resp = connection->response;
689 689
690 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
691 MHD_thread_ID_match_current_ (connection->pid) );
692
690 MHD_connection_mark_closed_ (connection); 693 MHD_connection_mark_closed_ (connection);
691 if (NULL != resp) 694 if (NULL != resp)
692 { 695 {
@@ -720,6 +723,10 @@ MHD_connection_finish_forward_ (struct MHD_Connection *connection)
720 struct MHD_Daemon *daemon = connection->daemon; 723 struct MHD_Daemon *daemon = connection->daemon;
721 struct MHD_UpgradeResponseHandle *urh = connection->urh; 724 struct MHD_UpgradeResponseHandle *urh = connection->urh;
722 725
726 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
727 (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || \
728 MHD_thread_ID_match_current_ (daemon->pid) );
729
723 if (0 == (daemon->options & MHD_USE_TLS)) 730 if (0 == (daemon->options & MHD_USE_TLS))
724 return; /* Nothing to do with non-TLS connection. */ 731 return; /* Nothing to do with non-TLS connection. */
725 732
@@ -3158,6 +3165,8 @@ static void
3158cleanup_connection (struct MHD_Connection *connection) 3165cleanup_connection (struct MHD_Connection *connection)
3159{ 3166{
3160 struct MHD_Daemon *daemon = connection->daemon; 3167 struct MHD_Daemon *daemon = connection->daemon;
3168 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
3169 MHD_thread_ID_match_current_ (connection->pid) );
3161 3170
3162 if (connection->in_cleanup) 3171 if (connection->in_cleanup)
3163 return; /* Prevent double cleanup. */ 3172 return; /* Prevent double cleanup. */
@@ -3237,6 +3246,8 @@ MHD_connection_handle_idle (struct MHD_Connection *connection)
3237 char *line; 3246 char *line;
3238 size_t line_len; 3247 size_t line_len;
3239 enum MHD_Result ret; 3248 enum MHD_Result ret;
3249 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
3250 MHD_thread_ID_match_current_ (connection->pid) );
3240 3251
3241 connection->in_idle = true; 3252 connection->in_idle = true;
3242 while (! connection->suspended) 3253 while (! connection->suspended)
@@ -3933,7 +3944,7 @@ MHD_queue_response (struct MHD_Connection *connection,
3933#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 3944#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3934 if ( (! connection->suspended) && 3945 if ( (! connection->suspended) &&
3935 (0 != (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) && 3946 (0 != (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) &&
3936 (! MHD_thread_ID_match_current_ (connection->pid.ID)) ) 3947 (! MHD_thread_ID_match_current_ (connection->pid)) )
3937 { 3948 {
3938#ifdef HAVE_MESSAGES 3949#ifdef HAVE_MESSAGES
3939 MHD_DLOG (daemon, 3950 MHD_DLOG (daemon,
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index 48434112..480c37cd 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -1336,6 +1336,8 @@ process_urh (struct MHD_UpgradeResponseHandle *urh)
1336 * of processing - it will be processed on next iteration. */ 1336 * of processing - it will be processed on next iteration. */
1337 bool was_closed; 1337 bool was_closed;
1338 1338
1339 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
1340 MHD_thread_ID_match_current_ (connection->pid) );
1339 if (daemon->shutdown) 1341 if (daemon->shutdown)
1340 { 1342 {
1341 /* Daemon shutting down, application will not receive any more data. */ 1343 /* Daemon shutting down, application will not receive any more data. */
@@ -1687,6 +1689,8 @@ thread_main_connection_upgrade (struct MHD_Connection *con)
1687 struct MHD_UpgradeResponseHandle *urh = con->urh; 1689 struct MHD_UpgradeResponseHandle *urh = con->urh;
1688 struct MHD_Daemon *daemon = con->daemon; 1690 struct MHD_Daemon *daemon = con->daemon;
1689 1691
1692 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
1693 MHD_thread_ID_match_current_ (con->pid) );
1690 /* Here, we need to bi-directionally forward 1694 /* Here, we need to bi-directionally forward
1691 until the application tells us that it is done 1695 until the application tells us that it is done
1692 with the socket; */ 1696 with the socket; */
@@ -2818,6 +2822,10 @@ internal_suspend_connection_ (struct MHD_Connection *connection)
2818{ 2822{
2819 struct MHD_Daemon *daemon = connection->daemon; 2823 struct MHD_Daemon *daemon = connection->daemon;
2820 2824
2825 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
2826 (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || \
2827 MHD_thread_ID_match_current_ (daemon->pid) );
2828
2821#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 2829#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
2822 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); 2830 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2823#endif 2831#endif
@@ -2913,6 +2921,10 @@ MHD_suspend_connection (struct MHD_Connection *connection)
2913{ 2921{
2914 struct MHD_Daemon *const daemon = connection->daemon; 2922 struct MHD_Daemon *const daemon = connection->daemon;
2915 2923
2924 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
2925 (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || \
2926 MHD_thread_ID_match_current_ (daemon->pid) );
2927
2916 if (0 == (daemon->options & MHD_TEST_ALLOW_SUSPEND_RESUME)) 2928 if (0 == (daemon->options & MHD_TEST_ALLOW_SUSPEND_RESUME))
2917 MHD_PANIC (_ ( 2929 MHD_PANIC (_ (
2918 "Cannot suspend connections without enabling MHD_ALLOW_SUSPEND_RESUME!\n")); 2930 "Cannot suspend connections without enabling MHD_ALLOW_SUSPEND_RESUME!\n"));
@@ -2991,6 +3003,9 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
2991#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 3003#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
2992 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); 3004 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
2993#endif 3005#endif
3006 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
3007 MHD_thread_ID_match_current_ (daemon->pid) );
3008
2994 if (daemon->resuming) 3009 if (daemon->resuming)
2995 { 3010 {
2996 prev = daemon->suspended_connections_tail; 3011 prev = daemon->suspended_connections_tail;
@@ -3206,6 +3221,9 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
3206 MHD_socket fd; 3221 MHD_socket fd;
3207 bool sk_nonbl; 3222 bool sk_nonbl;
3208 3223
3224 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
3225 MHD_thread_ID_match_current_ (daemon->pid) );
3226
3209 addrlen = sizeof (addrstorage); 3227 addrlen = sizeof (addrstorage);
3210 memset (addr, 3228 memset (addr,
3211 0, 3229 0,
@@ -3346,6 +3364,8 @@ static void
3346MHD_cleanup_connections (struct MHD_Daemon *daemon) 3364MHD_cleanup_connections (struct MHD_Daemon *daemon)
3347{ 3365{
3348 struct MHD_Connection *pos; 3366 struct MHD_Connection *pos;
3367 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
3368 MHD_thread_ID_match_current_ (daemon->pid) );
3349 3369
3350#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 3370#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
3351 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex); 3371 MHD_mutex_lock_chk_ (&daemon->cleanup_connection_mutex);
@@ -3460,6 +3480,9 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
3460 struct MHD_Connection *pos; 3480 struct MHD_Connection *pos;
3461 bool have_timeout; 3481 bool have_timeout;
3462 3482
3483 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
3484 MHD_thread_ID_match_current_ (daemon->pid) );
3485
3463 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 3486 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
3464 { 3487 {
3465#ifdef HAVE_MESSAGES 3488#ifdef HAVE_MESSAGES
@@ -4254,6 +4277,9 @@ run_epoll_for_upgrade (struct MHD_Daemon *daemon)
4254 struct MHD_UpgradeResponseHandle *pos; 4277 struct MHD_UpgradeResponseHandle *pos;
4255 struct MHD_UpgradeResponseHandle *prev; 4278 struct MHD_UpgradeResponseHandle *prev;
4256 4279
4280 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
4281 MHD_thread_ID_match_current_ (daemon->pid) );
4282
4257 num_events = MAX_EVENTS; 4283 num_events = MAX_EVENTS;
4258 while (0 != num_events) 4284 while (0 != num_events)
4259 { 4285 {
@@ -4733,6 +4759,9 @@ close_connection (struct MHD_Connection *pos)
4733{ 4759{
4734 struct MHD_Daemon *daemon = pos->daemon; 4760 struct MHD_Daemon *daemon = pos->daemon;
4735 4761
4762 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
4763 MHD_thread_ID_match_current_ (daemon->pid) );
4764
4736 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 4765 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
4737 { 4766 {
4738 MHD_connection_mark_closed_ (pos); 4767 MHD_connection_mark_closed_ (pos);
@@ -5628,8 +5657,7 @@ setup_epoll_fd (struct MHD_Daemon *daemon)
5628/** 5657/**
5629 * Setup epoll() FD for the daemon and initialize it to listen 5658 * Setup epoll() FD for the daemon and initialize it to listen
5630 * on the listen FD. 5659 * on the listen FD.
5631 * @remark To be called only from thread that process 5660 * @remark To be called only from MHD_start_daemon_va()
5632 * daemon's select()/poll()/etc.
5633 * 5661 *
5634 * @param daemon daemon to initialize for epoll() 5662 * @param daemon daemon to initialize for epoll()
5635 * @return #MHD_YES on success, #MHD_NO on failure 5663 * @return #MHD_YES on success, #MHD_NO on failure
@@ -6693,6 +6721,10 @@ close_all_connections (struct MHD_Daemon *daemon)
6693 struct MHD_UpgradeResponseHandle *urhn; 6721 struct MHD_UpgradeResponseHandle *urhn;
6694 const bool used_tls = (0 != (daemon->options & MHD_USE_TLS)); 6722 const bool used_tls = (0 != (daemon->options & MHD_USE_TLS));
6695 6723
6724 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
6725 (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || \
6726 MHD_thread_ID_match_current_ (daemon->pid) );
6727
6696#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS) 6728#if defined(MHD_USE_POSIX_THREADS) || defined(MHD_USE_W32_THREADS)
6697 mhd_assert (NULL == daemon->worker_pool); 6729 mhd_assert (NULL == daemon->worker_pool);
6698#endif 6730#endif
@@ -6850,6 +6882,10 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
6850 6882
6851 if (NULL == daemon) 6883 if (NULL == daemon)
6852 return; 6884 return;
6885 if ( (daemon->shutdown) && (NULL == daemon->master) )
6886 MHD_PANIC (_ ("MHD_stop_daemon() was called twice."));
6887 /* Slave daemons must be stopped by master daemon. */
6888 mhd_assert ( (NULL == daemon->master) || (daemon->shutdown) );
6853 6889
6854 daemon->shutdown = true; 6890 daemon->shutdown = true;
6855 if (daemon->was_quiesced) 6891 if (daemon->was_quiesced)
diff --git a/src/microhttpd/mhd_threads.h b/src/microhttpd/mhd_threads.h
index ee2f6020..6f690893 100644
--- a/src/microhttpd/mhd_threads.h
+++ b/src/microhttpd/mhd_threads.h
@@ -154,14 +154,15 @@ typedef struct _MHD_thread_handle_ID_ MHD_thread_handle_ID_;
154 * @param ID thread ID to match 154 * @param ID thread ID to match
155 * @return nonzero on match, zero otherwise 155 * @return nonzero on match, zero otherwise
156 */ 156 */
157#define MHD_thread_ID_match_current_(ID) (pthread_equal ((ID), pthread_self ())) 157#define MHD_thread_ID_match_current_(pid) \
158 (pthread_equal ((pid).ID, pthread_self ()))
158#elif defined(MHD_USE_W32_THREADS) 159#elif defined(MHD_USE_W32_THREADS)
159/** 160/**
160 * Check whether provided thread ID match current thread. 161 * Check whether provided thread ID match current thread.
161 * @param ID thread ID to match 162 * @param ID thread ID to match
162 * @return nonzero on match, zero otherwise 163 * @return nonzero on match, zero otherwise
163 */ 164 */
164#define MHD_thread_ID_match_current_(ID) (GetCurrentThreadId () == (ID)) 165#define MHD_thread_ID_match_current_(pid) (GetCurrentThreadId () == (pid).ID)
165#endif 166#endif
166 167
167#if defined(MHD_USE_POSIX_THREADS) 168#if defined(MHD_USE_POSIX_THREADS)
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
index 27386046..dc04cab5 100644
--- a/src/microhttpd/response.c
+++ b/src/microhttpd/response.c
@@ -969,6 +969,9 @@ MHD_response_execute_upgrade_ (struct MHD_Response *response,
969 struct MHD_UpgradeResponseHandle *urh; 969 struct MHD_UpgradeResponseHandle *urh;
970 size_t rbo; 970 size_t rbo;
971 971
972 mhd_assert ( (0 == (daemon->options & MHD_USE_INTERNAL_POLLING_THREAD)) || \
973 MHD_thread_ID_match_current_ (connection->pid) );
974
972 if (0 == (daemon->options & MHD_ALLOW_UPGRADE)) 975 if (0 == (daemon->options & MHD_ALLOW_UPGRADE))
973 return MHD_NO; 976 return MHD_NO;
974 977