1/*
2 Copyright (c) 2011-2016 mingw-w64 project
3
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 DEALINGS IN THE SOFTWARE.
21*/
22
23#ifndef WIN_PTHREADS_MISC_H
24#define WIN_PTHREADS_MISC_H
25
26#include <limits.h>
27/* public header files */
28#include "pthread_compat.h"
29
30#define PTR2INT(x) ((int)(uintptr_t)(x))
31
32#if SIZE_MAX>UINT_MAX
33typedef long long LONGBAG;
34#else
35typedef long LONGBAG;
36#endif
37
38extern BOOL (WINAPI *_pthread_get_handle_information) (HANDLE, LPDWORD);
39
40/* For gcc and clang define DUMMY_WRITABLE_DWORD as C99 compound literal.
41 * For other pre-C99 compilers declare DUMMY_WRITABLE_DWORD as static variable.
42 */
43#if defined(__GNUC__) || defined(__clang__)
44#define DUMMY_WRITABLE_DWORD (DWORD){0}
45#else
46static DWORD DUMMY_WRITABLE_DWORD;
47#endif
48
49#define TEST_HANDLE(h) \
50 (((h) != NULL && (h) != INVALID_HANDLE_VALUE) && ( \
51 _pthread_get_handle_information == NULL || \
52 _pthread_get_handle_information((h), &DUMMY_WRITABLE_DWORD) || \
53 GetLastError() == ERROR_CALL_NOT_IMPLEMENTED \
54 ))
55
56#define CHECK_HANDLE2(h, e) \
57 do { \
58 if (!TEST_HANDLE(h)) \
59 return e; \
60 } while (0)
61
62#define CHECK_HANDLE(h) CHECK_HANDLE2(h, EINVAL)
63
64#define CHECK_PTR(p) do { if (!(p)) return EINVAL; } while (0)
65
66#define UPD_RESULT(x,r) do { int _r = (x); (r) = (r) ? (r) : _r; } while (0)
67
68#define CHECK_THREAD(t) \
69 do { \
70 CHECK_PTR(t); \
71 CHECK_HANDLE((t)->h); \
72 } while (0)
73
74#define CHECK_OBJECT(o, e) \
75 do { \
76 if (!(o)) return e; \
77 CHECK_HANDLE2((o)->h, e); \
78 } while (0)
79
80#define VALID(x) if (!(p)) return EINVAL;
81
82/* ms can be 64 bit, solve wrap-around issues: */
83static WINPTHREADS_INLINE unsigned long dwMilliSecs(unsigned long long ms)
84{
85 if (ms >= 0xffffffffULL) return 0xfffffffful;
86 return (unsigned long) ms;
87}
88
89unsigned long long _pthread_time_in_ms(void);
90unsigned long long _pthread_time_in_ms_from_timespec(const struct _timespec64 *ts);
91unsigned long long _pthread_rel_time_in_ms(const struct _timespec64 *ts);
92unsigned long _pthread_wait_for_single_object (void *handle, unsigned long timeout);
93unsigned long _pthread_wait_for_multiple_objects (unsigned long count, void **handles, unsigned int all, unsigned long timeout);
94
95extern void (WINAPI *_pthread_get_system_time_best_as_file_time) (LPFILETIME);
96extern HRESULT (WINAPI *_pthread_set_thread_description) (HANDLE, PCWSTR);
97
98#if defined(__GNUC__) || defined(__clang__)
99#define likely(cond) __builtin_expect((cond) != 0, 1)
100#define unlikely(cond) __builtin_expect((cond) != 0, 0)
101#else
102#define likely(cond) (cond)
103#define unlikely(cond) (cond)
104#endif
105
106#if defined(__GNUC__) || defined(__clang__)
107#define UNREACHABLE() __builtin_unreachable()
108#elif defined(_MSC_VER)
109#define UNREACHABLE() __assume(0)
110#endif
111
112#endif