make the cached current time thread-safe
This commit is contained in:
parent
fef94a4c52
commit
fc4cc9e7ee
|
@ -383,7 +383,7 @@ namespace
|
|||
}
|
||||
|
||||
list pieces;
|
||||
ptime now = time_now();
|
||||
time_point now = clock_type::now();
|
||||
for (std::vector<cached_piece_info>::iterator i = ret.begin()
|
||||
, end(ret.end()); i != end; ++i)
|
||||
{
|
||||
|
|
|
@ -35,14 +35,12 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
#include "libtorrent/config.hpp"
|
||||
#include "libtorrent/time.hpp"
|
||||
#include <string>
|
||||
#include <boost/cstdint.hpp>
|
||||
|
||||
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();
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
33
src/time.cpp
33
src/time.cpp
|
@ -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/aux_/time.hpp"
|
||||
#include <atomic>
|
||||
|
||||
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<time_point> 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<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
|
||||
|
||||
} }
|
||||
|
||||
|
|
Loading…
Reference in New Issue