diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-10 13:52:51 +0000 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-08-10 13:52:51 +0000 |
commit | 40b07cc6451ce9b0c36f353385b777c4ebfcb138 (patch) | |
tree | 1c2262c387dd01c6672f51628b8a0c74cae18199 | |
parent | a7029c10cccdf2b0d4597eb79fb58d2f11d167f1 (diff) |
Moved locks and mutex abstraction to mhd_locks.h
Minor refactoring to allow better code optimization.
-rw-r--r-- | src/include/platform_interface.h | 108 | ||||
-rw-r--r-- | src/microhttpd/Makefile.am | 1 | ||||
-rw-r--r-- | src/microhttpd/connection.c | 12 | ||||
-rw-r--r-- | src/microhttpd/daemon.c | 49 | ||||
-rw-r--r-- | src/microhttpd/internal.h | 1 | ||||
-rw-r--r-- | src/microhttpd/mhd_locks.h | 150 | ||||
-rw-r--r-- | src/microhttpd/response.c | 4 | ||||
-rw-r--r-- | w32/common/libmicrohttpd-files.vcxproj | 1 | ||||
-rw-r--r-- | w32/common/libmicrohttpd-filters.vcxproj | 3 |
9 files changed, 186 insertions, 143 deletions
diff --git a/src/include/platform_interface.h b/src/include/platform_interface.h index 13f54530..c2aee64b 100644 --- a/src/include/platform_interface.h +++ b/src/include/platform_interface.h @@ -189,113 +189,5 @@ typedef int _MHD_socket_funcs_size; #else #define MHD_random_() MHD_W32_random_() #endif -#if defined(MHD_USE_W32_THREADS) -#define MHD_W32_MUTEX_ 1 -#include <windows.h> -typedef CRITICAL_SECTION MHD_mutex_; -#elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS) -#define MHD_PTHREAD_MUTEX_ 1 -typedef pthread_mutex_t MHD_mutex_; -#else -#error "No base mutex API is available." -#endif - -#if defined(MHD_PTHREAD_MUTEX_) -/** - * Create new mutex. - * @param mutex pointer to the mutex - * @return #MHD_YES on success, #MHD_NO on failure - */ -#define MHD_mutex_create_(mutex) \ - ((0 == pthread_mutex_init ((mutex), NULL)) ? MHD_YES : MHD_NO) -#elif defined(MHD_W32_MUTEX_) -/** - * Create new mutex. - * @param mutex pointer to mutex - * @return #MHD_YES on success, #MHD_NO on failure - */ -#define MHD_mutex_create_(mutex) \ - ((NULL != (mutex) && 0 != InitializeCriticalSectionAndSpinCount((mutex),2000)) ? MHD_YES : MHD_NO) -#endif - -#if defined(MHD_PTHREAD_MUTEX_) -/** - * Destroy previously created mutex. - * @param mutex pointer to mutex - * @return #MHD_YES on success, #MHD_NO on failure - */ -#define MHD_mutex_destroy_(mutex) \ - ((0 == pthread_mutex_destroy ((mutex))) ? MHD_YES : MHD_NO) -#elif defined(MHD_W32_MUTEX_) -/** - * Destroy previously created mutex. - * @param mutex pointer to mutex - * @return #MHD_YES on success, #MHD_NO on failure - */ -#define MHD_mutex_destroy_(mutex) \ - ((NULL != (mutex)) ? (DeleteCriticalSection(mutex), MHD_YES) : MHD_NO) -#endif - -#if defined(MHD_PTHREAD_MUTEX_) -/** - * Acquire lock on previously created mutex. - * If mutex was already locked by other thread, function - * blocks until mutex becomes available. - * @param mutex pointer to mutex - * @return #MHD_YES on success, #MHD_NO on failure - */ -#define MHD_mutex_lock_(mutex) \ - ((0 == pthread_mutex_lock((mutex))) ? MHD_YES : MHD_NO) -#elif defined(MHD_W32_MUTEX_) -/** - * Acquire lock on previously created mutex. - * If mutex was already locked by other thread, function - * blocks until mutex becomes available. - * @param mutex pointer to mutex - * @return #MHD_YES on success, #MHD_NO on failure - */ -#define MHD_mutex_lock_(mutex) \ - ((NULL != (mutex)) ? (EnterCriticalSection((mutex)), MHD_YES) : MHD_NO) -#endif - -#if defined(MHD_PTHREAD_MUTEX_) -/** - * Try to acquire lock on previously created mutex. - * Function returns immediately. - * @param mutex pointer to mutex - * @return #MHD_YES if mutex is locked, #MHD_NO if - * mutex was not locked. - */ -#define MHD_mutex_trylock_(mutex) \ - ((0 == pthread_mutex_trylock((mutex))) ? MHD_YES : MHD_NO) -#elif defined(MHD_W32_MUTEX_) -/** - * Try to acquire lock on previously created mutex. - * Function returns immediately. - * @param mutex pointer to mutex - * @return #MHD_YES if mutex is locked, #MHD_NO if - * mutex was not locked. - */ -#define MHD_mutex_trylock_(mutex) \ - ((NULL != (mutex) && 0 != TryEnterCriticalSection ((mutex))) ? MHD_YES : MHD_NO) -#endif - -#if defined(MHD_PTHREAD_MUTEX_) -/** - * Unlock previously created and locked mutex. - * @param mutex pointer to mutex - * @return #MHD_YES on success, #MHD_NO on failure - */ -#define MHD_mutex_unlock_(mutex) \ - ((0 == pthread_mutex_unlock((mutex))) ? MHD_YES : MHD_NO) -#elif defined(MHD_W32_MUTEX_) -/** - * Unlock previously created and locked mutex. - * @param mutex pointer to mutex - * @return #MHD_YES on success, #MHD_NO on failure - */ -#define MHD_mutex_unlock_(mutex) \ - ((NULL != (mutex)) ? (LeaveCriticalSection((mutex)), MHD_YES) : MHD_NO) -#endif #endif /* MHD_PLATFORM_INTERFACE_H */ diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am index e4b173ca..90324328 100644 --- a/src/microhttpd/Makefile.am +++ b/src/microhttpd/Makefile.am @@ -68,6 +68,7 @@ libmicrohttpd_la_SOURCES = \ sysfdsetsize.c sysfdsetsize.h \ mhd_str.c mhd_str.h \ mhd_threads.c mhd_threads.h \ + mhd_locks.h \ response.c response.h libmicrohttpd_la_CPPFLAGS = \ $(AM_CPPFLAGS) $(MHD_LIB_CPPFLAGS) \ diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index 55d7ba09..b3c43b63 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c @@ -31,19 +31,13 @@ #include "response.h" #include "mhd_mono_clock.h" #include "mhd_str.h" +#include "mhd_locks.h" #if HAVE_NETINET_TCP_H /* for TCP_CORK */ #include <netinet/tcp.h> #endif -#if defined(_WIN32) && defined(MHD_W32_MUTEX_) -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN 1 -#endif /* !WIN32_LEAN_AND_MEAN */ -#include <windows.h> -#endif /* _WIN32 && MHD_W32_MUTEX_ */ - /** * Message to transmit when http 1.1 request is received @@ -2412,7 +2406,7 @@ cleanup_connection (struct MHD_Connection *connection) } if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - if (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) + if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) MHD_PANIC ("Failed to acquire cleanup mutex\n"); } else @@ -2441,7 +2435,7 @@ cleanup_connection (struct MHD_Connection *connection) connection->resuming = MHD_NO; connection->in_idle = MHD_NO; if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_unlock_(&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_unlock_(&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to release cleanup mutex\n"); } diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index 4845d60a..7e13e311 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c @@ -33,6 +33,7 @@ #include "mhd_limits.h" #include "autoinit_funcs.h" #include "mhd_mono_clock.h" +#include "mhd_locks.h" #if HAVE_SEARCH_H #include <search.h> @@ -267,7 +268,7 @@ struct MHD_IPCount static void MHD_ip_count_lock (struct MHD_Daemon *daemon) { - if (MHD_YES != MHD_mutex_lock_(&daemon->per_ip_connection_mutex)) + if (!MHD_mutex_lock_(&daemon->per_ip_connection_mutex)) { MHD_PANIC ("Failed to acquire IP connection limit mutex\n"); } @@ -282,7 +283,7 @@ MHD_ip_count_lock (struct MHD_Daemon *daemon) static void MHD_ip_count_unlock (struct MHD_Daemon *daemon) { - if (MHD_YES != MHD_mutex_unlock_(&daemon->per_ip_connection_mutex)) + if (!MHD_mutex_unlock_(&daemon->per_ip_connection_mutex)) { MHD_PANIC ("Failed to release IP connection limit mutex\n"); } @@ -1536,7 +1537,7 @@ internal_add_connection (struct MHD_Daemon *daemon, if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - if (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) + if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) MHD_PANIC ("Failed to acquire cleanup mutex\n"); } else @@ -1547,7 +1548,7 @@ internal_add_connection (struct MHD_Daemon *daemon, daemon->connections_tail, connection); if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to release cleanup mutex\n"); if (NULL != daemon->notify_connection) @@ -1631,7 +1632,7 @@ internal_add_connection (struct MHD_Daemon *daemon, MHD_ip_limit_del (daemon, addr, addrlen); if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - if (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) + if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) MHD_PANIC ("Failed to acquire cleanup mutex\n"); } else @@ -1642,7 +1643,7 @@ internal_add_connection (struct MHD_Daemon *daemon, daemon->connections_tail, connection); if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to release cleanup mutex\n"); MHD_pool_destroy (connection->pool); free (connection->addr); @@ -1689,7 +1690,7 @@ MHD_suspend_connection (struct MHD_Connection *connection) MHD_PANIC ("Cannot suspend connections without enabling MHD_USE_SUSPEND_RESUME!\n"); if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) { - if (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) + if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) MHD_PANIC ("Failed to acquire cleanup mutex\n"); } else @@ -1733,7 +1734,7 @@ MHD_suspend_connection (struct MHD_Connection *connection) #endif connection->suspended = MHD_YES; if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to release cleanup mutex\n"); } @@ -1755,7 +1756,7 @@ MHD_resume_connection (struct MHD_Connection *connection) if (MHD_USE_SUSPEND_RESUME != (daemon->options & MHD_USE_SUSPEND_RESUME)) MHD_PANIC ("Cannot resume connections without enabling MHD_USE_SUSPEND_RESUME!\n"); if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to acquire cleanup mutex\n"); connection->resuming = MHD_YES; daemon->resuming = MHD_YES; @@ -1768,7 +1769,7 @@ MHD_resume_connection (struct MHD_Connection *connection) #endif } if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to release cleanup mutex\n"); } @@ -1789,7 +1790,7 @@ resume_suspended_connections (struct MHD_Daemon *daemon) ret = MHD_NO; if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to acquire cleanup mutex\n"); if (MHD_NO != daemon->resuming) next = daemon->suspended_connections_head; @@ -1845,7 +1846,7 @@ resume_suspended_connections (struct MHD_Daemon *daemon) pos->resuming = MHD_NO; } if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to release cleanup mutex\n"); return ret; } @@ -2069,7 +2070,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon) struct MHD_Connection *pos; if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to acquire cleanup mutex\n"); while (NULL != (pos = daemon->cleanup_head)) { @@ -2140,7 +2141,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon) free (pos); } if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to release cleanup mutex\n"); } @@ -3856,7 +3857,7 @@ MHD_start_daemon_va (unsigned int flags, } } - if (MHD_YES != MHD_mutex_create_ (&daemon->nnc_lock)) + if (!MHD_mutex_init_ (&daemon->nnc_lock)) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, @@ -4184,7 +4185,7 @@ MHD_start_daemon_va (unsigned int flags, } #endif - if (MHD_YES != MHD_mutex_create_ (&daemon->per_ip_connection_mutex)) + if (!MHD_mutex_init_ (&daemon->per_ip_connection_mutex)) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, @@ -4195,7 +4196,7 @@ MHD_start_daemon_va (unsigned int flags, MHD_PANIC ("close failed\n"); goto free_and_fail; } - if (MHD_YES != MHD_mutex_create_ (&daemon->cleanup_connection_mutex)) + if (!MHD_mutex_init_ (&daemon->cleanup_connection_mutex)) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, @@ -4331,7 +4332,7 @@ MHD_start_daemon_va (unsigned int flags, goto thread_failed; #endif /* Must init cleanup connection mutex for each worker */ - if (MHD_YES != MHD_mutex_create_ (&d->cleanup_connection_mutex)) + if (!MHD_mutex_init_ (&d->cleanup_connection_mutex)) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, @@ -4466,7 +4467,7 @@ close_all_connections (struct MHD_Daemon *daemon) /* first, make sure all threads are aware of shutdown; need to traverse DLLs in peace... */ if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to acquire cleanup mutex\n"); if (NULL != daemon->suspended_connections_head) MHD_PANIC ("MHD_stop_daemon() called while we have suspended connections.\n"); @@ -4481,7 +4482,7 @@ close_all_connections (struct MHD_Daemon *daemon) #endif } if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && - (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) + (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) MHD_PANIC ("Failed to release cleanup mutex\n"); /* now, collect per-connection threads */ @@ -4928,7 +4929,7 @@ gcry_w32_mutex_init (void **ppmtx) if (NULL == *ppmtx) return ENOMEM; - if (MHD_YES != MHD_mutex_create_ ((MHD_mutex_*)*ppmtx)) + if (!MHD_mutex_init_ ((MHD_mutex_*)*ppmtx)) { free (*ppmtx); *ppmtx = NULL; @@ -4942,7 +4943,7 @@ gcry_w32_mutex_init (void **ppmtx) static int gcry_w32_mutex_destroy (void **ppmtx) { - int res = (MHD_YES == MHD_mutex_destroy_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; + int res = (MHD_mutex_destroy_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; free (*ppmtx); return res; } @@ -4951,14 +4952,14 @@ gcry_w32_mutex_destroy (void **ppmtx) static int gcry_w32_mutex_lock (void **ppmtx) { - return (MHD_YES == MHD_mutex_lock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; + return (MHD_mutex_lock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; } static int gcry_w32_mutex_unlock (void **ppmtx) { - return (MHD_YES == MHD_mutex_unlock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; + return (MHD_mutex_unlock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; } diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h index aef6f17a..dd56de0c 100644 --- a/src/microhttpd/internal.h +++ b/src/microhttpd/internal.h @@ -44,6 +44,7 @@ #include <netinet/tcp.h> #endif #include "mhd_threads.h" +#include "mhd_locks.h" /** diff --git a/src/microhttpd/mhd_locks.h b/src/microhttpd/mhd_locks.h new file mode 100644 index 00000000..cf10c0d1 --- /dev/null +++ b/src/microhttpd/mhd_locks.h @@ -0,0 +1,150 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2016 Karlson2k (Evgeny Grin) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +/** + * @file microhttpd/mhd_locks.h + * @brief Header for platform-independent locks abstraction + * @author Karlson2k (Evgeny Grin) + * + * Provides basic abstraction for locks and mutex. + * Any functions can be implemented as macro on some platforms + * unless explicitly marked otherwise. + * Any function argument can be skipped in macro, so avoid + * variable modification in function parameters. + * + * @warning Unlike pthread functions, most of functions return + * nonzero on success. + */ + +#ifndef MHD_LOCKS_H +#define MHD_LOCKS_H 1 + +#include "mhd_options.h" + +#if defined(MHD_USE_W32_THREADS) +# define MHD_W32_MUTEX_ 1 +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN 1 +# endif /* !WIN32_LEAN_AND_MEAN */ +# include <windows.h> +#elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS) +# define MHD_PTHREAD_MUTEX_ 1 +# undef HAVE_CONFIG_H +# include <pthread.h> +# define HAVE_CONFIG_H 1 +#else +# error No base mutex API is available. +#endif + +#if defined(MHD_PTHREAD_MUTEX_) + typedef pthread_mutex_t MHD_mutex_; +#elif defined(MHD_W32_MUTEX_) + typedef CRITICAL_SECTION MHD_mutex_; +#endif + +#if defined(MHD_PTHREAD_MUTEX_) +/** + * Initialise new mutex. + * @param pmutex pointer to the mutex + * @return nonzero on success, zero otherwise + */ +#define MHD_mutex_init_(pmutex) (!(pthread_mutex_init((pmutex), NULL))) +#elif defined(MHD_W32_MUTEX_) +/** + * Initialise new mutex. + * @param pmutex pointer to mutex + * @return nonzero on success, zero otherwise + */ +#define MHD_mutex_init_(pmutex) (InitializeCriticalSectionAndSpinCount((pmutex),16)) +#endif + +#if defined(MHD_PTHREAD_MUTEX_) +/** + * Destroy previously initialised mutex. + * @param pmutex pointer to mutex + * @return nonzero on success, zero otherwise + */ +#define MHD_mutex_destroy_(pmutex) (!(pthread_mutex_destroy((pmutex)))) +#elif defined(MHD_W32_MUTEX_) +/** + * Destroy previously initialised mutex. + * @param pmutex pointer to mutex + * @return Always nonzero + */ +#define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection((pmutex)), !0) +#endif + +#if defined(MHD_PTHREAD_MUTEX_) +/** + * Acquire lock on previously initialised mutex. + * If mutex was already locked by other thread, function + * blocks until mutex becomes available. + * @param pmutex pointer to mutex + * @return nonzero on success, zero otherwise + */ +#define MHD_mutex_lock_(pmutex) (!(pthread_mutex_lock((pmutex)))) +#elif defined(MHD_W32_MUTEX_) +/** + * Acquire lock on previously initialised mutex. + * If mutex was already locked by other thread, function + * blocks until mutex becomes available. + * @param pmutex pointer to mutex + * @return Always nonzero + */ +#define MHD_mutex_lock_(pmutex) (EnterCriticalSection((pmutex)), !0) +#endif + +#if defined(MHD_PTHREAD_MUTEX_) +/** + * Try to acquire lock on previously initialised mutex. + * Function returns immediately. + * @param pmutex pointer to mutex + * @return nonzero if mutex is locked, zero if + * mutex was not locked. + */ +#define MHD_mutex_trylock_(pmutex) (!(pthread_mutex_trylock((pmutex)))) +#elif defined(MHD_W32_MUTEX_) +/** + * Try to acquire lock on previously initialised mutex. + * Function returns immediately. + * @param pmutex pointer to mutex + * @return nonzero if mutex is locked, zero if + * mutex was not locked. + */ +#define MHD_mutex_trylock_(pmutex) (TryEnterCriticalSection((pmutex)))) +#endif + +#if defined(MHD_PTHREAD_MUTEX_) +/** + * Unlock previously initialised and locked mutex. + * @param pmutex pointer to mutex + * @return nonzero on success, zero otherwise + */ +#define MHD_mutex_unlock_(pmutex) (!(pthread_mutex_unlock((pmutex)))) +#elif defined(MHD_W32_MUTEX_) +/** + * Unlock previously initialised and locked mutex. + * @param pmutex pointer to mutex + * @return Always nonzero + */ +#define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection((pmutex)), !0) +#endif + +#endif /* ! MHD_LOCKS_H */ diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c index 185cd2cd..3e967e68 100644 --- a/src/microhttpd/response.c +++ b/src/microhttpd/response.c @@ -257,7 +257,7 @@ MHD_create_response_from_callback (uint64_t size, response->fd = -1; response->data = (void *) &response[1]; response->data_buffer_size = block_size; - if (MHD_YES != MHD_mutex_create_ (&response->mutex)) + if (!MHD_mutex_init_ (&response->mutex)) { free (response); return NULL; @@ -513,7 +513,7 @@ MHD_create_response_from_data (size_t size, return NULL; memset (response, 0, sizeof (struct MHD_Response)); response->fd = -1; - if (MHD_YES != MHD_mutex_create_ (&response->mutex)) + if (!MHD_mutex_init_ (&response->mutex)) { free (response); return NULL; diff --git a/w32/common/libmicrohttpd-files.vcxproj b/w32/common/libmicrohttpd-files.vcxproj index be50c277..0fdfa2bd 100644 --- a/w32/common/libmicrohttpd-files.vcxproj +++ b/w32/common/libmicrohttpd-files.vcxproj @@ -39,6 +39,7 @@ <ClInclude Include="$(MhdSrc)microhttpd\sysfdsetsize.h" /> <ClInclude Include="$(MhdSrc)microhttpd\mhd_str.h" /> <ClInclude Include="$(MhdSrc)microhttpd\mhd_threads.h" /> + <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h" /> <ClInclude Include="$(MhdW32Common)MHD_config.h" /> </ItemGroup> <ItemGroup> diff --git a/w32/common/libmicrohttpd-filters.vcxproj b/w32/common/libmicrohttpd-filters.vcxproj index 85232458..6bb3dca7 100644 --- a/w32/common/libmicrohttpd-filters.vcxproj +++ b/w32/common/libmicrohttpd-filters.vcxproj @@ -136,6 +136,9 @@ <ClCompile Include="$(MhdSrc)microhttpd\mhd_threads.c"> <Filter>Source Files</Filter> </ClCompile> + <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h"> + <Filter>Source Files</Filter> + </ClInclude> </ItemGroup> <ItemGroup> <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc"> |