libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

commit c4f413682fb1423cd964a35d16cac9ff4584762b
parent ae5c45e446df498d237a8026eab0b8a1aa741b01
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Thu, 12 Jun 2025 22:48:11 +0200

mhd_locks: added W32 implementation based on SRW locks (and minor improvements)

Diffstat:
Msrc/mhd2/mhd_locks.h | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 74 insertions(+), 22 deletions(-)

diff --git a/src/mhd2/mhd_locks.h b/src/mhd2/mhd_locks.h @@ -41,23 +41,34 @@ #ifdef MHD_SUPPORT_THREADS -#if defined(mhd_THREADS_KIND_W32) -# define mhd_MUTEX_KIND_W32_CS 1 +#if defined(HAVE_PTHREAD_H) && defined(mhd_THREADS_KIND_POSIX) +/** + * The mutex is POSIX Threads' mutex + */ +# define mhd_MUTEX_KIND_PTHREAD 1 +# include <pthread.h> +# include "sys_null_macro.h" +#elif defined(mhd_THREADS_KIND_W32) +# include "sys_w32_ver.h" +# if _WIN32_WINNT >= 0x0600 /* Vista or later */ && \ + ! defined (MHD_NO_W32_SRWLOCKS) +/** + * The mutex is W32 SRW lock + */ +# define mhd_MUTEX_KIND_W32_SRW 1 +# else +/** + * The mutex is W32 Critical Section + */ +# define mhd_MUTEX_KIND_W32_CS 1 +# endif # if 0 /* _WIN32_WINNT >= 0x0602 */ /* Win8 or later */ - /* This include does not work as _ARM_ or _AMD64_ macros - are missing */ +/* This include does not work as _ARM_ or _AMD64_ macros + are missing */ # include <synchapi.h> # else # include <windows.h> # endif -#elif defined(HAVE_PTHREAD_H) && defined(mhd_THREADS_KIND_POSIX) -# define mhd_MUTEX_KIND_PTHREAD 1 -# include <pthread.h> -# ifdef HAVE_STDDEF_H -# include <stddef.h> /* for NULL */ -# else -# include "sys_base_types.h" -# endif #else #error No base mutex API is available. #endif @@ -66,6 +77,8 @@ #if defined(mhd_MUTEX_KIND_PTHREAD) typedef pthread_mutex_t mhd_mutex; +#elif defined(mhd_MUTEX_KIND_W32_SRW) +typedef SRWLOCK mhd_mutex; #elif defined(mhd_MUTEX_KIND_W32_CS) typedef CRITICAL_SECTION mhd_mutex; #endif @@ -76,7 +89,14 @@ typedef CRITICAL_SECTION mhd_mutex; * @param pmutex the pointer to the mutex * @return nonzero on success, zero otherwise */ -# define mhd_mutex_init(pmutex) (! (pthread_mutex_init ((pmutex), NULL))) +# define mhd_mutex_init(pmutex) (0 == pthread_mutex_init ((pmutex), NULL)) +#elif defined(mhd_MUTEX_KIND_W32_SRW) +/** + * Initialise a new mutex. + * @param pmutex the pointer to the mutex + * @return always nonzero (success) + */ +# define mhd_mutex_init(pmutex) (InitializeSRWLock ((pmutex)), ! 0) #elif defined(mhd_MUTEX_KIND_W32_CS) # if _WIN32_WINNT < 0x0600 /* Before Vista */ @@ -142,6 +162,13 @@ typedef CRITICAL_SECTION mhd_mutex; */ # define mhd_MUTEX_INITIALISER_STAT PTHREAD_MUTEX_INITIALIZER # endif /* PTHREAD_MUTEX_INITIALIZER */ +#elif defined(mhd_MUTEX_KIND_W32_SRW) +# if defined(SRWLOCK_INIT) +/** + * The value to statically initialise mutex + */ +# define mhd_MUTEX_INITIALISER_STAT SRWLOCK_INIT +# endif #endif #ifdef mhd_MUTEX_INITIALISER_STAT @@ -158,12 +185,19 @@ typedef CRITICAL_SECTION mhd_mutex; * @param pmutex the pointer to the mutex * @return nonzero on success, zero otherwise */ -# define mhd_mutex_destroy(pmutex) (! (pthread_mutex_destroy ((pmutex)))) +# define mhd_mutex_destroy(pmutex) (0 == pthread_mutex_destroy ((pmutex))) +#elif defined(mhd_MUTEX_KIND_W32_SRW) +/** + * Destroy (no-op) previously initialised mutex. + * @param pmutex the pointer to the mutex + * @return always nonzero (success) + */ +# define mhd_mutex_destroy(pmutex) ((void) (pmutex)) #elif defined(mhd_MUTEX_KIND_W32_CS) /** * Destroy previously initialised mutex. * @param pmutex the pointer to the mutex - * @return Always nonzero + * @return always nonzero (success) */ # define mhd_mutex_destroy(pmutex) (DeleteCriticalSection ((pmutex)), ! 0) #endif @@ -177,14 +211,23 @@ typedef CRITICAL_SECTION mhd_mutex; * @param pmutex the pointer to the mutex * @return nonzero on success, zero otherwise */ -# define mhd_mutex_lock(pmutex) (! (pthread_mutex_lock ((pmutex)))) +# define mhd_mutex_lock(pmutex) (0 == pthread_mutex_lock ((pmutex))) +#elif defined(mhd_MUTEX_KIND_W32_SRW) +/** + * Acquire a lock on previously initialised mutex. + * If the mutex was already locked by other thread, function blocks until + * the mutex becomes available. + * @param pmutex the pointer to the mutex + * @return always nonzero (success) + */ +# define mhd_mutex_lock(pmutex) (AcquireSRWLockExclusive ((pmutex)), ! 0) #elif defined(mhd_MUTEX_KIND_W32_CS) /** * Acquire a lock on previously initialised mutex. * If the mutex was already locked by other thread, function blocks until * the mutex becomes available. * @param pmutex the pointer to the mutex - * @return nonzero on success, zero otherwise + * @return always nonzero (success) */ # define mhd_mutex_lock(pmutex) (EnterCriticalSection ((pmutex)), ! 0) #endif @@ -195,12 +238,21 @@ typedef CRITICAL_SECTION mhd_mutex; * @param pmutex the pointer to the mutex * @return nonzero on success, zero otherwise */ -# define mhd_mutex_unlock(pmutex) (! (pthread_mutex_unlock ((pmutex)))) +# define mhd_mutex_unlock(pmutex) (0 == pthread_mutex_unlock ((pmutex))) +#elif defined(mhd_MUTEX_KIND_W32_SRW) +/** + * Acquire a lock on previously initialised mutex. + * If the mutex was already locked by other thread, function blocks until + * the mutex becomes available. + * @param pmutex the pointer to the mutex + * @return always nonzero (success) + */ +# define mhd_mutex_lock(pmutex) (ReleaseSRWLockExclusive ((pmutex)), ! 0) #elif defined(mhd_MUTEX_KIND_W32_CS) /** * Unlock previously initialised and locked mutex. * @param pmutex pointer to mutex - * @return Always nonzero + * @return always nonzero (success) */ # define mhd_mutex_unlock(pmutex) (LeaveCriticalSection ((pmutex)), ! 0) #endif @@ -243,11 +295,11 @@ typedef CRITICAL_SECTION mhd_mutex; # define mhd_MUTEX_INITIALISER_STAT /* empty */ # define mhd_MUTEX_STATIC_DEFN_INIT(ignored) /* nothing */ # define mhd_mutex_destroy(ignored) (! 0) -# define mhd_mutex_destroy_chk(ignored) (void) 0 +# define mhd_mutex_destroy_chk(ignored) ((void) 0) # define mhd_mutex_lock(ignored) (! 0) -# define mhd_mutex_lock_chk(ignored) (void) 0 +# define mhd_mutex_lock_chk(ignored) ((void) 0) # define mhd_mutex_unlock(ignored) (! 0) -# define mhd_mutex_unlock_chk(ignored) (void) 0 +# define mhd_mutex_unlock_chk(ignored) ((void) 0) #endif /* ! MHD_SUPPORT_THREADS */