github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/runners/worker/crossplatform_time.h (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements.  See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership.  The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * "License"); you may not use this file except in compliance
     8   * with the License.  You may obtain a copy of the License at
     9   *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  #ifndef BEAM_CROSSPLATFORM_TIME_H
    20  #define BEAM_CROSSPLATFORM_TIME_H
    21  
    22  #include <time.h>
    23  
    24  #ifdef _WIN32
    25  #include <windows.h>
    26  
    27  /**
    28   * Alternative to POSIX clock_gettime that may be run on Windows platform. The clk_id parameter is
    29   * ignored and function act as for CLOCK_MONOTONIC. On Windows XP or later Performance Counter is used.
    30   * On older platforms, where there's no Performance Counter, SystemTime will be used as a failover.
    31   */
    32  int clock_gettime(int clk_id, struct timespec *tv) {
    33      static LARGE_INTEGER counterFrequency = {0};
    34      static BOOL performanceCounterAvailable = TRUE;
    35      LARGE_INTEGER counterValue = {0};
    36  
    37      //initialization
    38      if (0 == counterFrequency.QuadPart) {
    39          if (0 == QueryPerformanceFrequency(&counterFrequency)) {
    40              performanceCounterAvailable = FALSE;
    41              counterFrequency.QuadPart = 10000000; // failover SystemTime is provided in 100-nanosecond intervals
    42          }
    43      }
    44  
    45      if (performanceCounterAvailable) {
    46          QueryPerformanceCounter(&counterValue);
    47      }
    48      else {
    49          FILETIME filetime = {0};
    50          GetSystemTimeAsFileTime(&filetime);
    51          counterValue.QuadPart = filetime.dwHighDateTime;
    52          counterValue.QuadPart <<= 32;
    53          counterValue.QuadPart |= filetime.dwLowDateTime;
    54      }
    55      tv->tv_sec = counterValue.QuadPart / counterFrequency.QuadPart;
    56      #pragma warning( suppress : 4244 ) // nanoseconds may not exceed billion, therefore it's safe to cast
    57      tv->tv_nsec = ((counterValue.QuadPart % counterFrequency.QuadPart) * 1000000000) / counterFrequency.QuadPart;
    58  
    59      return 0;
    60  }
    61  #endif
    62  
    63  #endif //BEAM_CROSSPLATFORM_TIME_H