diff options
Diffstat (limited to 'src/microhttpd/mhd_locks.h')
-rw-r--r-- | src/microhttpd/mhd_locks.h | 150 |
1 files changed, 150 insertions, 0 deletions
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 */ | ||