thread_clock.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // boost thread_clock.cpp -----------------------------------------------------------//
  2. // Copyright Beman Dawes 1994, 2006, 2008
  3. // Copyright Vicente J. Botet Escriba 2009-2011
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. // See http://www.boost.org/libs/chrono for documentation.
  7. //--------------------------------------------------------------------------------------//
  8. #include <boost/chrono/config.hpp>
  9. #include <boost/chrono/thread_clock.hpp>
  10. #include <cassert>
  11. #include <boost/assert.hpp>
  12. #if !defined(__VXWORKS__)
  13. # include <sys/times.h>
  14. #endif
  15. # include <pthread.h>
  16. # include <unistd.h>
  17. namespace boost { namespace chrono {
  18. thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
  19. {
  20. struct timespec ts;
  21. #if defined CLOCK_THREAD_CPUTIME_ID
  22. // get the timespec associated to the thread clock
  23. if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
  24. #else
  25. // get the current thread
  26. pthread_t pth=pthread_self();
  27. // get the clock_id associated to the current thread
  28. clockid_t clock_id;
  29. pthread_getcpuclockid(pth, &clock_id);
  30. // get the timespec associated to the thread clock
  31. if ( ::clock_gettime( clock_id, &ts ) )
  32. #endif
  33. {
  34. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  35. }
  36. // transform to nanoseconds
  37. return time_point(duration(
  38. static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
  39. }
  40. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  41. thread_clock::time_point thread_clock::now( system::error_code & ec )
  42. {
  43. struct timespec ts;
  44. #if defined CLOCK_THREAD_CPUTIME_ID
  45. // get the timespec associated to the thread clock
  46. if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
  47. #else
  48. // get the current thread
  49. pthread_t pth=pthread_self();
  50. // get the clock_id associated to the current thread
  51. clockid_t clock_id;
  52. pthread_getcpuclockid(pth, &clock_id);
  53. // get the timespec associated to the thread clock
  54. if ( ::clock_gettime( clock_id, &ts ) )
  55. #endif
  56. {
  57. if (::boost::chrono::is_throws(ec))
  58. {
  59. boost::throw_exception(
  60. system::system_error(
  61. errno,
  62. ::boost::system::system_category(),
  63. "chrono::thread_clock" ));
  64. }
  65. else
  66. {
  67. ec.assign( errno, ::boost::system::system_category() );
  68. return time_point();
  69. }
  70. }
  71. if (!::boost::chrono::is_throws(ec))
  72. {
  73. ec.clear();
  74. }
  75. // transform to nanoseconds
  76. return time_point(duration(
  77. static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
  78. }
  79. #endif
  80. } }