| /* | |
| Copyright (c) 2011-2016, 2026 mingw-w64 project | |
| Permission is hereby granted, free of charge, to any person obtaining a | |
| copy of this software and associated documentation files (the "Software"), | |
| to deal in the Software without restriction, including without limitation | |
| the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
| and/or sell copies of the Software, and to permit persons to whom the | |
| Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in | |
| all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
| DEALINGS IN THE SOFTWARE. | |
| */ | |
| #ifndef WIN_PTHREADS_MISC_H | |
| #define WIN_PTHREADS_MISC_H | |
| #ifdef HAVE_CONFIG_H | |
| #include "config.h" | |
| #endif | |
| /* Public headers */ | |
| #include <pthread.h> | |
| #include <pthread_compat.h> | |
| #include <pthread_signal.h> | |
| #include <pthread_time.h> | |
| #include <pthread_unistd.h> | |
| #include <sched.h> | |
| #include <semaphore.h> | |
| #include <sdkddkver.h> | |
| /** | |
| * Windows NT 3.1 | |
| * | |
| * Major version: 3 | |
| * Minor version: 10 | |
| */ | |
| #define WINPTHREADS_WINNT_NT_3_1 (0x030A) | |
| /** | |
| * Windows NT 3.5 | |
| * | |
| * Major version: 3 | |
| * Minor version: 50 | |
| */ | |
| #define WINPTHREADS_WINNT_NT_3_5 (0x0332) | |
| /** | |
| * Windows NT 3.51 | |
| * | |
| * Major version: 3 | |
| * Minor version: 51 | |
| */ | |
| #define WINPTHREADS_WINNT_NT_3_51 (0x0333) | |
| /** | |
| * Windows NT 4 | |
| * | |
| * Major version: 4 | |
| * Minor version: 0 | |
| */ | |
| #define WINPTHREADS_WINNT_NT_4 _WIN32_WINNT_NT4 | |
| /** | |
| * Windows 2000 | |
| * | |
| * Major version: 5 | |
| * Minor version: 0 | |
| */ | |
| #define WINPTHREADS_WINNT_2000 _WIN32_WINNT_WIN2K | |
| /** | |
| * Windows XP | |
| * | |
| * Major version: 5 | |
| * Minor version: 1 | |
| */ | |
| #define WINPTHREADS_WINNT_XP _WIN32_WINNT_WINXP | |
| /** | |
| * Windows Server 2003 / XP x64 | |
| * | |
| * Major version: 5 | |
| * Minor version: 2 | |
| */ | |
| #define WINPTHREADS_WINNT_2003 _WIN32_WINNT_WS03 | |
| /** | |
| * Windows Vista | |
| * | |
| * Major version: 6 | |
| * Minor version: 0 | |
| */ | |
| #define WINPTHREADS_WINNT_VISTA _WIN32_WINNT_VISTA | |
| /** | |
| * Windows 7 | |
| * | |
| * Major version: 6 | |
| * Minor version: 1 | |
| */ | |
| #define WINPTHREADS_WINNT_WIN7 _WIN32_WINNT_WIN7 | |
| /** | |
| * Windows 8 | |
| * | |
| * Major version: 6 | |
| * Minor version: 2 | |
| */ | |
| #define WINPTHREADS_WINNT_WIN8 _WIN32_WINNT_WIN8 | |
| /** | |
| * Windows 8.1 | |
| * | |
| * Major version: 6 | |
| * Minor version: 3 | |
| */ | |
| #define WINPTHREADS_WINNT_WIN8_1 _WIN32_WINNT_WINBLUE | |
| /** | |
| * Windows 10 | |
| * | |
| * Major version: 10 | |
| * Minor version: 0 | |
| */ | |
| #define WINPTHREADS_WINNT_WIN10 _WIN32_WINNT_WIN10 | |
| /** | |
| * Define `WINPTHREADS_WINNT` macro. | |
| * | |
| * Minimal Windows NT version to be supported by the library. | |
| */ | |
| /** | |
| * All Windows NT versions support x86. | |
| * x86 builds can also be configured with Win9x support. | |
| */ | |
| #if defined(_M_IX86) | |
| /** | |
| * For x86 builds with MSVC, use Windows XP as the minimal version as recent | |
| * MSVC does not support targeting older Windows versions. | |
| */ | |
| #if defined(_MSC_VER) | |
| #ifndef WINPTHREADS_WINNT | |
| #define WINPTHREADS_WINNT WINPTHREADS_WINNT_XP | |
| #endif | |
| /** | |
| * For x86 build with mingw-w64, use NT 3.1 as the minimal version. | |
| */ | |
| #else /* mingw-w64 */ | |
| #ifndef WINPTHREADS_WINNT | |
| #define WINPTHREADS_WINNT WINPTHREADS_WINNT_NT_3_1 | |
| #endif | |
| /** | |
| * Enable Win9x support if not disabled explicitly, and minimal Windows version | |
| * to be supported is not greater than Windows XP x64. | |
| */ | |
| #if !defined(WINPTHREADS_WIN9X) && WINPTHREADS_WINNT <= WINPTHREADS_WINNT_2003 | |
| #define WINPTHREADS_WIN9X 1 | |
| #endif | |
| #endif /* mingw-w64 */ | |
| #if defined(WINPTHREADS_WIN9X) && WINPTHREADS_WIN9X && WINPTHREADS_WINNT > WINPTHREADS_WINNT_2003 | |
| #error Win9x support cannot be enabled when WINPTHREADS_WINNT is defined to a value higher than _WIN32_WINNT_WS03 | |
| #endif | |
| /** | |
| * Native x64, not arm64ec. | |
| * Support for x64 is only available since Windows Server 2003 SP1 / XP x64. | |
| */ | |
| #elif defined(_M_X64) && !defined(_M_ARM64EC) | |
| #if defined(WINPTHREADS_WINNT) && WINPTHREADS_WINNT < WINPTHREADS_WINNT_2003 | |
| #undef WINPTHREADS_WINNT | |
| #endif | |
| #ifndef WINPTHREADS_WINNT | |
| #define WINPTHREADS_WINNT WINPTHREADS_WINNT_2003 | |
| #endif | |
| #ifdef WINPTHREADS_WIN9X | |
| #undef WINPTHREADS_WIN9X | |
| #endif | |
| /** | |
| * Support for arm32 is available since Windows 8. | |
| */ | |
| #elif defined(_M_ARM) | |
| #if defined(WINPTHREADS_WINNT) && WINPTHREADS_WINNT < WINPTHREADS_WINNT_WIN8 | |
| #undef WINPTHREADS_WINNT | |
| #endif | |
| #ifndef WINPTHREADS_WINNT | |
| #define WINPTHREADS_WINNT WINPTHREADS_WINNT_WIN8 | |
| #endif | |
| #ifdef WINPTHREADS_WIN9X | |
| #undef WINPTHREADS_WIN9X | |
| #endif | |
| /** | |
| * Support for arm64 and arm64ec is available since Windows 10. | |
| */ | |
| #elif defined(_M_ARM64) || defined(_M_ARM64EC) | |
| #if defined(WINPTHREADS_WINNT) && WINPTHREADS_WINNT < WINPTHREADS_WINNT_WIN10 | |
| #undef WINPTHREADS_WINNT | |
| #endif | |
| #ifndef WINPTHREADS_WINNT | |
| #define WINPTHREADS_WINNT WINPTHREADS_WINNT_WIN10 | |
| #endif | |
| #ifdef WINPTHREADS_WIN9X | |
| #undef WINPTHREADS_WIN9X | |
| #endif | |
| #else /* unknown arch */ | |
| #error Unsupported architecture | |
| #endif /* unknown arch */ | |
| /** | |
| * Whether to include code for Win9x support. | |
| */ | |
| #ifndef WINPTHREADS_WIN9X | |
| #define WINPTHREADS_WIN9X 0 | |
| #endif | |
| #if defined(__GNUC__) || defined(__clang__) | |
| #define likely(cond) __builtin_expect ((cond) != 0, 1) | |
| #define unlikely(cond) __builtin_expect ((cond) != 0, 0) | |
| #else | |
| #define likely(cond) (cond) | |
| #define unlikely(cond) (cond) | |
| #endif | |
| #if defined(__GNUC__) || defined(__clang__) | |
| #define UNREACHABLE() __builtin_unreachable () | |
| #elif defined(_MSC_VER) | |
| #define UNREACHABLE() __assume (0) | |
| #endif | |
| /** | |
| * Many Windows functions have two versions: ANSI and Unicode. | |
| * | |
| * Unless you are targeting Windows 10 or later and using UTF-8 manifest, | |
| * the Unicode versions should always be preferred; otherwise, those | |
| * functions will only be able to operate on ANSI and OEM code pages. | |
| * | |
| * However, Unicode functions are not supported on Win9x systems, | |
| * and so ANSI versions have to be used when Win9x support is desired. | |
| * | |
| * The best approach to handle this is to use Unicode versions through some | |
| * kind of wrapper; when Win9x support is enabled, make those wrappers choose | |
| * whether to call Unicode or ANSI version at runtime, and make those wrappers | |
| * handle conversion from Unicode to appropriate code page. | |
| * | |
| * In some cases, however, we may call such functions in context where we | |
| * pass constant string literals or using runtime checks is not feasible. | |
| * | |
| * The macros `WINPTHREADS_AW` and `WINPTHREADS_TEXT` allow to call Unicode | |
| * or ANSI version depending on whether Win9x support is enabled. | |
| */ | |
| #if WINPTHREADS_WIN9X | |
| #define WINPTHREADS_AW(name) name ## A | |
| #define WINPTHREADS_TEXT(text) text | |
| #else | |
| #define WINPTHREADS_AW(name) name ## W | |
| #define WINPTHREADS_TEXT_CONCAT(text) L ## text | |
| #define WINPTHREADS_TEXT(text) WINPTHREADS_TEXT_CONCAT (text) | |
| #endif | |
| /** | |
| * Portability wrapper for `GetHandleInformation`. | |
| */ | |
| int winpthreads_get_handle_information (void *, unsigned *); | |
| /** | |
| * Portability wrapper for `SetThreadDescription`. | |
| * | |
| * Since `SetThreadDescription` expects wide character string for its | |
| * second argument, this wrapper converts its second argument using | |
| * active ANSI code page before passing it to `SetThreadDescription`. | |
| */ | |
| int winpthreads_set_thread_description_ansi (void *, const char *); | |
| /** | |
| * Portability wrapper for `SetThreadDescription`. | |
| */ | |
| int winpthreads_set_thread_description_unicode (void *, const wchar_t *); | |
| /** | |
| * Validate `handle`. | |
| * | |
| * Returns non-zero if `handle` is valid and zero otherwise. | |
| */ | |
| int winpthreads_validate_handle (void *handle); | |
| /** | |
| * Initialize affinity functions. | |
| */ | |
| void winpthreads_affinity_init (void); | |
| /** | |
| * Values returned by `winpthreads_wait`. | |
| */ | |
| typedef enum { | |
| /** | |
| * Call succeeded. | |
| */ | |
| wait_succeeded, | |
| /** | |
| * Call failed. | |
| * | |
| * The reason usually depends on the type of the object `winpthreads_wait` | |
| * waited for; call `GetLastError` to obtain more details. | |
| */ | |
| wait_failed, | |
| /** | |
| * Call succeeded. | |
| * | |
| * This return value means that `winpthreads_wait` was called on a mutex | |
| * whose previous owner terminated without releasing it; the calling thread | |
| * owns the mutex now. | |
| */ | |
| wait_abandoned_mutex, | |
| /** | |
| * The wait timeout expired. | |
| */ | |
| wait_timeout_expired, | |
| /** | |
| * The wait was interrupted. | |
| */ | |
| wait_interrupted, | |
| } winpthreads_wait_status; | |
| /** | |
| * When this flag is set, the wait will be interrupted if the calling thread | |
| * receives cancellation notification. | |
| * | |
| * Note that by itself, `winpthreads_wait` is not a cancellation point; | |
| * it is responsibility of the caller to handle pending cancellation request, | |
| * usually by simply calling `pthread_testcancel`. | |
| */ | |
| #define WINPTHREADS_WAIT_CANCELLATION_NOTIFICATION ((int) 0x00000001) | |
| /** | |
| * The `timeout` argument to `winpthreads_wait` specifies absolute time | |
| * as measured by the `clock` argument. | |
| * | |
| * Otherwise, `winpthreads_wait` always uses monotonic clock (`CLOCK_MONOTONIC`) | |
| * to measure elapsed wait time, regardless of the `clock` argument. | |
| * | |
| * This flag has no effect if the `timeout` argument is | |
| * `WINPTHREADS_WAIT_TIMEOUT_ZERO` or `WINPTHREADS_WAIT_TIMEOUT_INDEFINITE`. | |
| */ | |
| #define WINPTHREADS_WAIT_TIMEOUT_ABSTIME ((int) 0x01000000) | |
| /** | |
| * Can be passed as `timeout` argument to `winpthreads_wait`. | |
| */ | |
| #define WINPTHREADS_WAIT_TIMEOUT_ZERO ((const struct _timespec64 *) 0) | |
| /** | |
| * Can be passed as `timeout` argument to `winpthreads_wait`. | |
| */ | |
| #define WINPTHREADS_WAIT_TIMEOUT_INDEFINITE ((const struct _timespec64 *) -1) | |
| /** | |
| * The wait routine. | |
| * | |
| * Block execution of the calling thread until either: | |
| * | |
| * - If non-NULL, `handle` becomes signaled | |
| * - The `timeout` expires | |
| * - The wait is interrupted | |
| * | |
| * The `flags` argument can be any combination of `WINPTHREADS_WAIT_*` flags | |
| * defined above. | |
| * | |
| * The `timeout` argument specifies the wait timeout; it can also be one of | |
| * the following special values: | |
| * | |
| * 1. `NULL` or `WINPTHREADS_WAIT_TIMEOUT_ZERO`: do not block execution; | |
| * only check whether `handle` is signaled. | |
| * | |
| * 2. `WINPTHREADS_WAIT_TIMEOUT_INDEFINITE`: block execution indefinitely | |
| * until either `handle` becomes signaled or the wait is interrupted. | |
| * | |
| * If `remain` is non-NULL, and the wait is interrupted before `timeout` | |
| * has expired, this parameter receives remaining wait time. | |
| */ | |
| winpthreads_wait_status winpthreads_wait (void *handle, int flags, clockid_t clock, const struct _timespec64 *timeout, struct _timespec64 *remain); | |
| /* Internal headers */ | |
| #include "barrier.h" | |
| #include "cond.h" | |
| #include "rwlock.h" | |
| #include "sem.h" | |
| #include "thread.h" | |
| #include "winpthreads-time.h" | |
| #endif |