make the cached current time thread-safe

This commit is contained in:
arvidn 2016-04-30 18:35:23 -04:00
parent fef94a4c52
commit fc4cc9e7ee
4 changed files with 28 additions and 21 deletions

View File

@ -383,7 +383,7 @@ namespace
} }
list pieces; list pieces;
ptime now = time_now(); time_point now = clock_type::now();
for (std::vector<cached_piece_info>::iterator i = ret.begin() for (std::vector<cached_piece_info>::iterator i = ret.begin()
, end(ret.end()); i != end; ++i) , end(ret.end()); i != end; ++i)
{ {

View File

@ -35,14 +35,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/time.hpp" #include "libtorrent/time.hpp"
#include <string>
#include <boost/cstdint.hpp>
namespace libtorrent { namespace aux namespace libtorrent { namespace aux
{ {
// returns the current time, as represented by time_point. The // returns the current time, as represented by time_point. The
// resolution of this timer is about 100 ms. // resolution of this timer is about 100 ms.
time_point const& time_now(); time_point time_now();
TORRENT_EXTRA_EXPORT void update_time_now(); TORRENT_EXTRA_EXPORT void update_time_now();

View File

@ -106,6 +106,10 @@ POSSIBILITY OF SUCH DAMAGE.
# define TORRENT_DEPRECATED __attribute__ ((deprecated)) # define TORRENT_DEPRECATED __attribute__ ((deprecated))
# endif # endif
# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 8)
# define TORRENT_NO_ATOMIC_DURATION 1
# endif
// ======= SUNPRO ========= // ======= SUNPRO =========
#elif defined __SUNPRO_CC #elif defined __SUNPRO_CC
@ -542,6 +546,12 @@ int snprintf(char* buf, int len, char const* fmt, ...)
#define TORRENT_NO_FPU 0 #define TORRENT_NO_FPU 0
#endif #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 TORRENT_USE_IOSTREAM
#ifndef BOOST_NO_IOSTREAM #ifndef BOOST_NO_IOSTREAM
#define TORRENT_USE_IOSTREAM 1 #define TORRENT_USE_IOSTREAM 1

View File

@ -30,30 +30,29 @@ POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <ctime>
#include <string>
#include <cstdio>
#include <boost/limits.hpp>
#include <boost/version.hpp>
#include "libtorrent/config.hpp"
#include "libtorrent/time.hpp" #include "libtorrent/time.hpp"
#include "libtorrent/aux_/time.hpp" #include "libtorrent/aux_/time.hpp"
#include <atomic>
namespace libtorrent { namespace aux namespace libtorrent { namespace aux
{ {
// used to cache the current time // used to cache the current time regularly (update_time_now() is called by
// every 100 ms. This is cheaper // the session_impl main thread). This is cheaper than a system call and can
// than a system call and can be // be used where more accurate time is not necessary
// used where more accurate time #if !TORRENT_NO_ATOMIC_DURATION
// is not necessary
namespace { namespace {
time_point g_current_time = clock_type::now(); std::atomic<time_point> g_current_time(clock_type::now());
} }
time_point time_now() { return aux::g_current_time.load(); }
time_point const& time_now() { return aux::g_current_time; } void update_time_now() { g_current_time.store(clock_type::now()); }
#else
void update_time_now() { g_current_time = clock_type::now(); } // work-around for not being able to put time_point in std::atomic
namespace {
std::atomic<time_duration::rep> 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
} } } }