Skip to content

Commit

Permalink
mac thread_clock implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ccbrown committed Dec 12, 2013
1 parent 824a12e commit 143260d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/boost/chrono/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
# if defined( BOOST_CHRONO_MAC_API )
# define BOOST_CHRONO_HAS_PROCESS_CLOCKS
# define BOOST_CHRONO_HAS_CLOCK_STEADY
# define BOOST_CHRONO_HAS_THREAD_CLOCK
# define BOOST_CHRONO_THREAD_CLOCK_IS_STEADY true
# endif

Expand Down
80 changes: 78 additions & 2 deletions include/boost/chrono/detail/inlined/mac/thread_clock.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// boost thread_clock.cpp -----------------------------------------------------------//

// Copyright Vicente J. Botet Escriba 2010
// Copyright Beman Dawes 1994, 2006, 2008
// Copyright Vicente J. Botet Escriba 2009-2011
// Copyright Christopher Brown 2013

// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
Expand All @@ -10,6 +12,80 @@
//--------------------------------------------------------------------------------------//

#include <boost/chrono/config.hpp>
#include <boost/chrono/detail/inlined/posix/thread_clock.hpp>
#include <boost/chrono/thread_clock.hpp>
#include <cassert>

# include <pthread.h>
# include <mach/thread_act.h>

namespace boost { namespace chrono {

thread_clock::time_point thread_clock::now( ) BOOST_NOEXCEPT
{
// get the thread port (borrowing pthread's reference)
mach_port_t port = pthread_mach_thread_np(pthread_self());

// get the thread info
thread_basic_info_data_t info;
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS )
{
BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
return time_point();
}

// convert to nanoseconds
duration user = duration(
static_cast<thread_clock::rep>( info.user_time.seconds ) * 1000000000
+ static_cast<thread_clock::rep>(info.user_time.microseconds ) * 1000);

duration system = duration(
static_cast<thread_clock::rep>( info.system_time.seconds ) * 1000000000
+ static_cast<thread_clock::rep>( info.system_time.microseconds ) * 1000);

return time_point( user + system );
}

#if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
thread_clock::time_point thread_clock::now( system::error_code & ec )
{
// get the thread port (borrowing pthread's reference)
mach_port_t port = pthread_mach_thread_np(pthread_self());

// get the thread info
thread_basic_info_data_t info;
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
if ( thread_info(port, THREAD_BASIC_INFO, (thread_info_t)&info, &count) != KERN_SUCCESS )
{
if (BOOST_CHRONO_IS_THROWS(ec))
{
boost::throw_exception(
system::system_error(
EINVAL,
BOOST_CHRONO_SYSTEM_CATEGORY,
"chrono::thread_clock" ));
}
else
{
ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
return time_point();
}
}
if (!BOOST_CHRONO_IS_THROWS(ec))
{
ec.clear();
}

// convert to nanoseconds
duration user = duration(
static_cast<thread_clock::rep>( info.user_time.seconds ) * 1000000000
+ static_cast<thread_clock::rep>(info.user_time.microseconds ) * 1000);

duration system = duration(
static_cast<thread_clock::rep>( info.system_time.seconds ) * 1000000000
+ static_cast<thread_clock::rep>( info.system_time.microseconds ) * 1000);

return time_point( user + system );
}
#endif
} }

0 comments on commit 143260d

Please sign in to comment.