diff --git a/README.md b/README.md index 0999448e..38c0e1eb 100644 --- a/README.md +++ b/README.md @@ -6040,9 +6040,12 @@ If the C11 implementation does not respect the C standard (i.e. the compiler targets C11 mode the \_\_STDC\_NO\_THREADS\_\_ macro is not defined but the header threads.h is not available or not working), -then the user shall define manually the M\_USE\_C11\_NO\_THREADS -macro before including any M\*LIB header to disable it, so that -M\*LIB can select another implementation. +then the user shall define manually the M\_USE\_THREAD\_BACKEND +macro to select the back end to use: + +* 1: for C11 +* 2: for WINDOWS +* 3: for PTHREAD Example: diff --git a/m-mutex.h b/m-mutex.h index 8eb648b2..1c35d32b 100644 --- a/m-mutex.h +++ b/m-mutex.h @@ -25,11 +25,21 @@ #ifndef MSTARLIB_MUTEX_H #define MSTARLIB_MUTEX_H -#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \ - && !defined(__STDC_NO_THREADS__) \ - && !defined(M_USE_C11_NO_THREADS) +/* Auto-detect the thread backend to use if the user has not override it */ +#ifndef M_USE_THREAD_BACKEND +# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \ + && !defined(__STDC_NO_THREADS__) +# define M_USE_THREAD_BACKEND 1 +# elif defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) +# define M_USE_THREAD_BACKEND 2 +# else +# define M_USE_THREAD_BACKEND 3 +# endif +#endif + /****************************** C11 version ********************************/ +#if M_USE_THREAD_BACKEND == 1 #include #include @@ -159,10 +169,9 @@ static inline void m_oncei_call(m_oncei_t o, void (*func)(void)) M_END_PROTECTED_CODE -// MSYS2 doesn't define _WIN32 by default but __CYGWIN__ -#elif defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) /****************************** WIN32 version ******************************/ +#elif M_USE_THREAD_BACKEND == 2 /* CLANG provides some useless and wrong warnings: * - _WIN32_WINNT starts with '_' which is reserved by the standard @@ -333,9 +342,9 @@ static inline void m_oncei_call(m_oncei_t o, void (*func)(void)) M_END_PROTECTED_CODE -#else /**************************** PTHREAD version ******************************/ +#else #include #ifdef _POSIX_PRIORITY_SCHEDULING