libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
hp_timer_impl.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2012 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
20 
21 template<typename Mdl>
22 inline typename hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::value_type
23 hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::to_usec(const time_utc_t ticks) noexcept(true) {
24  const value_type tmp=static_cast<value_type>(ticks.tv_sec)*1000000+ticks.tv_nsec/1000.0;
25  return tmp;
26 }
27 
28 template<typename Mdl>
29 inline const typename hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::time_utc_t
30 hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::get_start_time() noexcept(false) {
31  time_utc_t tm;
32  if (::clock_gettime(CLOCK_REALTIME, &tm)) {
33  throw exception_type(_T("High-performance counter not supported."), info::function(__LINE__,__PRETTY_FUNCTION__,typeid(&hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::get_start_time)), JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
34  }
35  return tm;
36 }
37 
38 template<typename Mdl>
39 inline typename hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::value_type
40 hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::current_count() noexcept(false) {
41  return to_usec(get_start_time());
42 }
43 
44 template<typename Mdl>
45 inline const typename hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::time_utc_t
46 hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::current_time() const noexcept(false) {
47  return get_start_time();
48 }
49 
50 template<typename Mdl>
51 inline
52 hp_timer<ppd::generic_traits::api_type::posix_pthreads, Mdl>::hp_timer() noexcept(false)
53 : start_up_time(get_start_time()),
54  start_up_count(current_count()) {
55 }
56 
57 template <typename T>
58 inline
59 hp_interval<T>::~hp_interval() noexcept(true) {
60  const typename timer_t::time_utc_t end=timer.current_time();
61  if (end.tv_nsec>=start.tv_nsec) {
62  interval.tv_nsec=end.tv_nsec-start.tv_nsec;
63  interval.tv_sec=end.tv_sec-start.tv_sec;
64  } else {
65  interval.tv_nsec=1000000000+(end.tv_nsec-start.tv_nsec);
66  interval.tv_sec=end.tv_sec-start.tv_sec-1;
67  }
68 }
69 
70 } }