thread_clock.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // boost thread_clock.cpp -----------------------------------------------------------//
  2. // Copyright Beman Dawes 1994, 2006, 2008
  3. // Copyright Vicente J. Botet Escriba 2009-2011
  4. // Copyright Christopher Brown 2013
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. // See http://www.boost.org/libs/chrono for documentation.
  8. //--------------------------------------------------------------------------------------//
  9. #include <boost/chrono/config.hpp>
  10. #include <boost/chrono/thread_clock.hpp>
  11. #include <cassert>
  12. #include <boost/assert.hpp>
  13. # include <pthread.h>
  14. # include <mach/thread_act.h>
  15. namespace boost { namespace chrono {
  16. thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
  17. {
  18. // get the thread port (borrowing pthread's reference)
  19. mach_port_t port = pthread_mach_thread_np(pthread_self());
  20. // get the thread info
  21. thread_basic_info_data_t info;
  22. mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
  23. if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS )
  24. {
  25. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  26. return time_point();
  27. }
  28. // convert to nanoseconds
  29. duration user = duration(
  30. static_cast<thread_clock::rep>( info.user_time.seconds ) * 1000000000
  31. + static_cast<thread_clock::rep>(info.user_time.microseconds ) * 1000);
  32. duration system = duration(
  33. static_cast<thread_clock::rep>( info.system_time.seconds ) * 1000000000
  34. + static_cast<thread_clock::rep>( info.system_time.microseconds ) * 1000);
  35. return time_point( user + system );
  36. }
  37. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  38. thread_clock::time_point thread_clock::now( system::error_code & ec )
  39. {
  40. // get the thread port (borrowing pthread's reference)
  41. mach_port_t port = pthread_mach_thread_np(pthread_self());
  42. // get the thread info
  43. thread_basic_info_data_t info;
  44. mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
  45. if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS )
  46. {
  47. if (::boost::chrono::is_throws(ec))
  48. {
  49. boost::throw_exception(
  50. system::system_error(
  51. EINVAL,
  52. ::boost::system::system_category(),
  53. "chrono::thread_clock" ));
  54. }
  55. else
  56. {
  57. ec.assign( errno, ::boost::system::system_category() );
  58. return time_point();
  59. }
  60. }
  61. if (!::boost::chrono::is_throws(ec))
  62. {
  63. ec.clear();
  64. }
  65. // convert to nanoseconds
  66. duration user = duration(
  67. static_cast<thread_clock::rep>( info.user_time.seconds ) * 1000000000
  68. + static_cast<thread_clock::rep>(info.user_time.microseconds ) * 1000);
  69. duration system = duration(
  70. static_cast<thread_clock::rep>( info.system_time.seconds ) * 1000000000
  71. + static_cast<thread_clock::rep>( info.system_time.microseconds ) * 1000);
  72. return time_point( user + system );
  73. }
  74. #endif
  75. } }