|
Packit |
875988 |
/*
|
|
Packit |
875988 |
This file is part of libmicrohttpd
|
|
Packit |
875988 |
Copyright (C) 2016 Karlson2k (Evgeny Grin)
|
|
Packit |
875988 |
|
|
Packit |
875988 |
This library is free software; you can redistribute it and/or
|
|
Packit |
875988 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
875988 |
License as published by the Free Software Foundation; either
|
|
Packit |
875988 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
This library is distributed in the hope that it will be useful,
|
|
Packit |
875988 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
875988 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
875988 |
Lesser General Public License for more details.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
875988 |
License along with this library; if not, write to the Free Software
|
|
Packit |
875988 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
875988 |
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* @file microhttpd/mhd_locks.h
|
|
Packit |
875988 |
* @brief Header for platform-independent locks abstraction
|
|
Packit |
875988 |
* @author Karlson2k (Evgeny Grin)
|
|
Packit |
875988 |
* @author Christian Grothoff
|
|
Packit |
875988 |
*
|
|
Packit |
875988 |
* Provides basic abstraction for locks/mutex.
|
|
Packit |
875988 |
* Any functions can be implemented as macro on some platforms
|
|
Packit |
875988 |
* unless explicitly marked otherwise.
|
|
Packit |
875988 |
* Any function argument can be skipped in macro, so avoid
|
|
Packit |
875988 |
* variable modification in function parameters.
|
|
Packit |
875988 |
*
|
|
Packit |
875988 |
* @warning Unlike pthread functions, most of functions return
|
|
Packit |
875988 |
* nonzero on success.
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifndef MHD_LOCKS_H
|
|
Packit |
875988 |
#define MHD_LOCKS_H 1
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#include "mhd_options.h"
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#if defined(MHD_USE_W32_THREADS)
|
|
Packit |
875988 |
# define MHD_W32_MUTEX_ 1
|
|
Packit |
875988 |
# ifndef WIN32_LEAN_AND_MEAN
|
|
Packit |
875988 |
# define WIN32_LEAN_AND_MEAN 1
|
|
Packit |
875988 |
# endif /* !WIN32_LEAN_AND_MEAN */
|
|
Packit |
875988 |
# include <windows.h>
|
|
Packit |
875988 |
#elif defined(HAVE_PTHREAD_H) && defined(MHD_USE_POSIX_THREADS)
|
|
Packit |
875988 |
# define MHD_PTHREAD_MUTEX_ 1
|
|
Packit |
875988 |
# undef HAVE_CONFIG_H
|
|
Packit |
875988 |
# include <pthread.h>
|
|
Packit |
875988 |
# define HAVE_CONFIG_H 1
|
|
Packit |
875988 |
#else
|
|
Packit |
875988 |
# error No base mutex API is available.
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#ifndef MHD_PANIC
|
|
Packit |
875988 |
# include <stdio.h>
|
|
Packit |
875988 |
# include <stdlib.h>
|
|
Packit |
875988 |
/* Simple implementation of MHD_PANIC, to be used outside lib */
|
|
Packit |
875988 |
# define MHD_PANIC(msg) do { fprintf (stderr, \
|
|
Packit |
875988 |
"Abnormal termination at %d line in file %s: %s\n", \
|
|
Packit |
875988 |
(int)__LINE__, __FILE__, msg); abort();} while(0)
|
|
Packit |
875988 |
#endif /* ! MHD_PANIC */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#if defined(MHD_PTHREAD_MUTEX_)
|
|
Packit |
875988 |
typedef pthread_mutex_t MHD_mutex_;
|
|
Packit |
875988 |
#elif defined(MHD_W32_MUTEX_)
|
|
Packit |
875988 |
typedef CRITICAL_SECTION MHD_mutex_;
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#if defined(MHD_PTHREAD_MUTEX_)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Initialise new mutex.
|
|
Packit |
875988 |
* @param pmutex pointer to the mutex
|
|
Packit |
875988 |
* @return nonzero on success, zero otherwise
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_init_(pmutex) (!(pthread_mutex_init((pmutex), NULL)))
|
|
Packit |
875988 |
#elif defined(MHD_W32_MUTEX_)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Initialise new mutex.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
* @return nonzero on success, zero otherwise
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_init_(pmutex) (InitializeCriticalSectionAndSpinCount((pmutex),16))
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#if defined(MHD_PTHREAD_MUTEX_)
|
|
Packit |
875988 |
# if defined(PTHREAD_MUTEX_INITIALIZER)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Define static mutex and statically initialise it.
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
# define MHD_MUTEX_STATIC_DEFN_INIT_(m) static MHD_mutex_ m = PTHREAD_MUTEX_INITIALIZER
|
|
Packit |
875988 |
# endif /* PTHREAD_MUTEX_INITIALIZER */
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#if defined(MHD_PTHREAD_MUTEX_)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Destroy previously initialised mutex.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
* @return nonzero on success, zero otherwise
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_destroy_(pmutex) (!(pthread_mutex_destroy((pmutex))))
|
|
Packit |
875988 |
#elif defined(MHD_W32_MUTEX_)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Destroy previously initialised mutex.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
* @return Always nonzero
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_destroy_(pmutex) (DeleteCriticalSection((pmutex)), !0)
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Destroy previously initialised mutex and abort execution
|
|
Packit |
875988 |
* if error is detected.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_destroy_chk_(pmutex) do { \
|
|
Packit |
875988 |
if (!MHD_mutex_destroy_(pmutex)) \
|
|
Packit |
875988 |
MHD_PANIC(_("Failed to destroy mutex.\n")); \
|
|
Packit |
875988 |
} while(0)
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#if defined(MHD_PTHREAD_MUTEX_)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Acquire lock on previously initialised mutex.
|
|
Packit |
875988 |
* If mutex was already locked by other thread, function
|
|
Packit |
875988 |
* blocks until mutex becomes available.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
* @return nonzero on success, zero otherwise
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_lock_(pmutex) (!(pthread_mutex_lock((pmutex))))
|
|
Packit |
875988 |
#elif defined(MHD_W32_MUTEX_)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Acquire lock on previously initialised mutex.
|
|
Packit |
875988 |
* If mutex was already locked by other thread, function
|
|
Packit |
875988 |
* blocks until mutex becomes available.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
* @return Always nonzero
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_lock_(pmutex) (EnterCriticalSection((pmutex)), !0)
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Acquire lock on previously initialised mutex.
|
|
Packit |
875988 |
* If mutex was already locked by other thread, function
|
|
Packit |
875988 |
* blocks until mutex becomes available.
|
|
Packit |
875988 |
* If error is detected, execution will be aborted.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_lock_chk_(pmutex) do { \
|
|
Packit |
875988 |
if (!MHD_mutex_lock_(pmutex)) \
|
|
Packit |
875988 |
MHD_PANIC(_("Failed to lock mutex.\n")); \
|
|
Packit |
875988 |
} while(0)
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#if defined(MHD_PTHREAD_MUTEX_)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Unlock previously initialised and locked mutex.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
* @return nonzero on success, zero otherwise
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_unlock_(pmutex) (!(pthread_mutex_unlock((pmutex))))
|
|
Packit |
875988 |
#elif defined(MHD_W32_MUTEX_)
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Unlock previously initialised and locked mutex.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
* @return Always nonzero
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_unlock_(pmutex) (LeaveCriticalSection((pmutex)), !0)
|
|
Packit |
875988 |
#endif
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* Unlock previously initialised and locked mutex.
|
|
Packit |
875988 |
* If error is detected, execution will be aborted.
|
|
Packit |
875988 |
* @param pmutex pointer to mutex
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
#define MHD_mutex_unlock_chk_(pmutex) do { \
|
|
Packit |
875988 |
if (!MHD_mutex_unlock_(pmutex)) \
|
|
Packit |
875988 |
MHD_PANIC(_("Failed to unlock mutex.\n")); \
|
|
Packit |
875988 |
} while(0)
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#endif /* ! MHD_LOCKS_H */
|