aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-10 13:52:51 +0000
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2016-08-10 13:52:51 +0000
commit40b07cc6451ce9b0c36f353385b777c4ebfcb138 (patch)
tree1c2262c387dd01c6672f51628b8a0c74cae18199
parenta7029c10cccdf2b0d4597eb79fb58d2f11d167f1 (diff)
downloadlibmicrohttpd-40b07cc6451ce9b0c36f353385b777c4ebfcb138.tar.gz
libmicrohttpd-40b07cc6451ce9b0c36f353385b777c4ebfcb138.zip
Moved locks and mutex abstraction to mhd_locks.h
Minor refactoring to allow better code optimization.
-rw-r--r--src/include/platform_interface.h108
-rw-r--r--src/microhttpd/Makefile.am1
-rw-r--r--src/microhttpd/connection.c12
-rw-r--r--src/microhttpd/daemon.c49
-rw-r--r--src/microhttpd/internal.h1
-rw-r--r--src/microhttpd/mhd_locks.h150
-rw-r--r--src/microhttpd/response.c4
-rw-r--r--w32/common/libmicrohttpd-files.vcxproj1
-rw-r--r--w32/common/libmicrohttpd-filters.vcxproj3
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;
189#else 189#else
190#define MHD_random_() MHD_W32_random_() 190#define MHD_random_() MHD_W32_random_()
191#endif 191#endif
192#if defined(MHD_USE_W32_THREADS)
193#define MHD_W32_MUTEX_ 1
194#include <windows.h>
195typedef CRITICAL_SECTION MHD_mutex_;
196#elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS)
197#define MHD_PTHREAD_MUTEX_ 1
198typedef pthread_mutex_t MHD_mutex_;
199#else
200#error "No base mutex API is available."
201#endif
202
203#if defined(MHD_PTHREAD_MUTEX_)
204/**
205 * Create new mutex.
206 * @param mutex pointer to the mutex
207 * @return #MHD_YES on success, #MHD_NO on failure
208 */
209#define MHD_mutex_create_(mutex) \
210 ((0 == pthread_mutex_init ((mutex), NULL)) ? MHD_YES : MHD_NO)
211#elif defined(MHD_W32_MUTEX_)
212/**
213 * Create new mutex.
214 * @param mutex pointer to mutex
215 * @return #MHD_YES on success, #MHD_NO on failure
216 */
217#define MHD_mutex_create_(mutex) \
218 ((NULL != (mutex) && 0 != InitializeCriticalSectionAndSpinCount((mutex),2000)) ? MHD_YES : MHD_NO)
219#endif
220
221#if defined(MHD_PTHREAD_MUTEX_)
222/**
223 * Destroy previously created mutex.
224 * @param mutex pointer to mutex
225 * @return #MHD_YES on success, #MHD_NO on failure
226 */
227#define MHD_mutex_destroy_(mutex) \
228 ((0 == pthread_mutex_destroy ((mutex))) ? MHD_YES : MHD_NO)
229#elif defined(MHD_W32_MUTEX_)
230/**
231 * Destroy previously created mutex.
232 * @param mutex pointer to mutex
233 * @return #MHD_YES on success, #MHD_NO on failure
234 */
235#define MHD_mutex_destroy_(mutex) \
236 ((NULL != (mutex)) ? (DeleteCriticalSection(mutex), MHD_YES) : MHD_NO)
237#endif
238
239#if defined(MHD_PTHREAD_MUTEX_)
240/**
241 * Acquire lock on previously created mutex.
242 * If mutex was already locked by other thread, function
243 * blocks until mutex becomes available.
244 * @param mutex pointer to mutex
245 * @return #MHD_YES on success, #MHD_NO on failure
246 */
247#define MHD_mutex_lock_(mutex) \
248 ((0 == pthread_mutex_lock((mutex))) ? MHD_YES : MHD_NO)
249#elif defined(MHD_W32_MUTEX_)
250/**
251 * Acquire lock on previously created mutex.
252 * If mutex was already locked by other thread, function
253 * blocks until mutex becomes available.
254 * @param mutex pointer to mutex
255 * @return #MHD_YES on success, #MHD_NO on failure
256 */
257#define MHD_mutex_lock_(mutex) \
258 ((NULL != (mutex)) ? (EnterCriticalSection((mutex)), MHD_YES) : MHD_NO)
259#endif
260
261#if defined(MHD_PTHREAD_MUTEX_)
262/**
263 * Try to acquire lock on previously created mutex.
264 * Function returns immediately.
265 * @param mutex pointer to mutex
266 * @return #MHD_YES if mutex is locked, #MHD_NO if
267 * mutex was not locked.
268 */
269#define MHD_mutex_trylock_(mutex) \
270 ((0 == pthread_mutex_trylock((mutex))) ? MHD_YES : MHD_NO)
271#elif defined(MHD_W32_MUTEX_)
272/**
273 * Try to acquire lock on previously created mutex.
274 * Function returns immediately.
275 * @param mutex pointer to mutex
276 * @return #MHD_YES if mutex is locked, #MHD_NO if
277 * mutex was not locked.
278 */
279#define MHD_mutex_trylock_(mutex) \
280 ((NULL != (mutex) && 0 != TryEnterCriticalSection ((mutex))) ? MHD_YES : MHD_NO)
281#endif
282
283#if defined(MHD_PTHREAD_MUTEX_)
284/**
285 * Unlock previously created and locked mutex.
286 * @param mutex pointer to mutex
287 * @return #MHD_YES on success, #MHD_NO on failure
288 */
289#define MHD_mutex_unlock_(mutex) \
290 ((0 == pthread_mutex_unlock((mutex))) ? MHD_YES : MHD_NO)
291#elif defined(MHD_W32_MUTEX_)
292/**
293 * Unlock previously created and locked mutex.
294 * @param mutex pointer to mutex
295 * @return #MHD_YES on success, #MHD_NO on failure
296 */
297#define MHD_mutex_unlock_(mutex) \
298 ((NULL != (mutex)) ? (LeaveCriticalSection((mutex)), MHD_YES) : MHD_NO)
299#endif
300 192
301#endif /* MHD_PLATFORM_INTERFACE_H */ 193#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 = \
68 sysfdsetsize.c sysfdsetsize.h \ 68 sysfdsetsize.c sysfdsetsize.h \
69 mhd_str.c mhd_str.h \ 69 mhd_str.c mhd_str.h \
70 mhd_threads.c mhd_threads.h \ 70 mhd_threads.c mhd_threads.h \
71 mhd_locks.h \
71 response.c response.h 72 response.c response.h
72libmicrohttpd_la_CPPFLAGS = \ 73libmicrohttpd_la_CPPFLAGS = \
73 $(AM_CPPFLAGS) $(MHD_LIB_CPPFLAGS) \ 74 $(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 @@
31#include "response.h" 31#include "response.h"
32#include "mhd_mono_clock.h" 32#include "mhd_mono_clock.h"
33#include "mhd_str.h" 33#include "mhd_str.h"
34#include "mhd_locks.h"
34 35
35#if HAVE_NETINET_TCP_H 36#if HAVE_NETINET_TCP_H
36/* for TCP_CORK */ 37/* for TCP_CORK */
37#include <netinet/tcp.h> 38#include <netinet/tcp.h>
38#endif 39#endif
39 40
40#if defined(_WIN32) && defined(MHD_W32_MUTEX_)
41#ifndef WIN32_LEAN_AND_MEAN
42#define WIN32_LEAN_AND_MEAN 1
43#endif /* !WIN32_LEAN_AND_MEAN */
44#include <windows.h>
45#endif /* _WIN32 && MHD_W32_MUTEX_ */
46
47 41
48/** 42/**
49 * Message to transmit when http 1.1 request is received 43 * Message to transmit when http 1.1 request is received
@@ -2412,7 +2406,7 @@ cleanup_connection (struct MHD_Connection *connection)
2412 } 2406 }
2413 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 2407 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
2414 { 2408 {
2415 if (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) 2409 if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex))
2416 MHD_PANIC ("Failed to acquire cleanup mutex\n"); 2410 MHD_PANIC ("Failed to acquire cleanup mutex\n");
2417 } 2411 }
2418 else 2412 else
@@ -2441,7 +2435,7 @@ cleanup_connection (struct MHD_Connection *connection)
2441 connection->resuming = MHD_NO; 2435 connection->resuming = MHD_NO;
2442 connection->in_idle = MHD_NO; 2436 connection->in_idle = MHD_NO;
2443 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 2437 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
2444 (MHD_YES != MHD_mutex_unlock_(&daemon->cleanup_connection_mutex)) ) 2438 (!MHD_mutex_unlock_(&daemon->cleanup_connection_mutex)) )
2445 MHD_PANIC ("Failed to release cleanup mutex\n"); 2439 MHD_PANIC ("Failed to release cleanup mutex\n");
2446} 2440}
2447 2441
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 @@
33#include "mhd_limits.h" 33#include "mhd_limits.h"
34#include "autoinit_funcs.h" 34#include "autoinit_funcs.h"
35#include "mhd_mono_clock.h" 35#include "mhd_mono_clock.h"
36#include "mhd_locks.h"
36 37
37#if HAVE_SEARCH_H 38#if HAVE_SEARCH_H
38#include <search.h> 39#include <search.h>
@@ -267,7 +268,7 @@ struct MHD_IPCount
267static void 268static void
268MHD_ip_count_lock (struct MHD_Daemon *daemon) 269MHD_ip_count_lock (struct MHD_Daemon *daemon)
269{ 270{
270 if (MHD_YES != MHD_mutex_lock_(&daemon->per_ip_connection_mutex)) 271 if (!MHD_mutex_lock_(&daemon->per_ip_connection_mutex))
271 { 272 {
272 MHD_PANIC ("Failed to acquire IP connection limit mutex\n"); 273 MHD_PANIC ("Failed to acquire IP connection limit mutex\n");
273 } 274 }
@@ -282,7 +283,7 @@ MHD_ip_count_lock (struct MHD_Daemon *daemon)
282static void 283static void
283MHD_ip_count_unlock (struct MHD_Daemon *daemon) 284MHD_ip_count_unlock (struct MHD_Daemon *daemon)
284{ 285{
285 if (MHD_YES != MHD_mutex_unlock_(&daemon->per_ip_connection_mutex)) 286 if (!MHD_mutex_unlock_(&daemon->per_ip_connection_mutex))
286 { 287 {
287 MHD_PANIC ("Failed to release IP connection limit mutex\n"); 288 MHD_PANIC ("Failed to release IP connection limit mutex\n");
288 } 289 }
@@ -1536,7 +1537,7 @@ internal_add_connection (struct MHD_Daemon *daemon,
1536 1537
1537 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 1538 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
1538 { 1539 {
1539 if (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) 1540 if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex))
1540 MHD_PANIC ("Failed to acquire cleanup mutex\n"); 1541 MHD_PANIC ("Failed to acquire cleanup mutex\n");
1541 } 1542 }
1542 else 1543 else
@@ -1547,7 +1548,7 @@ internal_add_connection (struct MHD_Daemon *daemon,
1547 daemon->connections_tail, 1548 daemon->connections_tail,
1548 connection); 1549 connection);
1549 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 1550 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
1550 (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) 1551 (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) )
1551 MHD_PANIC ("Failed to release cleanup mutex\n"); 1552 MHD_PANIC ("Failed to release cleanup mutex\n");
1552 1553
1553 if (NULL != daemon->notify_connection) 1554 if (NULL != daemon->notify_connection)
@@ -1631,7 +1632,7 @@ internal_add_connection (struct MHD_Daemon *daemon,
1631 MHD_ip_limit_del (daemon, addr, addrlen); 1632 MHD_ip_limit_del (daemon, addr, addrlen);
1632 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 1633 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
1633 { 1634 {
1634 if (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) 1635 if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex))
1635 MHD_PANIC ("Failed to acquire cleanup mutex\n"); 1636 MHD_PANIC ("Failed to acquire cleanup mutex\n");
1636 } 1637 }
1637 else 1638 else
@@ -1642,7 +1643,7 @@ internal_add_connection (struct MHD_Daemon *daemon,
1642 daemon->connections_tail, 1643 daemon->connections_tail,
1643 connection); 1644 connection);
1644 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 1645 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
1645 (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) 1646 (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) )
1646 MHD_PANIC ("Failed to release cleanup mutex\n"); 1647 MHD_PANIC ("Failed to release cleanup mutex\n");
1647 MHD_pool_destroy (connection->pool); 1648 MHD_pool_destroy (connection->pool);
1648 free (connection->addr); 1649 free (connection->addr);
@@ -1689,7 +1690,7 @@ MHD_suspend_connection (struct MHD_Connection *connection)
1689 MHD_PANIC ("Cannot suspend connections without enabling MHD_USE_SUSPEND_RESUME!\n"); 1690 MHD_PANIC ("Cannot suspend connections without enabling MHD_USE_SUSPEND_RESUME!\n");
1690 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 1691 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
1691 { 1692 {
1692 if (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) 1693 if (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex))
1693 MHD_PANIC ("Failed to acquire cleanup mutex\n"); 1694 MHD_PANIC ("Failed to acquire cleanup mutex\n");
1694 } 1695 }
1695 else 1696 else
@@ -1733,7 +1734,7 @@ MHD_suspend_connection (struct MHD_Connection *connection)
1733#endif 1734#endif
1734 connection->suspended = MHD_YES; 1735 connection->suspended = MHD_YES;
1735 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 1736 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
1736 (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) 1737 (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) )
1737 MHD_PANIC ("Failed to release cleanup mutex\n"); 1738 MHD_PANIC ("Failed to release cleanup mutex\n");
1738} 1739}
1739 1740
@@ -1755,7 +1756,7 @@ MHD_resume_connection (struct MHD_Connection *connection)
1755 if (MHD_USE_SUSPEND_RESUME != (daemon->options & MHD_USE_SUSPEND_RESUME)) 1756 if (MHD_USE_SUSPEND_RESUME != (daemon->options & MHD_USE_SUSPEND_RESUME))
1756 MHD_PANIC ("Cannot resume connections without enabling MHD_USE_SUSPEND_RESUME!\n"); 1757 MHD_PANIC ("Cannot resume connections without enabling MHD_USE_SUSPEND_RESUME!\n");
1757 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 1758 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
1758 (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) 1759 (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) )
1759 MHD_PANIC ("Failed to acquire cleanup mutex\n"); 1760 MHD_PANIC ("Failed to acquire cleanup mutex\n");
1760 connection->resuming = MHD_YES; 1761 connection->resuming = MHD_YES;
1761 daemon->resuming = MHD_YES; 1762 daemon->resuming = MHD_YES;
@@ -1768,7 +1769,7 @@ MHD_resume_connection (struct MHD_Connection *connection)
1768#endif 1769#endif
1769 } 1770 }
1770 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 1771 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
1771 (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) 1772 (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) )
1772 MHD_PANIC ("Failed to release cleanup mutex\n"); 1773 MHD_PANIC ("Failed to release cleanup mutex\n");
1773} 1774}
1774 1775
@@ -1789,7 +1790,7 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
1789 1790
1790 ret = MHD_NO; 1791 ret = MHD_NO;
1791 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 1792 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
1792 (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) 1793 (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) )
1793 MHD_PANIC ("Failed to acquire cleanup mutex\n"); 1794 MHD_PANIC ("Failed to acquire cleanup mutex\n");
1794 if (MHD_NO != daemon->resuming) 1795 if (MHD_NO != daemon->resuming)
1795 next = daemon->suspended_connections_head; 1796 next = daemon->suspended_connections_head;
@@ -1845,7 +1846,7 @@ resume_suspended_connections (struct MHD_Daemon *daemon)
1845 pos->resuming = MHD_NO; 1846 pos->resuming = MHD_NO;
1846 } 1847 }
1847 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 1848 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
1848 (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) 1849 (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) )
1849 MHD_PANIC ("Failed to release cleanup mutex\n"); 1850 MHD_PANIC ("Failed to release cleanup mutex\n");
1850 return ret; 1851 return ret;
1851} 1852}
@@ -2069,7 +2070,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
2069 struct MHD_Connection *pos; 2070 struct MHD_Connection *pos;
2070 2071
2071 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 2072 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
2072 (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) 2073 (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) )
2073 MHD_PANIC ("Failed to acquire cleanup mutex\n"); 2074 MHD_PANIC ("Failed to acquire cleanup mutex\n");
2074 while (NULL != (pos = daemon->cleanup_head)) 2075 while (NULL != (pos = daemon->cleanup_head))
2075 { 2076 {
@@ -2140,7 +2141,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
2140 free (pos); 2141 free (pos);
2141 } 2142 }
2142 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 2143 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
2143 (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) 2144 (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) )
2144 MHD_PANIC ("Failed to release cleanup mutex\n"); 2145 MHD_PANIC ("Failed to release cleanup mutex\n");
2145} 2146}
2146 2147
@@ -3856,7 +3857,7 @@ MHD_start_daemon_va (unsigned int flags,
3856 } 3857 }
3857 } 3858 }
3858 3859
3859 if (MHD_YES != MHD_mutex_create_ (&daemon->nnc_lock)) 3860 if (!MHD_mutex_init_ (&daemon->nnc_lock))
3860 { 3861 {
3861#ifdef HAVE_MESSAGES 3862#ifdef HAVE_MESSAGES
3862 MHD_DLOG (daemon, 3863 MHD_DLOG (daemon,
@@ -4184,7 +4185,7 @@ MHD_start_daemon_va (unsigned int flags,
4184 } 4185 }
4185#endif 4186#endif
4186 4187
4187 if (MHD_YES != MHD_mutex_create_ (&daemon->per_ip_connection_mutex)) 4188 if (!MHD_mutex_init_ (&daemon->per_ip_connection_mutex))
4188 { 4189 {
4189#ifdef HAVE_MESSAGES 4190#ifdef HAVE_MESSAGES
4190 MHD_DLOG (daemon, 4191 MHD_DLOG (daemon,
@@ -4195,7 +4196,7 @@ MHD_start_daemon_va (unsigned int flags,
4195 MHD_PANIC ("close failed\n"); 4196 MHD_PANIC ("close failed\n");
4196 goto free_and_fail; 4197 goto free_and_fail;
4197 } 4198 }
4198 if (MHD_YES != MHD_mutex_create_ (&daemon->cleanup_connection_mutex)) 4199 if (!MHD_mutex_init_ (&daemon->cleanup_connection_mutex))
4199 { 4200 {
4200#ifdef HAVE_MESSAGES 4201#ifdef HAVE_MESSAGES
4201 MHD_DLOG (daemon, 4202 MHD_DLOG (daemon,
@@ -4331,7 +4332,7 @@ MHD_start_daemon_va (unsigned int flags,
4331 goto thread_failed; 4332 goto thread_failed;
4332#endif 4333#endif
4333 /* Must init cleanup connection mutex for each worker */ 4334 /* Must init cleanup connection mutex for each worker */
4334 if (MHD_YES != MHD_mutex_create_ (&d->cleanup_connection_mutex)) 4335 if (!MHD_mutex_init_ (&d->cleanup_connection_mutex))
4335 { 4336 {
4336#ifdef HAVE_MESSAGES 4337#ifdef HAVE_MESSAGES
4337 MHD_DLOG (daemon, 4338 MHD_DLOG (daemon,
@@ -4466,7 +4467,7 @@ close_all_connections (struct MHD_Daemon *daemon)
4466 /* first, make sure all threads are aware of shutdown; need to 4467 /* first, make sure all threads are aware of shutdown; need to
4467 traverse DLLs in peace... */ 4468 traverse DLLs in peace... */
4468 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 4469 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
4469 (MHD_YES != MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) ) 4470 (!MHD_mutex_lock_ (&daemon->cleanup_connection_mutex)) )
4470 MHD_PANIC ("Failed to acquire cleanup mutex\n"); 4471 MHD_PANIC ("Failed to acquire cleanup mutex\n");
4471 if (NULL != daemon->suspended_connections_head) 4472 if (NULL != daemon->suspended_connections_head)
4472 MHD_PANIC ("MHD_stop_daemon() called while we have suspended connections.\n"); 4473 MHD_PANIC ("MHD_stop_daemon() called while we have suspended connections.\n");
@@ -4481,7 +4482,7 @@ close_all_connections (struct MHD_Daemon *daemon)
4481#endif 4482#endif
4482 } 4483 }
4483 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && 4484 if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) &&
4484 (MHD_YES != MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) ) 4485 (!MHD_mutex_unlock_ (&daemon->cleanup_connection_mutex)) )
4485 MHD_PANIC ("Failed to release cleanup mutex\n"); 4486 MHD_PANIC ("Failed to release cleanup mutex\n");
4486 4487
4487 /* now, collect per-connection threads */ 4488 /* now, collect per-connection threads */
@@ -4928,7 +4929,7 @@ gcry_w32_mutex_init (void **ppmtx)
4928 4929
4929 if (NULL == *ppmtx) 4930 if (NULL == *ppmtx)
4930 return ENOMEM; 4931 return ENOMEM;
4931 if (MHD_YES != MHD_mutex_create_ ((MHD_mutex_*)*ppmtx)) 4932 if (!MHD_mutex_init_ ((MHD_mutex_*)*ppmtx))
4932 { 4933 {
4933 free (*ppmtx); 4934 free (*ppmtx);
4934 *ppmtx = NULL; 4935 *ppmtx = NULL;
@@ -4942,7 +4943,7 @@ gcry_w32_mutex_init (void **ppmtx)
4942static int 4943static int
4943gcry_w32_mutex_destroy (void **ppmtx) 4944gcry_w32_mutex_destroy (void **ppmtx)
4944{ 4945{
4945 int res = (MHD_YES == MHD_mutex_destroy_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; 4946 int res = (MHD_mutex_destroy_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1;
4946 free (*ppmtx); 4947 free (*ppmtx);
4947 return res; 4948 return res;
4948} 4949}
@@ -4951,14 +4952,14 @@ gcry_w32_mutex_destroy (void **ppmtx)
4951static int 4952static int
4952gcry_w32_mutex_lock (void **ppmtx) 4953gcry_w32_mutex_lock (void **ppmtx)
4953{ 4954{
4954 return (MHD_YES == MHD_mutex_lock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; 4955 return (MHD_mutex_lock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1;
4955} 4956}
4956 4957
4957 4958
4958static int 4959static int
4959gcry_w32_mutex_unlock (void **ppmtx) 4960gcry_w32_mutex_unlock (void **ppmtx)
4960{ 4961{
4961 return (MHD_YES == MHD_mutex_unlock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1; 4962 return (MHD_mutex_unlock_ ((MHD_mutex_*)*ppmtx)) ? 0 : 1;
4962} 4963}
4963 4964
4964 4965
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 @@
44#include <netinet/tcp.h> 44#include <netinet/tcp.h>
45#endif 45#endif
46#include "mhd_threads.h" 46#include "mhd_threads.h"
47#include "mhd_locks.h"
47 48
48 49
49/** 50/**
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 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2016 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 *
26 * Provides basic abstraction for locks and mutex.
27 * Any functions can be implemented as macro on some platforms
28 * unless explicitly marked otherwise.
29 * Any function argument can be skipped in macro, so avoid
30 * variable modification in function parameters.
31 *
32 * @warning Unlike pthread functions, most of functions return
33 * nonzero on success.
34 */
35
36#ifndef MHD_LOCKS_H
37#define MHD_LOCKS_H 1
38
39#include "mhd_options.h"
40
41#if defined(MHD_USE_W32_THREADS)
42# define MHD_W32_MUTEX_ 1
43# ifndef WIN32_LEAN_AND_MEAN
44# define WIN32_LEAN_AND_MEAN 1
45# endif /* !WIN32_LEAN_AND_MEAN */
46# include <windows.h>
47#elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS)
48# define MHD_PTHREAD_MUTEX_ 1
49# undef HAVE_CONFIG_H
50# include <pthread.h>
51# define HAVE_CONFIG_H 1
52#else
53# error No base mutex API is available.
54#endif
55
56#if defined(MHD_PTHREAD_MUTEX_)
57 typedef pthread_mutex_t MHD_mutex_;
58#elif defined(MHD_W32_MUTEX_)
59 typedef CRITICAL_SECTION MHD_mutex_;
60#endif
61
62#if defined(MHD_PTHREAD_MUTEX_)
63/**
64 * Initialise new mutex.
65 * @param pmutex pointer to the mutex
66 * @return nonzero on success, zero otherwise
67 */
68#define MHD_mutex_init_(pmutex) (!(pthread_mutex_init((pmutex), NULL)))
69#elif defined(MHD_W32_MUTEX_)
70/**
71 * Initialise new mutex.
72 * @param pmutex pointer to mutex
73 * @return nonzero on success, zero otherwise
74 */
75#define MHD_mutex_init_(pmutex) (InitializeCriticalSectionAndSpinCount((pmutex),16))
76#endif
77
78#if defined(MHD_PTHREAD_MUTEX_)
79/**
80 * Destroy previously initialised mutex.
81 * @param pmutex pointer to mutex
82 * @return nonzero on success, zero otherwise
83 */
84#define MHD_mutex_destroy_(pmutex) (!(pthread_mutex_destroy((pmutex))))
85#elif defined(MHD_W32_MUTEX_)
86/**
87 * Destroy previously initialised mutex.
88 * @param pmutex pointer to mutex
89 * @return Always nonzero
90 */
91#define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection((pmutex)), !0)
92#endif
93
94#if defined(MHD_PTHREAD_MUTEX_)
95/**
96 * Acquire lock on previously initialised mutex.
97 * If mutex was already locked by other thread, function
98 * blocks until mutex becomes available.
99 * @param pmutex pointer to mutex
100 * @return nonzero on success, zero otherwise
101 */
102#define MHD_mutex_lock_(pmutex) (!(pthread_mutex_lock((pmutex))))
103#elif defined(MHD_W32_MUTEX_)
104/**
105 * Acquire lock on previously initialised mutex.
106 * If mutex was already locked by other thread, function
107 * blocks until mutex becomes available.
108 * @param pmutex pointer to mutex
109 * @return Always nonzero
110 */
111#define MHD_mutex_lock_(pmutex) (EnterCriticalSection((pmutex)), !0)
112#endif
113
114#if defined(MHD_PTHREAD_MUTEX_)
115/**
116 * Try to acquire lock on previously initialised mutex.
117 * Function returns immediately.
118 * @param pmutex pointer to mutex
119 * @return nonzero if mutex is locked, zero if
120 * mutex was not locked.
121 */
122#define MHD_mutex_trylock_(pmutex) (!(pthread_mutex_trylock((pmutex))))
123#elif defined(MHD_W32_MUTEX_)
124/**
125 * Try to acquire lock on previously initialised mutex.
126 * Function returns immediately.
127 * @param pmutex pointer to mutex
128 * @return nonzero if mutex is locked, zero if
129 * mutex was not locked.
130 */
131#define MHD_mutex_trylock_(pmutex) (TryEnterCriticalSection((pmutex))))
132#endif
133
134#if defined(MHD_PTHREAD_MUTEX_)
135/**
136 * Unlock previously initialised and locked mutex.
137 * @param pmutex pointer to mutex
138 * @return nonzero on success, zero otherwise
139 */
140#define MHD_mutex_unlock_(pmutex) (!(pthread_mutex_unlock((pmutex))))
141#elif defined(MHD_W32_MUTEX_)
142/**
143 * Unlock previously initialised and locked mutex.
144 * @param pmutex pointer to mutex
145 * @return Always nonzero
146 */
147#define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection((pmutex)), !0)
148#endif
149
150#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,
257 response->fd = -1; 257 response->fd = -1;
258 response->data = (void *) &response[1]; 258 response->data = (void *) &response[1];
259 response->data_buffer_size = block_size; 259 response->data_buffer_size = block_size;
260 if (MHD_YES != MHD_mutex_create_ (&response->mutex)) 260 if (!MHD_mutex_init_ (&response->mutex))
261 { 261 {
262 free (response); 262 free (response);
263 return NULL; 263 return NULL;
@@ -513,7 +513,7 @@ MHD_create_response_from_data (size_t size,
513 return NULL; 513 return NULL;
514 memset (response, 0, sizeof (struct MHD_Response)); 514 memset (response, 0, sizeof (struct MHD_Response));
515 response->fd = -1; 515 response->fd = -1;
516 if (MHD_YES != MHD_mutex_create_ (&response->mutex)) 516 if (!MHD_mutex_init_ (&response->mutex))
517 { 517 {
518 free (response); 518 free (response);
519 return NULL; 519 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 @@
39 <ClInclude Include="$(MhdSrc)microhttpd\sysfdsetsize.h" /> 39 <ClInclude Include="$(MhdSrc)microhttpd\sysfdsetsize.h" />
40 <ClInclude Include="$(MhdSrc)microhttpd\mhd_str.h" /> 40 <ClInclude Include="$(MhdSrc)microhttpd\mhd_str.h" />
41 <ClInclude Include="$(MhdSrc)microhttpd\mhd_threads.h" /> 41 <ClInclude Include="$(MhdSrc)microhttpd\mhd_threads.h" />
42 <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h" />
42 <ClInclude Include="$(MhdW32Common)MHD_config.h" /> 43 <ClInclude Include="$(MhdW32Common)MHD_config.h" />
43 </ItemGroup> 44 </ItemGroup>
44 <ItemGroup> 45 <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 @@
136 <ClCompile Include="$(MhdSrc)microhttpd\mhd_threads.c"> 136 <ClCompile Include="$(MhdSrc)microhttpd\mhd_threads.c">
137 <Filter>Source Files</Filter> 137 <Filter>Source Files</Filter>
138 </ClCompile> 138 </ClCompile>
139 <ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h">
140 <Filter>Source Files</Filter>
141 </ClInclude>
139 </ItemGroup> 142 </ItemGroup>
140 <ItemGroup> 143 <ItemGroup>
141 <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc"> 144 <ResourceCompile Include="$(MhdW32Common)microhttpd_dll_res_vc.rc">