PageRenderTime 46ms CodeModel.GetById 14ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 250 lines | 225 code | 17 blank | 8 comment | 30 complexity | deee4cbdbf8d52ca03c903b7a4712622 MD5 | raw file
  1//  boost process_cpu_clocks.cpp  -----------------------------------------------------------//
  2
  3//  Copyright Beman Dawes 1994, 2006, 2008
  4//  Copyright Vicente J. Botet Escriba 2009
  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/process_cpu_clocks.hpp>
 15#include <boost/assert.hpp>
 16
 17# include <sys/times.h>
 18# include <unistd.h>
 19
 20
 21namespace boost { namespace chrono {
 22namespace chrono_detail
 23{
 24  inline long tick_factor()        // multiplier to convert ticks
 25                            //  to nanoseconds; -1 if unknown
 26  {
 27    static long factor = 0;
 28    if ( !factor )
 29    {
 30      if ( (factor = ::sysconf( _SC_CLK_TCK )) <= 0 )
 31        factor = -1;
 32      else
 33      {
 34        BOOST_ASSERT( factor <= 1000000l ); // doesn't handle large ticks
 35        factor = 1000000l / factor;  // compute factor
 36        if ( !factor ) factor = -1;
 37      }
 38    }
 39    return factor;
 40  }
 41}
 42
 43process_real_cpu_clock::time_point process_real_cpu_clock::now(
 44        system::error_code & ec) 
 45{
 46    
 47    tms tm;
 48    clock_t c = ::times( &tm );
 49    if ( c == clock_t(-1) ) // error
 50    {
 51        if (BOOST_CHRONO_IS_THROWS(ec))
 52        {
 53            boost::throw_exception(
 54                    system::system_error( 
 55                            errno, 
 56                            BOOST_CHRONO_SYSTEM_CATEGORY, 
 57                            "chrono::process_real_cpu_clock" ));
 58        }
 59        else
 60        {
 61            ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
 62            return time_point();
 63        }
 64    }
 65    else
 66    {
 67        if ( chrono_detail::tick_factor() != -1 )
 68        {
 69            if (!BOOST_CHRONO_IS_THROWS(ec)) 
 70            {
 71                ec.clear();
 72            }
 73            return time_point(
 74                    microseconds(c)*chrono_detail::tick_factor());
 75        }
 76        else
 77        {
 78            if (BOOST_CHRONO_IS_THROWS(ec))
 79            {
 80                boost::throw_exception(
 81                        system::system_error( 
 82                                errno, 
 83                                BOOST_CHRONO_SYSTEM_CATEGORY, 
 84                                "chrono::process_real_cpu_clock" ));
 85            }
 86            else
 87            {
 88                ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
 89                return time_point();
 90            }
 91        }
 92    }
 93}
 94
 95process_user_cpu_clock::time_point process_user_cpu_clock::now(
 96        system::error_code & ec)
 97{
 98    tms tm;
 99    clock_t c = ::times( &tm );
100    if ( c == clock_t(-1) ) // error
101    {
102        if (BOOST_CHRONO_IS_THROWS(ec))
103        {
104            boost::throw_exception(
105                    system::system_error( 
106                            errno, 
107                            BOOST_CHRONO_SYSTEM_CATEGORY, 
108                            "chrono::process_user_cpu_clock" ));
109        }
110        else
111        {
112            ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
113            return time_point();
114        }
115    }
116    else
117    {
118        if ( chrono_detail::tick_factor() != -1 )
119        {
120            if (!BOOST_CHRONO_IS_THROWS(ec)) 
121            {
122                ec.clear();
123            }
124            return time_point(
125                    microseconds(tm.tms_utime + tm.tms_cutime)*chrono_detail::tick_factor());
126        }
127        else
128        {
129            if (BOOST_CHRONO_IS_THROWS(ec))
130            {
131                boost::throw_exception(
132                        system::system_error( 
133                                errno, 
134                                BOOST_CHRONO_SYSTEM_CATEGORY, 
135                                "chrono::process_user_cpu_clock" ));
136            }
137            else
138            {
139                ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
140                return time_point();
141            }
142        }
143    }
144}
145
146process_system_cpu_clock::time_point process_system_cpu_clock::now(
147        system::error_code & ec) 
148{
149    tms tm;
150    clock_t c = ::times( &tm );
151    if ( c == clock_t(-1) ) // error
152    {
153        if (BOOST_CHRONO_IS_THROWS(ec))
154        {
155            boost::throw_exception(
156                    system::system_error( 
157                            errno, 
158                            BOOST_CHRONO_SYSTEM_CATEGORY, 
159                            "chrono::process_system_cpu_clock" ));
160        }
161        else
162        {
163            ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
164            return time_point();
165        }
166    }
167    else
168    {
169        if ( chrono_detail::tick_factor() != -1 )
170        {
171            if (!BOOST_CHRONO_IS_THROWS(ec)) 
172            {
173                ec.clear();
174            }
175            return time_point(
176                    microseconds(tm.tms_stime + tm.tms_cstime)*chrono_detail::tick_factor());
177        }
178        else
179        {
180            if (BOOST_CHRONO_IS_THROWS(ec))
181            {
182                boost::throw_exception(
183                        system::system_error( 
184                                errno, 
185                                BOOST_CHRONO_SYSTEM_CATEGORY, 
186                                "chrono::process_system_cpu_clock" ));
187            }
188            else
189            {
190                ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
191                return time_point();
192            }
193        }
194    }
195}
196
197process_cpu_clock::time_point process_cpu_clock::now( 
198        system::error_code & ec ) 
199{
200    
201    
202    tms tm;
203    clock_t c = ::times( &tm );
204    if ( c == clock_t(-1) ) // error
205    {
206        if (BOOST_CHRONO_IS_THROWS(ec))
207        {
208            boost::throw_exception(
209                    system::system_error( 
210                            errno, 
211                            BOOST_CHRONO_SYSTEM_CATEGORY, 
212                            "chrono::process_clock" ));
213        }
214        else
215        {
216            ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
217            return time_point();
218        }
219    }
220    else
221    {
222        if ( chrono_detail::tick_factor() != -1 )
223        {
224            time_point::rep r(
225                    c*chrono_detail::tick_factor(), 
226                    (tm.tms_utime + tm.tms_cutime)*chrono_detail::tick_factor(), 
227                    (tm.tms_stime + tm.tms_cstime)*chrono_detail::tick_factor());
228            return time_point(duration(r));
229        }
230        else
231        {
232            if (BOOST_CHRONO_IS_THROWS(ec))
233            {
234                boost::throw_exception(
235                        system::system_error( 
236                                errno, 
237                                BOOST_CHRONO_SYSTEM_CATEGORY, 
238                                "chrono::process_clock" ));
239            }
240            else
241            {
242                ec.assign( errno, BOOST_CHRONO_SYSTEM_CATEGORY );
243                return time_point();
244            }
245        }
246    }
247    
248}
249
250} }