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