GNU libmicrohttpd  0.9.29
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 
94 #define MHD_mutex_destroy_(pmutex) (!(pthread_mutex_destroy((pmutex))))
95 #elif defined(MHD_W32_MUTEX_)
96 
101 #define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection((pmutex)), !0)
102 #endif
103 
109 #define MHD_mutex_destroy_chk_(pmutex) do { \
110  if (!MHD_mutex_destroy_(pmutex)) \
111  MHD_PANIC(_("Failed to destroy mutex.\n")); \
112  } while(0)
113 
114 
115 #if defined(MHD_PTHREAD_MUTEX_)
116 
123 #define MHD_mutex_lock_(pmutex) (!(pthread_mutex_lock((pmutex))))
124 #elif defined(MHD_W32_MUTEX_)
125 
132 #define MHD_mutex_lock_(pmutex) (EnterCriticalSection((pmutex)), !0)
133 #endif
134 
142 #define MHD_mutex_lock_chk_(pmutex) do { \
143  if (!MHD_mutex_lock_(pmutex)) \
144  MHD_PANIC(_("Failed to lock mutex.\n")); \
145  } while(0)
146 
147 #if defined(MHD_PTHREAD_MUTEX_)
148 
153 #define MHD_mutex_unlock_(pmutex) (!(pthread_mutex_unlock((pmutex))))
154 #elif defined(MHD_W32_MUTEX_)
155 
160 #define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection((pmutex)), !0)
161 #endif
162 
168 #define MHD_mutex_unlock_chk_(pmutex) do { \
169  if (!MHD_mutex_unlock_(pmutex)) \
170  MHD_PANIC(_("Failed to unlock mutex.\n")); \
171  } while(0)
172 
173 
174 #endif /* ! MHD_LOCKS_H */
additional automatic macros for MHD_config.h