GNU libmicrohttpd  0.9.72
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 #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 # include <stdlib.h>
62 /* Simple implementation of MHD_PANIC, to be used outside lib */
63 # define MHD_PANIC(msg) do { fprintf (stderr, \
64  "Abnormal termination at %d line in file %s: %s\n", \
65  (int) __LINE__, __FILE__, msg); abort (); \
66 } while (0)
67 #endif /* ! MHD_PANIC */
68 
69 #if defined(MHD_PTHREAD_MUTEX_)
70 typedef pthread_mutex_t MHD_mutex_;
71 #elif defined(MHD_W32_MUTEX_)
72 typedef CRITICAL_SECTION MHD_mutex_;
73 #endif
74 
75 #if defined(MHD_PTHREAD_MUTEX_)
81 #define MHD_mutex_init_(pmutex) (! (pthread_mutex_init ((pmutex), NULL)))
82 #elif defined(MHD_W32_MUTEX_)
88 #define MHD_mutex_init_(pmutex) (InitializeCriticalSectionAndSpinCount ( \
89  (pmutex),16))
90 #endif
91 
92 #if defined(MHD_PTHREAD_MUTEX_)
93 # if defined(PTHREAD_MUTEX_INITIALIZER)
97 # define MHD_MUTEX_STATIC_DEFN_INIT_(m) static MHD_mutex_ m = \
98  PTHREAD_MUTEX_INITIALIZER
99 # endif /* PTHREAD_MUTEX_INITIALIZER */
100 #endif
101 
102 #if defined(MHD_PTHREAD_MUTEX_)
108 #define MHD_mutex_destroy_(pmutex) (! (pthread_mutex_destroy ((pmutex))))
109 #elif defined(MHD_W32_MUTEX_)
115 #define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection ((pmutex)), ! 0)
116 #endif
117 
123 #define MHD_mutex_destroy_chk_(pmutex) do { \
124  if (! MHD_mutex_destroy_ (pmutex)) \
125  MHD_PANIC (_ ("Failed to destroy mutex.\n")); \
126 } while (0)
127 
128 
129 #if defined(MHD_PTHREAD_MUTEX_)
137 #define MHD_mutex_lock_(pmutex) (! (pthread_mutex_lock ((pmutex))))
138 #elif defined(MHD_W32_MUTEX_)
146 #define MHD_mutex_lock_(pmutex) (EnterCriticalSection ((pmutex)), ! 0)
147 #endif
148 
156 #define MHD_mutex_lock_chk_(pmutex) do { \
157  if (! MHD_mutex_lock_ (pmutex)) \
158  MHD_PANIC (_ ("Failed to lock mutex.\n")); \
159 } while (0)
160 
161 #if defined(MHD_PTHREAD_MUTEX_)
167 #define MHD_mutex_unlock_(pmutex) (! (pthread_mutex_unlock ((pmutex))))
168 #elif defined(MHD_W32_MUTEX_)
174 #define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection ((pmutex)), ! 0)
175 #endif
176 
182 #define MHD_mutex_unlock_chk_(pmutex) do { \
183  if (! MHD_mutex_unlock_ (pmutex)) \
184  MHD_PANIC (_ ("Failed to unlock mutex.\n")); \
185 } while (0)
186 
187 #else /* ! MHD_USE_THREADS */
188 
189 #define MHD_mutex_init_(ignore) (! 0)
190 #define MHD_mutex_destroy_(ignore) (! 0)
191 #define MHD_mutex_destroy_chk_(ignore) (void)0
192 #define MHD_mutex_lock_(ignore) (! 0)
193 #define MHD_mutex_lock_chk_(ignore) (void)0
194 #define MHD_mutex_unlock_(ignore) (! 0)
195 #define MHD_mutex_unlock_chk_(ignore) (void)0
196 
197 #endif /* ! MHD_USE_THREADS */
198 
199 #endif /* ! MHD_LOCKS_H */
additional automatic macros for MHD_config.h