Răsfoiți Sursa

Implement wrapper macros for mutex manipulations, support W32 native mutex, require at least WinXP headers on W32

Evgeny Grin (Karlson2k) 12 ani în urmă
părinte
comite
60d4fbee43
2 a modificat fișierele cu 121 adăugiri și 1 ștergeri
  1. 10 0
      src/include/platform.h
  2. 111 1
      src/include/platform_interface.h

+ 10 - 0
src/include/platform.h

@@ -51,6 +51,16 @@
 #define _LP64
 #define _LP64
 #endif
 #endif
 
 
+#if defined(_WIN32)
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#else // _WIN32_WINNT
+#if _WIN32_WINNT < 0x0501
+#error "Headers for Windows XP or later are required"
+#endif // _WIN32_WINNT < 0x0501
+#endif // _WIN32_WINNT
+#endif // _WIN32
+
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdint.h>

+ 111 - 1
src/include/platform_interface.h

@@ -18,7 +18,7 @@
 */
 */
 
 
 /**
 /**
- * @file platform/platfrom_interface.h
+ * @file include/platfrom_interface.h
  * @brief  internal platform abstraction functions
  * @brief  internal platform abstraction functions
  * @author Karlson2k (Evgeny Grin)
  * @author Karlson2k (Evgeny Grin)
  */
  */
@@ -139,4 +139,114 @@
 #define MHD_random_() MHD_W32_random_()
 #define MHD_random_() MHD_W32_random_()
 #endif
 #endif
 
 
+#if defined(_WIN32) && !defined(__CYGWIN__)
+#define MHD_W32_MUTEX_ 1
+/* 'void*' is the same as 'HANDLE'
+ * this way allow typedef without including "windows.h" */
+typedef void* MHD_mutex_;
+#elif defined(HAVE_PTHREAD_H)
+#define MHD_PTHREAD_MUTEX_ 1
+typedef pthread_mutex_t MHD_mutex_;
+#else
+#error "No base mutex API is available."
+#endif
+
+#if defined(MHD_PTHREAD_MUTEX_)
+/**
+ * Create new mutex.
+ * @param mutex pointer to the mutex
+ * @return #MHD_YES on success, #MHD_NO on failure
+ */
+#define MHD_mutex_create_(mutex) \
+  ((0 == pthread_mutex_init ((mutex), NULL)) ? MHD_YES : MHD_NO)
+#elif defined(MHD_W32_MUTEX_)
+/**
+ * Create new mutex.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES on success, #MHD_NO on failure
+ */
+#define MHD_mutex_create_(mutex) \
+  ((NULL != (mutex) && NULL != (*(mutex) = CreateMutex(NULL, FALSE, NULL))) ? MHD_YES : MHD_NO)
+#endif
+
+#if defined(MHD_PTHREAD_MUTEX_)
+/**
+ * Destroy previously created mutex.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES on success, #MHD_NO on failure
+ */
+#define MHD_mutex_destroy_(mutex) \
+  ((0 == pthread_mutex_destroy ((mutex), NULL)) ? MHD_YES : MHD_NO)
+#elif defined(MHD_W32_MUTEX_)
+/**
+ * Destroy previously created mutex.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES on success, #MHD_NO on failure
+ */
+#define MHD_mutex_destroy_(mutex) \
+  ((NULL != (mutex) && 0 != CloseHandle(*(mutex)) && NULL == (*(mutex) = NULL)) ? MHD_YES : MHD_NO)
+#endif
+
+#if defined(MHD_PTHREAD_MUTEX_)
+/**
+ * Acquire lock on previously created mutex.
+ * If mutex was already locked by other thread, function
+ * blocks until mutex becomes available.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES on success, #MHD_NO on failure
+ */
+#define MHD_mutex_lock_(mutex) \
+  ((0 == pthread_mutex_lock((mutex), NULL)) ? MHD_YES : MHD_NO)
+#elif defined(MHD_W32_MUTEX_)
+/**
+ * Acquire lock on previously created mutex.
+ * If mutex was already locked by other thread, function
+ * blocks until mutex becomes available.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES on success, #MHD_NO on failure
+ */
+#define MHD_mutex_lock_(mutex) \
+  ((NULL != (mutex) && WAIT_OBJECT_0 == WaitForSingleObject(*(mutex), INFINITE)) ? MHD_YES : MHD_NO)
+#endif
+
+#if defined(MHD_PTHREAD_MUTEX_)
+/**
+ * Try to acquire lock on previously created mutex.
+ * Function returns immediately.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES if mutex is locked, #MHD_NO if
+ * mutex was not locked.
+ */
+#define MHD_mutex_trylock_(mutex) \
+  ((0 == pthread_mutex_trylock((mutex), NULL)) ? MHD_YES : MHD_NO)
+#elif defined(MHD_W32_MUTEX_)
+/**
+ * Try to acquire lock on previously created mutex.
+ * Function returns immediately.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES if mutex is locked, #MHD_NO if
+ * mutex was not locked.
+ */
+#define MHD_mutex_trylock_(mutex) \
+  ((NULL != (mutex) && WAIT_OBJECT_0 == WaitForSingleObject(*(mutex), 0)) ? MHD_YES : MHD_NO)
+#endif
+
+#if defined(MHD_PTHREAD_MUTEX_)
+/**
+ * Unlock previously created and locked mutex.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES on success, #MHD_NO on failure
+ */
+#define MHD_mutex_unlock_(mutex) \
+  ((0 == pthread_mutex_unlock((mutex), NULL)) ? MHD_YES : MHD_NO)
+#elif defined(MHD_W32_MUTEX_)
+/**
+ * Unlock previously created and locked mutex.
+ * @param mutex pointer to mutex
+ * @return #MHD_YES on success, #MHD_NO on failure
+ */
+#define MHD_mutex_unlock_(mutex) \
+  ((NULL != (mutex) && 0 != ReleaseMutex(*(mutex))) ? MHD_YES : MHD_NO)
+#endif
+
 #endif // MHD_PLATFORM_INTERFACE_H
 #endif // MHD_PLATFORM_INTERFACE_H