aboutsummaryrefslogtreecommitdiff
path: root/src/lib/mhd_locks.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/mhd_locks.h')
-rw-r--r--src/lib/mhd_locks.h183
1 files changed, 183 insertions, 0 deletions
diff --git a/src/lib/mhd_locks.h b/src/lib/mhd_locks.h
new file mode 100644
index 00000000..21db56c4
--- /dev/null
+++ b/src/lib/mhd_locks.h
@@ -0,0 +1,183 @@
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 * @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#if defined(MHD_USE_W32_THREADS)
43# define MHD_W32_MUTEX_ 1
44# ifndef WIN32_LEAN_AND_MEAN
45# define WIN32_LEAN_AND_MEAN 1
46# endif /* !WIN32_LEAN_AND_MEAN */
47# include <windows.h>
48#elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS)
49# define MHD_PTHREAD_MUTEX_ 1
50# undef HAVE_CONFIG_H
51# include <pthread.h>
52# define HAVE_CONFIG_H 1
53#else
54# error No base mutex API is available.
55#endif
56
57#ifndef MHD_PANIC
58# include <stdio.h>
59# include <stdlib.h>
60/* Simple implementation of MHD_PANIC, to be used outside lib */
61# define MHD_PANIC(msg) do { fprintf (stderr, \
62 "Abnormal termination at %d line in file %s: %s\n", \
63 (int)__LINE__, __FILE__, msg); abort();} while(0)
64#endif /* ! MHD_PANIC */
65
66#if defined(MHD_PTHREAD_MUTEX_)
67 typedef pthread_mutex_t MHD_mutex_;
68#elif defined(MHD_W32_MUTEX_)
69 typedef CRITICAL_SECTION MHD_mutex_;
70#endif
71
72#if defined(MHD_PTHREAD_MUTEX_)
73/**
74 * Initialise new mutex.
75 * @param pmutex pointer to the mutex
76 * @return nonzero on success, zero otherwise
77 */
78#define MHD_mutex_init_(pmutex) (!(pthread_mutex_init((pmutex), NULL)))
79#elif defined(MHD_W32_MUTEX_)
80/**
81 * Initialise new mutex.
82 * @param pmutex pointer to mutex
83 * @return nonzero on success, zero otherwise
84 */
85#define MHD_mutex_init_(pmutex) (InitializeCriticalSectionAndSpinCount((pmutex),16))
86#endif
87
88#if defined(MHD_PTHREAD_MUTEX_)
89# if defined(PTHREAD_MUTEX_INITIALIZER)
90/**
91 * Define static mutex and statically initialise it.
92 */
93# define MHD_MUTEX_STATIC_DEFN_INIT_(m) static MHD_mutex_ m = PTHREAD_MUTEX_INITIALIZER
94# endif /* PTHREAD_MUTEX_INITIALIZER */
95#endif
96
97#if defined(MHD_PTHREAD_MUTEX_)
98/**
99 * Destroy previously initialised mutex.
100 * @param pmutex pointer to mutex
101 * @return nonzero on success, zero otherwise
102 */
103#define MHD_mutex_destroy_(pmutex) (!(pthread_mutex_destroy((pmutex))))
104#elif defined(MHD_W32_MUTEX_)
105/**
106 * Destroy previously initialised mutex.
107 * @param pmutex pointer to mutex
108 * @return Always nonzero
109 */
110#define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection((pmutex)), !0)
111#endif
112
113/**
114 * Destroy previously initialised mutex and abort execution
115 * if error is detected.
116 * @param pmutex pointer to mutex
117 */
118#define MHD_mutex_destroy_chk_(pmutex) do { \
119 if (!MHD_mutex_destroy_(pmutex)) \
120 MHD_PANIC(_("Failed to destroy mutex.\n")); \
121 } while(0)
122
123
124#if defined(MHD_PTHREAD_MUTEX_)
125/**
126 * Acquire lock on previously initialised mutex.
127 * If mutex was already locked by other thread, function
128 * blocks until mutex becomes available.
129 * @param pmutex pointer to mutex
130 * @return nonzero on success, zero otherwise
131 */
132#define MHD_mutex_lock_(pmutex) (!(pthread_mutex_lock((pmutex))))
133#elif defined(MHD_W32_MUTEX_)
134/**
135 * Acquire lock on previously initialised mutex.
136 * If mutex was already locked by other thread, function
137 * blocks until mutex becomes available.
138 * @param pmutex pointer to mutex
139 * @return Always nonzero
140 */
141#define MHD_mutex_lock_(pmutex) (EnterCriticalSection((pmutex)), !0)
142#endif
143
144/**
145 * Acquire lock on previously initialised mutex.
146 * If mutex was already locked by other thread, function
147 * blocks until mutex becomes available.
148 * If error is detected, execution will be aborted.
149 * @param pmutex pointer to mutex
150 */
151#define MHD_mutex_lock_chk_(pmutex) do { \
152 if (!MHD_mutex_lock_(pmutex)) \
153 MHD_PANIC(_("Failed to lock mutex.\n")); \
154 } while(0)
155
156#if defined(MHD_PTHREAD_MUTEX_)
157/**
158 * Unlock previously initialised and locked mutex.
159 * @param pmutex pointer to mutex
160 * @return nonzero on success, zero otherwise
161 */
162#define MHD_mutex_unlock_(pmutex) (!(pthread_mutex_unlock((pmutex))))
163#elif defined(MHD_W32_MUTEX_)
164/**
165 * Unlock previously initialised and locked mutex.
166 * @param pmutex pointer to mutex
167 * @return Always nonzero
168 */
169#define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection((pmutex)), !0)
170#endif
171
172/**
173 * Unlock previously initialised and locked mutex.
174 * If error is detected, execution will be aborted.
175 * @param pmutex pointer to mutex
176 */
177#define MHD_mutex_unlock_chk_(pmutex) do { \
178 if (!MHD_mutex_unlock_(pmutex)) \
179 MHD_PANIC(_("Failed to unlock mutex.\n")); \
180 } while(0)
181
182
183#endif /* ! MHD_LOCKS_H */