summaryrefslogtreecommitdiff
path: root/boost/thread/detail/platform_time.hpp
blob: 2180f13c056b9b2b0aae3a28746a10a215fb58a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#ifndef BOOST_THREAD_DETAIL_PLATFORM_TIME_HPP
#define BOOST_THREAD_DETAIL_PLATFORM_TIME_HPP
//  (C) Copyright 2007-8 Anthony Williams
//  (C) Copyright 2012 Vicente J. Botet Escriba
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#include <boost/thread/detail/config.hpp>
#include <boost/thread/thread_time.hpp>
#if defined BOOST_THREAD_USES_DATETIME
#include <boost/date_time/posix_time/conversion.hpp>
#endif
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef BOOST_THREAD_USES_CHRONO
#include <boost/chrono/duration.hpp>
#include <boost/chrono/system_clocks.hpp>
#include <boost/chrono/ceil.hpp>
#endif

#if defined(BOOST_THREAD_CHRONO_WINDOWS_API)
#include <boost/detail/winapi/time.hpp>
#include <boost/detail/winapi/timers.hpp>
#include <boost/thread/win32/thread_primitives.hpp>
#elif defined(BOOST_THREAD_CHRONO_MAC_API)
#include <sys/time.h> //for gettimeofday and timeval
#include <mach/mach_time.h>  // mach_absolute_time, mach_timebase_info_data_t

#else
#include <time.h>  // for clock_gettime
#endif

#include <limits>

#include <boost/config/abi_prefix.hpp>

namespace boost
{
//typedef boost::int_least64_t time_max_t;
typedef boost::intmax_t time_max_t;

#if defined BOOST_THREAD_CHRONO_MAC_API
namespace threads
{

namespace chrono_details
{

// steady_clock

// Note, in this implementation steady_clock and high_resolution_clock
//   are the same clock.  They are both based on mach_absolute_time().
//   mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
//   nanoseconds since the computer booted up.  MachInfo.numer and MachInfo.denom
//   are run time constants supplied by the OS.  This clock has no relationship
//   to the Gregorian calendar.  It's main use is as a high resolution timer.

// MachInfo.numer / MachInfo.denom is often 1 on the latest equipment.  Specialize
//   for that case as an optimization.

inline time_max_t
steady_simplified()
{
    return mach_absolute_time();
}

inline double compute_steady_factor(kern_return_t& err)
{
    mach_timebase_info_data_t MachInfo;
    err = mach_timebase_info(&MachInfo);
    if ( err != 0  ) {
        return 0;
    }
    return static_cast<double>(MachInfo.numer) / MachInfo.denom;
}

inline time_max_t steady_full()
{
    kern_return_t err;
    const double factor = chrono_details::compute_steady_factor(err);
    if (err != 0)
    {
      BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
    }
    return static_cast<time_max_t>(mach_absolute_time() * factor);
}


typedef time_max_t (*FP)();

inline FP init_steady_clock(kern_return_t & err)
{
    mach_timebase_info_data_t MachInfo;
    err = mach_timebase_info(&MachInfo);
    if ( err != 0  )
    {
        return 0;
    }

    if (MachInfo.numer == MachInfo.denom)
    {
        return &chrono_details::steady_simplified;
    }
    return &chrono_details::steady_full;
}

}
}
#endif

  namespace detail
  {
#if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
    inline timespec ns_to_timespec(boost::time_max_t const& ns)
    {
      boost::time_max_t s = ns / 1000000000l;
      timespec ts;
      ts.tv_sec = static_cast<long> (s);
      ts.tv_nsec = static_cast<long> (ns - s * 1000000000l);
      return ts;
    }
    inline boost::time_max_t timespec_to_ns(timespec const& ts)
    {
      return static_cast<boost::time_max_t>(ts.tv_sec) * 1000000000l + ts.tv_nsec;
    }
#endif

    struct platform_duration
    {
#if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
      explicit platform_duration(timespec const& v) : ts_val(v) {}
      timespec const& getTs() const { return ts_val; }

      explicit platform_duration(boost::time_max_t const& ns = 0) : ts_val(ns_to_timespec(ns)) {}
      boost::time_max_t getNs() const { return timespec_to_ns(ts_val); }
#else
      explicit platform_duration(boost::time_max_t const& ns = 0) : ns_val(ns) {}
      boost::time_max_t getNs() const { return ns_val; }
#endif

#if defined BOOST_THREAD_USES_DATETIME
      platform_duration(boost::posix_time::time_duration const& rel_time)
      {
#if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
        ts_val.tv_sec = rel_time.total_seconds();
        ts_val.tv_nsec = static_cast<long>(rel_time.fractional_seconds() * (1000000000l / rel_time.ticks_per_second()));
#else
        ns_val = static_cast<boost::time_max_t>(rel_time.total_seconds()) * 1000000000l;
        ns_val += rel_time.fractional_seconds() * (1000000000l / rel_time.ticks_per_second());
#endif
      }
#endif

#if defined BOOST_THREAD_USES_CHRONO
      template <class Rep, class Period>
      platform_duration(chrono::duration<Rep, Period> const& d)
      {
#if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
        ts_val = ns_to_timespec(chrono::ceil<chrono::nanoseconds>(d).count());
#else
        ns_val = chrono::ceil<chrono::nanoseconds>(d).count();
#endif
      }
#endif

