thread_clock.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #if !defined(__VXWORKS__)
  12. # include <sys/times.h>
  13. #endif
  14. # include <pthread.h>
  15. # include <unistd.h>
  16. namespace boost { namespace chrono {
  17. thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
  18. {
  19. struct timespec ts;
  20. #if defined CLOCK_THREAD_CPUTIME_ID
  21. // get the timespec associated to the thread clock
  22. if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
  23. #else
  24. // get the current thread
  25. pthread_t pth=pthread_self();
  26. // get the clock_id associated to the current thread
  27. clockid_t clock_id;
  28. pthread_getcpuclockid(pth, &clock_id);
  29. // get the timespec associated to the thread clock
  30. if ( ::clock_gettime( clock_id, &ts ) )
  31. #endif
  32. {
  33. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  34. }
  35. // transform to nanoseconds
  36. return time_point(duration(
  37. static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
  38. }
  39. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  40. thread_clock::time_point thread_clock::now( system::error_code & ec )
  41. {
  42. struct timespec ts;
  43. #if defined CLOCK_THREAD_CPUTIME_ID
  44. // get the timespec associated to the thread clock
  45. if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
  46. #else
  47. // get the current thread
  48. pthread_t pth=pthread_self();
  49. // get the clock_id associated to the current thread
  50. clockid_t clock_id;
  51. pthread_getcpuclockid(pth, &clock_id);
  52. // get the timespec associated to the thread clock
  53. if ( ::clock_gettime( clock_id, &ts ) )
  54. #endif
  55. {
  56. if (BOOST_CHRONO_IS_THROWS(ec))
  57. {
  58. boost::throw_exception(
  59. system::system_error(
  60. errno,
  61. BOOST_CHRONO_SYSTEM_CATEGORY,
  62. "chrono::thread_clock" ));
  63. }
  64. else
  65. {
  66. ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
  67. return time_point();
  68. }
  69. }
  70. if (!BOOST_CHRONO_IS_THROWS(ec))
  71. {
  72. ec.clear();
  73. }
  74. // transform to nanoseconds
  75. return time_point(duration(
  76. static_cast<thread_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
  77. }
  78. #endif
  79. } }