2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief ClockService inline non-template implementation */
19 #include <boost/date_time/posix_time/posix_time_types.hpp>
20 #include <senf/Utils/Exception.hh>
21 #include <senf/Utils/senflikely.hh>
23 #define prefix_ inline
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 # define SENF_CLOCKTYPEVAL(clock) (clock).value()
29 # define SENF_CLOCKTYPEVAL(clock) (clock)
32 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35 prefix_ senf::ClockService::clock_type senf::ClockService::now()
38 clock_gettime(CLOCK_MONOTONIC, &spec);
39 return clock_type(spec.tv_sec * 1000000000LL + spec.tv_nsec);
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 //namespace senf { namespace scheduler { ClockService::clock_type now(); } }
51 prefix_ senf::ClockService::ClockService()
56 prefix_ void senf::ClockService::restart_m()
58 baseAbstime_ = boost::posix_time::microsec_clock::universal_time();
64 prefix_ senf::ClockService::abstime_type senf::ClockService::abstime(clock_type const & clock)
66 return instance().abstime_m(clock);
69 prefix_ senf::ClockService::reltime_type senf::ClockService::reltime(clock_type const & clock)
71 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
72 return boost::posix_time::nanoseconds(
73 SENF_CLOCKTYPEVAL( clock));
75 return boost::posix_time::microseconds(
76 SENF_CLOCKTYPEVAL( (clock + clock_type(500))/1000));
80 prefix_ senf::ClockService::clock_type senf::ClockService::clock(abstime_type const & time)
82 return instance().clock_m(time);
85 prefix_ senf::ClockService::clock_type senf::ClockService::from_time_t(time_t const & time)
87 return clock( boost::posix_time::from_time_t(time) );
90 prefix_ time_t senf::ClockService::to_time_t(senf::ClockService::clock_type const & time)
92 return (abstime(time) - boost::posix_time::from_time_t(0)).total_seconds();
95 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::clock_type senf::ClockService::nanoseconds(int64_type const & v)
100 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::clock_type senf::ClockService::microseconds(int64_type const & v)
102 return v * nanoseconds(1000);
105 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::clock_type senf::ClockService::milliseconds(int64_type const & v)
107 return v * microseconds(1000);
110 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::clock_type senf::ClockService::seconds(int64_type const & v)
112 return v * milliseconds(1000);
115 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::clock_type senf::ClockService::minutes(int64_type const & v)
117 return v * seconds(60);
120 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::clock_type senf::ClockService::hours(int64_type const & v)
122 return v * minutes(60);
125 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::clock_type senf::ClockService::days(int64_type const & v)
127 return v * hours(24);
130 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::int64_type senf::ClockService::in_nanoseconds(clock_type const & v)
132 return SENF_CLOCKTYPEVAL( v);
135 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::int64_type senf::ClockService::in_microseconds(clock_type const & v)
137 return SENF_CLOCKTYPEVAL( v / nanoseconds(1000));
140 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::int64_type senf::ClockService::in_milliseconds(clock_type const & v)
142 return SENF_CLOCKTYPEVAL( v / microseconds(1000));
145 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::int64_type senf::ClockService::in_seconds(clock_type const & v)
147 return SENF_CLOCKTYPEVAL( v / milliseconds(1000));
150 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::int64_type senf::ClockService::in_minutes(clock_type const & v)
152 return SENF_CLOCKTYPEVAL( v / seconds(60));
155 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::int64_type senf::ClockService::in_hours(clock_type const & v)
157 return SENF_CLOCKTYPEVAL( v / minutes(60));
160 prefix_ SENF_CLOCKSERVICE_CONSTEXPR senf::ClockService::int64_type senf::ClockService::in_days(clock_type const & v)
162 return SENF_CLOCKTYPEVAL( v / hours(24));
165 prefix_ senf::ClockService::clock_type senf::ClockService::from_timeval(timeval const & time)
167 return from_time_t(time.tv_sec) + microseconds(time.tv_usec);
170 prefix_ void senf::ClockService::restart()
172 instance().restart_m();
175 #undef SENF_CLOCKTYPEVAL
177 //-/////////////////////////////////////////////////////////////////////////////////////////////////
184 // comment-column: 40
185 // c-file-style: "senf"
186 // indent-tabs-mode: nil
187 // ispell-local-dictionary: "american"
188 // compile-command: "scons -u test"