      boost::time_max_t getMs() const
      {
        const boost::time_max_t ns = getNs();
        // ceil/floor away from zero
        if (ns >= 0)
        {
          // return ceiling of positive numbers
          return (ns + 999999) / 1000000;
        }
        else
        {
          // return floor of negative numbers
          return (ns - 999999) / 1000000;
        }
      }

      static platform_duration zero()
      {
        return platform_duration(0);
      }

    private:
#if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
      timespec ts_val;
#else
      boost::time_max_t ns_val;
#endif
    };

    inline bool operator==(platform_duration const& lhs, platform_duration const& rhs)
    {
      return lhs.getNs() == rhs.getNs();
    }
    inline bool operator!=(platform_duration const& lhs, platform_duration const& rhs)
    {
      return lhs.getNs() != rhs.getNs();
    }
    inline bool operator<(platform_duration const& lhs, platform_duration const& rhs)
    {
      return lhs.getNs() < rhs.getNs();
    }
    inline bool operator<=(platform_duration const& lhs, platform_duration const& rhs)
    {
      return lhs.getNs() <= rhs.getNs();
    }
    inline bool operator>(platform_duration const& lhs, platform_duration const& rhs)
    {
      return lhs.getNs() > rhs.getNs();
    }
    inline bool operator>=(platform_duration const& lhs, platform_duration const& rhs)
    {
      return lhs.getNs() >= rhs.getNs();
    }

    static inline platform_duration platform_milliseconds(long const& ms)
    {
      return platform_duration(ms * 1000000l);
    }

    struct real_platform_timepoint
    {
#if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
      explicit real_platform_timepoint(timespec const& v) : dur(v) {}
      timespec const& getTs() const { return dur.getTs(); }
#endif

      explicit real_platform_timepoint(boost::time_max_t const& ns) : dur(ns) {}
      boost::time_max_t getNs() const { return dur.getNs(); }

#if defined BOOST_THREAD_USES_DATETIME
      real_platform_timepoint(boost::system_time const& abs_time)
        : dur(abs_time - boost::posix_time::from_time_t(0)) {}
#endif

#if defined BOOST_THREAD_USES_CHRONO
      template <class Duration>
      real_platform_timepoint(chrono::time_point<chrono::system_clock, Duration> const& abs_time)
        : dur(abs_time.time_since_epoch()) {}
#endif

    private:
      platform_duration dur;
    };

