PageRenderTime 10ms CodeModel.GetById 4ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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