Ticket #7726: thread_clock.hpp

File thread_clock.hpp, 2.9 KB (added by p.brockamp@…, 10 years ago)

Chrono

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