    inline bool operator==(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
    {
      return lhs.getNs() == rhs.getNs();
    }
    inline bool operator!=(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
    {
      return lhs.getNs() != rhs.getNs();
    }
    inline bool operator<(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
    {
      return lhs.getNs() < rhs.getNs();
    }
    inline bool operator<=(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
    {
      return lhs.getNs() <= rhs.getNs();
    }
    inline bool operator>(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
    {
      return lhs.getNs() > rhs.getNs();
    }
    inline bool operator>=(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
    {
      return lhs.getNs() >= rhs.getNs();
    }

    inline real_platform_timepoint operator+(real_platform_timepoint const& lhs, platform_duration const& rhs)
    {
      return real_platform_timepoint(lhs.getNs() + rhs.getNs());
    }
    inline real_platform_timepoint operator+(platform_duration const& lhs, real_platform_timepoint const& rhs)
    {
      return real_platform_timepoint(lhs.getNs() + rhs.getNs());
    }
    inline platform_duration operator-(real_platform_timepoint const& lhs, real_platform_timepoint const& rhs)
    {
      return platform_duration(lhs.getNs() - rhs.getNs());
    }

    struct real_platform_clock
    {
      static real_platform_timepoint now()
      {
#if defined(BOOST_THREAD_CHRONO_WINDOWS_API)
        boost::detail::winapi::FILETIME_ ft;
        boost::detail::winapi::GetSystemTimeAsFileTime(&ft);  // never fails
        boost::time_max_t ns = ((((static_cast<boost::time_max_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) - 116444736000000000LL) * 100LL);
        return real_platform_timepoint(ns);
#elif defined(BOOST_THREAD_CHRONO_MAC_API)
        timeval tv;
        ::gettimeofday(&tv, 0);
        timespec ts;
        ts.tv_sec = tv.tv_sec;
        ts.tv_nsec = tv.tv_usec * 1000;
        return real_platform_timepoint(ts);
#else
        timespec ts;
        if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
        {
          BOOST_ASSERT(0 && "Boost::Thread - clock_gettime(CLOCK_REALTIME) Internal Error");
          return real_platform_timepoint(0);
        }
        return real_platform_timepoint(ts);
#endif
      }
    };

#if defined(BOOST_THREAD_HAS_MONO_CLOCK)

  struct mono_platform_timepoint
  {
#if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API

    explicit mono_platform_timepoint(timespec const& v) : dur(v) {}
    timespec const& getTs() const { return dur.getTs(); }
#endif

    explicit mono_platform_timepoint(boost::time_max_t const& ns) : dur(ns) {}
    boost::time_max_t getNs() const { return dur.getNs(); }

#if defined BOOST_THREAD_USES_CHRONO
    // This conversion assumes that chrono::steady_clock::time_point and mono_platform_timepoint share the same epoch.
    template <class Duration>
    mono_platform_timepoint(chrono::time_point<chrono::steady_clock, Duration> const& abs_time)
      : dur(abs_time.time_since_epoch()) {}
#endif

    // can't name this max() since that is a macro on some Windows systems
    static mono_platform_timepoint getMax()
    {
#if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API
      timespec ts;
      ts.tv_sec = (std::numeric_limits<time_t>::max)();
      ts.tv_nsec = 999999999;
      return mono_platform_timepoint(ts);
#else
      boost::time_max_t ns = (std::numeric_limits<boost::time_max_t>::max)();
      return mono_platform_timepoint(ns);
#endif
    }

  private:
    platform_duration dur;
  };

  inline bool operator==(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  {
    return lhs.getNs() == rhs.getNs();
  }
  inline bool operator!=(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  {
    return lhs.getNs() != rhs.getNs();
  }
  inline bool operator<(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  {
    return lhs.getNs() < rhs.getNs();
  }
  inline bool operator<=(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  {
    return lhs.getNs() <= rhs.getNs();
  }
  inline bool operator>(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  {
    return lhs.getNs() > rhs.getNs();
  }
  inline bool operator>=(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  {
    return lhs.getNs() >= rhs.getNs();
  }

  inline mono_platform_timepoint operator+(mono_platform_timepoint const& lhs, platform_duration const& rhs)
  {
    return mono_platform_timepoint(lhs.getNs() + rhs.getNs());
  }
  inline mono_platform_timepoint operator+(platform_duration const& lhs, mono_platform_timepoint const& rhs)
  {
    return mono_platform_timepoint(lhs.getNs() + rhs.getNs());
  }
  inline platform_duration operator-(mono_platform_timepoint const& lhs, mono_platform_timepoint const& rhs)
  {
    return platform_duration(lhs.getNs() - rhs.getNs());
  }

  struct mono_platform_clock
  {
    static mono_platform_timepoint now()
    {
#if defined(BOOST_THREAD_CHRONO_WINDOWS_API)
#if defined(BOOST_THREAD_USES_CHRONO)
      // Use QueryPerformanceCounter() to match the implementation in Boost
      // Chrono so that chrono::steady_clock::now() and this function share the
      // same epoch and so can be converted between each other.
      boost::detail::winapi::LARGE_INTEGER_ freq;
      if ( !boost::detail::winapi::QueryPerformanceFrequency( &freq ) )
      {
        BOOST_ASSERT(0 && "Boost::Thread - QueryPerformanceFrequency Internal Error");
        return mono_platform_timepoint(0);
      }
      if ( freq.QuadPart <= 0 )
      {
        BOOST_ASSERT(0 && "Boost::Thread - QueryPerformanceFrequency Internal Error");
        return mono_platform_timepoint(0);
      }

      boost::detail::winapi::LARGE_INTEGER_ pcount;
      unsigned times=0;
      while ( ! boost::detail::winapi::QueryPerformanceCounter( &pcount ) )
      {
        if ( ++times > 3 )
        {
          BOOST_ASSERT(0 && "Boost::Thread - QueryPerformanceCounter Internal Error");
          return mono_platform_timepoint(0);
        }
      }

      long double ns = 1000000000.0L * pcount.QuadPart / freq.QuadPart;
      return mono_platform_timepoint(static_cast<boost::time_max_t>(ns));
#else
      // Use GetTickCount64() because it's more reliable on older
      // systems like Windows XP and Windows Server 2003.
      win32::ticks_type msec = win32::gettickcount64();
      return mono_platform_timepoint(msec * 1000000);
#endif
#elif defined(BOOST_THREAD_CHRONO_MAC_API)
      kern_return_t err;
      threads::chrono_details::FP fp = threads::chrono_details::init_steady_clock(err);
      if ( err != 0  )
      {
        BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
      }
      return mono_platform_timepoint(fp());
#else
      timespec ts;
      if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
      {
        BOOST_ASSERT(0 && "Boost::Thread - clock_gettime(CLOCK_MONOTONIC) Internal Error");
        return mono_platform_timepoint(0);
      }
      return mono_platform_timepoint(ts);
#endif
    }
  };

#endif

#if defined(BOOST_THREAD_INTERNAL_CLOCK_IS_MONO)
  typedef mono_platform_clock     internal_platform_clock;
  typedef mono_platform_timepoint internal_platform_timepoint;
#else
  typedef real_platform_clock     internal_platform_clock;
  typedef real_platform_timepoint internal_platform_timepoint;
#endif

#ifdef BOOST_THREAD_USES_CHRONO
#ifdef BOOST_THREAD_INTERNAL_CLOCK_IS_MONO
  typedef chrono::steady_clock internal_chrono_clock;
#else
  typedef chrono::system_clock internal_chrono_clock;
#endif
#endif

  }
}

#include <boost/config/abi_suffix.hpp>

#endif