diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index c183a6ea7..f826131a2 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -383,7 +383,7 @@ namespace } list pieces; - ptime now = time_now(); + time_point now = clock_type::now(); for (std::vector::iterator i = ret.begin() , end(ret.end()); i != end; ++i) { diff --git a/include/libtorrent/aux_/time.hpp b/include/libtorrent/aux_/time.hpp index a9afb894b..d433340e9 100644 --- a/include/libtorrent/aux_/time.hpp +++ b/include/libtorrent/aux_/time.hpp @@ -35,14 +35,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/time.hpp" -#include -#include namespace libtorrent { namespace aux { // returns the current time, as represented by time_point. The // resolution of this timer is about 100 ms. - time_point const& time_now(); + time_point time_now(); TORRENT_EXTRA_EXPORT void update_time_now(); diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index dd579f699..95cc36c65 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -106,6 +106,10 @@ POSSIBILITY OF SUCH DAMAGE. # define TORRENT_DEPRECATED __attribute__ ((deprecated)) # endif +# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 8) +# define TORRENT_NO_ATOMIC_DURATION 1 +# endif + // ======= SUNPRO ========= #elif defined __SUNPRO_CC @@ -542,6 +546,12 @@ int snprintf(char* buf, int len, char const* fmt, ...) #define TORRENT_NO_FPU 0 #endif +// defined to 1 if the compiler does not support putting +// std::chrono::time_point in a std::atomic (which it's supposed to) +#ifndef TORRENT_NO_ATOMIC_DURATION +#define TORRENT_NO_ATOMIC_DURATION 0 +#endif + #ifndef TORRENT_USE_IOSTREAM #ifndef BOOST_NO_IOSTREAM #define TORRENT_USE_IOSTREAM 1 diff --git a/src/time.cpp b/src/time.cpp index 8975cb0ea..c233df0e0 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -30,30 +30,29 @@ POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include -#include -#include -#include "libtorrent/config.hpp" #include "libtorrent/time.hpp" #include "libtorrent/aux_/time.hpp" +#include namespace libtorrent { namespace aux { - // used to cache the current time - // every 100 ms. This is cheaper - // than a system call and can be - // used where more accurate time - // is not necessary + // used to cache the current time regularly (update_time_now() is called by + // the session_impl main thread). This is cheaper than a system call and can + // be used where more accurate time is not necessary +#if !TORRENT_NO_ATOMIC_DURATION namespace { - time_point g_current_time = clock_type::now(); + std::atomic g_current_time(clock_type::now()); } - - time_point const& time_now() { return aux::g_current_time; } - - void update_time_now() { g_current_time = clock_type::now(); } - + time_point time_now() { return aux::g_current_time.load(); } + void update_time_now() { g_current_time.store(clock_type::now()); } +#else + // work-around for not being able to put time_point in std::atomic + namespace { + std::atomic g_current_time(clock_type::now().time_since_epoch().count()); + } + time_point time_now() { return time_point(clock_type::duration(aux::g_current_time.load())); } + void update_time_now() { g_current_time.store(clock_type::now().time_since_epoch().count()); } +#endif } }