GNU libmicrohttpd  0.9.59
mhd_locks.h
Go to the documentation of this file.
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 
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 
78 #define MHD_mutex_init_(pmutex) (!(pthread_mutex_init((pmutex), NULL)))
79 #elif defined(MHD_W32_MUTEX_)
80 
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 
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 
103 #define MHD_mutex_destroy_(pmutex) (!(pthread_mutex_destroy((pmutex))))
104 #elif defined(MHD_W32_MUTEX_)
105 
110 #define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection((pmutex)), !0)
111 #endif
112 
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 
132 #define MHD_mutex_lock_(pmutex) (!(pthread_mutex_lock((pmutex))))
133 #elif defined(MHD_W32_MUTEX_)
134 
141 #define MHD_mutex_lock_(pmutex) (EnterCriticalSection((pmutex)), !0)
142 #endif
143 
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 
162 #define MHD_mutex_unlock_(pmutex) (!(pthread_mutex_unlock((pmutex))))
163 #elif defined(MHD_W32_MUTEX_)
164 
169 #define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection((pmutex)), !0)
170 #endif
171 
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 */
additional automatic macros for MHD_config.h