/Src/Dependencies/Boost/boost/chrono/detail/inlined/posix/thread_clock.hpp

http://hadesmem.googlecode.com/ · C++ Header · 89 lines · 61 code · 11 blank · 17 comment · 6 complexity · 3ba633839e3cca53f31c5e11c0c68b91 MD5 · raw file

  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 <sys/times.h>
  12. # include <unistd.h>
  13. namespace boost { namespace chrono {
  14. thread_clock::time_point thread_clock::now( )
  15. {
  16. struct timespec ts;
  17. #if defined CLOCK_THREAD_CPUTIME_ID
  18. // get the timespec associated to the thread clock
  19. if ( ::clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts ) )
  20. #else
  21. // get the current thread
  22. pthread_t pth=pthread_self();
  23. // get the clock_id associated to the current thread
  24. clockid_t clock_id;
  25. pthread_getcpuclockid(pth, &clock_id);
  26. // get the timespec associated to the thread clock
  27. if ( ::clock_gettime( clock_id, &ts ) )
  28. #endif
  29. {
  30. boost::throw_exception(
  31. system::system_error(
  32. errno,
  33. BOOST_CHRONO_SYSTEM_CATEGORY,
  34. "chrono::thread_clock" ));
  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. 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. } }