1  /* Implementation of threads compatibility routines for libgcc2.  */
       2  
       3  /* Copyright (C) 1999-2023 Free Software Foundation, Inc.
       4  
       5  This file is part of GCC.
       6  
       7  GCC is free software; you can redistribute it and/or modify it under
       8  the terms of the GNU General Public License as published by the Free
       9  Software Foundation; either version 3, or (at your option) any later
      10  version.
      11  
      12  GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13  WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15  for more details.
      16  
      17  Under Section 7 of GPL version 3, you are granted additional
      18  permissions described in the GCC Runtime Library Exception, version
      19  3.1, as published by the Free Software Foundation.
      20  
      21  You should have received a copy of the GNU General Public License and
      22  a copy of the GCC Runtime Library Exception along with this program;
      23  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      24  <http://www.gnu.org/licenses/>.  */
      25  
      26  /* This module is separate from the rest of the implementation because it
      27     references symbols in system libraries that are only available on Vista
      28     and Server 2008 or later versions.  */
      29  
      30  /* Get the out-of-line version of the inline routines.  */
      31  
      32  #if _WIN32_WINNT < 0x0600
      33  #undef _WIN32_WINNT
      34  #define _WIN32_WINNT 0x0600
      35  #endif
      36  
      37  #define __GTHREAD_WIN32_COND_INLINE
      38  
      39  #define __gthread_cond_init_function __gthr_win32_cond_init_function
      40  #define __gthread_cond_broadcast __gthr_win32_cond_broadcast
      41  #define __gthread_cond_signal __gthr_win32_cond_signal
      42  #define __gthread_cond_wait __gthr_win32_cond_wait
      43  #define __gthread_cond_timedwait __gthr_win32_cond_timedwait
      44  
      45  #include "gthr-win32.h"
      46  
      47  /* The number of 100-nanoseconds between 1/1/1601 and 1/1/1970. */
      48  #define FILETIME_1970 116444736000000000ULL
      49  
      50  /* The number of 100-nanoseconds per second.  */
      51  #define NSEC100_PER_SEC (1000000000ULL / 100)
      52  
      53  /* The number of 100-nanoseconds per millisecond.  */
      54  #define NSEC100_PER_MSEC (NSEC100_PER_SEC / 1000)
      55  
      56  /* The ceiling division of X by Y.  */
      57  #define CEIL_DIV(X, Y) (((X) + (Y) - 1) / (Y))
      58  
      59  /* Convert absolute thread time to relative time in millisecond.  */
      60  
      61  DWORD
      62  __gthr_win32_abs_to_rel_time (const __gthread_time_t *abs_time)
      63  {
      64    union {
      65      ULONGLONG nsec100;
      66      FILETIME ft;
      67    } now;
      68    ULONGLONG abs_time_nsec100;
      69  
      70    /* The Windows epoch is 1/1/1601 while the Unix epoch is 1/1/1970.  */
      71    GetSystemTimeAsFileTime (&now.ft);
      72    now.nsec100 -= FILETIME_1970;
      73  
      74    abs_time_nsec100
      75      = (ULONGLONG) abs_time->tv_sec * NSEC100_PER_SEC
      76  	+ CEIL_DIV (abs_time->tv_nsec, 100);
      77  
      78    if (abs_time_nsec100 < now.nsec100)
      79      return 0;
      80  
      81    return (DWORD) CEIL_DIV (abs_time_nsec100 - now.nsec100, NSEC100_PER_SEC);
      82  }
      83  
      84  /* Check the sizes of the local version of the Win32 types.  */
      85  
      86  #define CHECK_SIZE_OF(TYPE) \
      87    typedef int assertion[sizeof(__gthr_win32_##TYPE) == sizeof(TYPE) ? 1 : -1];
      88  
      89  CHECK_SIZE_OF (CONDITION_VARIABLE)