| 1 |
/* |
| 2 |
This file is part of libmicrohttpd |
| 3 |
Copyright (C) 2016-2022 Karlson2k (Evgeny Grin) |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Lesser General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2.1 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public |
| 16 |
License along with this library; if not, write to the Free Software |
| 17 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
|
| 19 |
*/ |
| 20 |
|
| 21 |
/** |
| 22 |
* @file microhttpd/mhd_locks.h |
| 23 |
* @brief Header for platform-independent locks abstraction |
| 24 |
* @author Karlson2k (Evgeny Grin) |
| 25 |
* @author Christian Grothoff |
| 26 |
* |
| 27 |
* Provides basic abstraction for locks/mutex. |
| 28 |
* Any functions can be implemented as macro on some platforms |
| 29 |
* unless explicitly marked otherwise. |
| 30 |
* Any function argument can be skipped in macro, so avoid |
| 31 |
* variable modification in function parameters. |
| 32 |
* |
| 33 |
* @warning Unlike pthread functions, most of functions return |
| 34 |
* nonzero on success. |
| 35 |
*/ |
| 36 |
|
| 37 |
#ifndef MHD_LOCKS_H |
| 38 |
#define MHD_LOCKS_H 1 |
| 39 |
|
| 40 |
#include "mhd_options.h" |
| 41 |
|
| 42 |
#ifdef MHD_USE_THREADS |
| 43 |
|
| 44 |
#if defined(MHD_USE_W32_THREADS) |
| 45 |
# define MHD_W32_MUTEX_ 1 |
| 46 |
# ifndef WIN32_LEAN_AND_MEAN |
| 47 |
# define WIN32_LEAN_AND_MEAN 1 |
| 48 |
# endif /* !WIN32_LEAN_AND_MEAN */ |
| 49 |
# include <windows.h> |
| 50 |
#elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS) |
| 51 |
# define MHD_PTHREAD_MUTEX_ 1 |
| 52 |
# undef HAVE_CONFIG_H |
| 53 |
# include <pthread.h> |
| 54 |
# define HAVE_CONFIG_H 1 |
| 55 |
#else |
| 56 |
# error No base mutex API is available. |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef MHD_PANIC |
| 60 |
# include <stdio.h> |
| 61 |
# ifdef HAVE_STDLIB_H |
| 62 |
# include <stdlib.h> |
| 63 |
# endif /* HAVE_STDLIB_H */ |
| 64 |
/* Simple implementation of MHD_PANIC, to be used outside lib */ |
| 65 |
# define MHD_PANIC(msg) \ |
| 66 |
do { fprintf (stderr, \ |
| 67 |
"Abnormal termination at %d line in file %s: %s\n", \ |
| 68 |
(int) __LINE__, __FILE__, msg); abort (); \ |
| 69 |
} while (0) |
| 70 |
#endif /* ! MHD_PANIC */ |
| 71 |
|
| 72 |
#if defined(MHD_PTHREAD_MUTEX_) |
| 73 |
typedef pthread_mutex_t MHD_mutex_; |
| 74 |
#elif defined(MHD_W32_MUTEX_) |
| 75 |
typedef CRITICAL_SECTION MHD_mutex_; |
| 76 |
#endif |
| 77 |
|
| 78 |
#if defined(MHD_PTHREAD_MUTEX_) |
| 79 |
/** |
| 80 |
* Initialise new mutex. |
| 81 |
* @param pmutex pointer to the mutex |
| 82 |
* @return nonzero on success, zero otherwise |
| 83 |
*/ |
| 84 |
#define MHD_mutex_init_(pmutex) (! (pthread_mutex_init ((pmutex), NULL))) |
| 85 |
#elif defined(MHD_W32_MUTEX_) |
| 86 |
/** |
| 87 |
* Initialise new mutex. |
| 88 |
* @param pmutex pointer to mutex |
| 89 |
* @return nonzero on success, zero otherwise |
| 90 |
*/ |
| 91 |
#define MHD_mutex_init_(pmutex) \ |
| 92 |
(InitializeCriticalSectionAndSpinCount ((pmutex),16)) |
| 93 |
#endif |
| 94 |
|
| 95 |
#if defined(MHD_PTHREAD_MUTEX_) |
| 96 |
# if defined(PTHREAD_MUTEX_INITIALIZER) |
| 97 |
/** |
| 98 |
* Define static mutex and statically initialise it. |
| 99 |
*/ |
| 100 |
# define MHD_MUTEX_STATIC_DEFN_INIT_(m) \ |
| 101 |
static MHD_mutex_ m = PTHREAD_MUTEX_INITIALIZER |
| 102 |
# endif /* PTHREAD_MUTEX_INITIALIZER */ |
| 103 |
#endif |
| 104 |
|
| 105 |
#if defined(MHD_PTHREAD_MUTEX_) |
| 106 |
/** |
| 107 |
* Destroy previously initialised mutex. |
| 108 |
* @param pmutex pointer to mutex |
| 109 |
* @return nonzero on success, zero otherwise |
| 110 |
*/ |
| 111 |
#define MHD_mutex_destroy_(pmutex) (! (pthread_mutex_destroy ((pmutex)))) |
| 112 |
#elif defined(MHD_W32_MUTEX_) |
| 113 |
/** |
| 114 |
* Destroy previously initialised mutex. |
| 115 |
* @param pmutex pointer to mutex |
| 116 |
* @return Always nonzero |
| 117 |
*/ |
| 118 |
#define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection ((pmutex)), ! 0) |
| 119 |
#endif |
| 120 |
|
| 121 |
/** |
| 122 |
* Destroy previously initialised mutex and abort execution |
| 123 |
* if error is detected. |
| 124 |
* @param pmutex pointer to mutex |
| 125 |
*/ |
| 126 |
#define MHD_mutex_destroy_chk_(pmutex) do { \ |
| 127 |
if (! MHD_mutex_destroy_ (pmutex)) \ |
| 128 |
MHD_PANIC (_ ("Failed to destroy mutex.\n")); \ |
| 129 |
} while (0) |
| 130 |
|
| 131 |
|
| 132 |
#if defined(MHD_PTHREAD_MUTEX_) |
| 133 |
/** |
| 134 |
* Acquire lock on previously initialised mutex. |
| 135 |
* If mutex was already locked by other thread, function |
| 136 |
* blocks until mutex becomes available. |
| 137 |
* @param pmutex pointer to mutex |
| 138 |
* @return nonzero on success, zero otherwise |
| 139 |
*/ |
| 140 |
#define MHD_mutex_lock_(pmutex) (! (pthread_mutex_lock ((pmutex)))) |
| 141 |
#elif defined(MHD_W32_MUTEX_) |
| 142 |
/** |
| 143 |
* Acquire lock on previously initialised mutex. |
| 144 |
* If mutex was already locked by other thread, function |
| 145 |
* blocks until mutex becomes available. |
| 146 |
* @param pmutex pointer to mutex |
| 147 |
* @return Always nonzero |
| 148 |
*/ |
| 149 |
#define MHD_mutex_lock_(pmutex) (EnterCriticalSection ((pmutex)), ! 0) |
| 150 |
#endif |
| 151 |
|
| 152 |
/** |
| 153 |
* Acquire lock on previously initialised mutex. |
| 154 |
* If mutex was already locked by other thread, function |
| 155 |
* blocks until mutex becomes available. |
| 156 |
* If error is detected, execution will be aborted. |
| 157 |
* @param pmutex pointer to mutex |
| 158 |
*/ |
| 159 |
#define MHD_mutex_lock_chk_(pmutex) do { \ |
| 160 |
if (! MHD_mutex_lock_ (pmutex)) \ |
| 161 |
MHD_PANIC (_ ("Failed to lock mutex.\n")); \ |
| 162 |
} while (0) |
| 163 |
|
| 164 |
#if defined(MHD_PTHREAD_MUTEX_) |
| 165 |
/** |
| 166 |
* Unlock previously initialised and locked mutex. |
| 167 |
* @param pmutex pointer to mutex |
| 168 |
* @return nonzero on success, zero otherwise |
| 169 |
*/ |
| 170 |
#define MHD_mutex_unlock_(pmutex) (! (pthread_mutex_unlock ((pmutex)))) |
| 171 |
#elif defined(MHD_W32_MUTEX_) |
| 172 |
/** |
| 173 |
* Unlock previously initialised and locked mutex. |
| 174 |
* @param pmutex pointer to mutex |
| 175 |
* @return Always nonzero |
| 176 |
*/ |
| 177 |
#define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection ((pmutex)), ! 0) |
| 178 |
#endif |
| 179 |
|
| 180 |
/** |
| 181 |
* Unlock previously initialised and locked mutex. |
| 182 |
* If error is detected, execution will be aborted. |
| 183 |
* @param pmutex pointer to mutex |
| 184 |
*/ |
| 185 |
#define MHD_mutex_unlock_chk_(pmutex) do { \ |
| 186 |
if (! MHD_mutex_unlock_ (pmutex)) \ |
| 187 |
MHD_PANIC (_ ("Failed to unlock mutex.\n")); \ |
| 188 |
} while (0) |
| 189 |
|
| 190 |
#else /* ! MHD_USE_THREADS */ |
| 191 |
|
| 192 |
#define MHD_mutex_init_(ignore) (! 0) |
| 193 |
#define MHD_mutex_destroy_(ignore) (! 0) |
| 194 |
#define MHD_mutex_destroy_chk_(ignore) (void)0 |
| 195 |
#define MHD_mutex_lock_(ignore) (! 0) |
| 196 |
#define MHD_mutex_lock_chk_(ignore) (void)0 |
| 197 |
#define MHD_mutex_unlock_(ignore) (! 0) |
| 198 |
#define MHD_mutex_unlock_chk_(ignore) (void)0 |
| 199 |
|
| 200 |
#endif /* ! MHD_USE_THREADS */ |
| 201 |
|
| 202 |
#endif /* ! MHD_LOCKS_H */ |