master
  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
 38#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
 39#undef GetHandleInformation
 40#define GetHandleInformation(h,f)  (1)
 41#endif
 42
 43#define CHECK_HANDLE(h)                                                 \
 44  do {                                                                  \
 45    DWORD dwFlags;                                                      \
 46    if (!(h) || ((h) == INVALID_HANDLE_VALUE) || !GetHandleInformation((h), &dwFlags)) \
 47      return EINVAL;                                                    \
 48  } while (0)
 49
 50#define CHECK_PTR(p) do { if (!(p)) return EINVAL; } while (0)
 51
 52#define UPD_RESULT(x,r) do { int _r = (x); (r) = (r) ? (r) : _r; } while (0)
 53
 54#define CHECK_THREAD(t)                         \
 55  do {                                          \
 56    CHECK_PTR(t);                               \
 57    CHECK_HANDLE((t)->h);                       \
 58  } while (0)
 59
 60#define CHECK_OBJECT(o, e)                                              \
 61  do {                                                                  \
 62    DWORD dwFlags;                                                      \
 63    if (!(o)) return e;                                                 \
 64    if (!((o)->h) || (((o)->h) == INVALID_HANDLE_VALUE) || !GetHandleInformation(((o)->h), &dwFlags)) \
 65      return e;                                                         \
 66  } while (0)
 67
 68#define VALID(x)    if (!(p)) return EINVAL;
 69
 70/* ms can be 64 bit, solve wrap-around issues: */
 71static WINPTHREADS_INLINE unsigned long dwMilliSecs(unsigned long long ms)
 72{
 73  if (ms >= 0xffffffffULL) return 0xfffffffful;
 74  return (unsigned long) ms;
 75}
 76
 77unsigned long long _pthread_time_in_ms(void);
 78unsigned long long _pthread_time_in_ms_from_timespec(const struct _timespec64 *ts);
 79unsigned long long _pthread_rel_time_in_ms(const struct _timespec64 *ts);
 80unsigned long _pthread_wait_for_single_object (void *handle, unsigned long timeout);
 81unsigned long _pthread_wait_for_multiple_objects (unsigned long count, void **handles, unsigned int all, unsigned long timeout);
 82
 83extern void (WINAPI *_pthread_get_system_time_best_as_file_time) (LPFILETIME);
 84extern HRESULT (WINAPI *_pthread_set_thread_description) (HANDLE, PCWSTR);
 85
 86#if defined(__GNUC__) || defined(__clang__)
 87#define likely(cond) __builtin_expect((cond) != 0, 1)
 88#define unlikely(cond) __builtin_expect((cond) != 0, 0)
 89#else
 90#define likely(cond) (cond)
 91#define unlikely(cond) (cond)
 92#endif
 93
 94#if defined(__GNUC__) || defined(__clang__)
 95#define UNREACHABLE() __builtin_unreachable()
 96#elif defined(_MSC_VER)
 97#define UNREACHABLE() __assume(0)
 98#endif
 99
100#endif