Merge pull request #677 from arvidn/atomic-time

make the cached current time thread-safe
This commit is contained in:
Arvid Norberg 2016-05-01 08:51:24 -04:00
commit 3916d0dd5e
12 changed files with 48 additions and 203 deletions

View File

@ -255,13 +255,14 @@ endif()
# Boost
if(NOT DEFINED Boost_INCLUDE_DIR OR NOT DEFINED Boost_LIBRARIES)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system chrono)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system)
endif()
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(torrent-rasterbar ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
# this works around a bug in asio in boost-1.39
#add_definitions(-DBOOST_ASIO_HASH_MAP_BUCKETS=1021 -D__USE_W32_SOCKETS -DWIN32_LEAN_AND_MEAN )
add_definitions(-DBOOST_ASIO_HASH_MAP_BUCKETS=1021 )
add_definitions(-DBOOST_ASIO_HAS_STD_CHRONO=1 )
if (WIN32)
target_link_libraries(torrent-rasterbar wsock32 ws2_32 Iphlpapi)

15
Jamfile
View File

@ -22,7 +22,6 @@ if $(BOOST_ROOT)
ECHO "building boost from source directory: " $(BOOST_ROOT) ;
use-project /boost : $(BOOST_ROOT) ;
alias boost_chrono : /boost/chrono//boost_chrono : : : <include>$(BOOST_ROOT) ;
alias boost_system : /boost/system//boost_system : : : <include>$(BOOST_ROOT) ;
}
else
@ -42,13 +41,9 @@ else
;
# the names are decorated in MacPorts
lib boost_chrono : : <target-os>darwin <name>boost_chrono-mt $(boost-lib-search-path)
: : $(boost-include-path) ;
lib boost_system : : <target-os>darwin <name>boost_system-mt $(boost-lib-search-path)
: : $(boost-include-path) ;
lib boost_chrono : : <name>boost_chrono $(boost-lib-search-path)
: : $(boost-include-path) ;
lib boost_system : : <name>boost_system $(boost-lib-search-path)
: : $(boost-include-path) ;
}
@ -173,12 +168,10 @@ rule linking ( properties * )
# library, so that it properly exports its symbols
result += <define>BOOST_ALL_DYN_LINK ;
result += <library>boost_system/<link>static/<define>BOOST_ALL_DYN_LINK ;
result += <library>boost_chrono/<link>static/<define>BOOST_ALL_DYN_LINK ;
}
else
{
result += <library>boost_system/<link>static ;
result += <library>boost_chrono/<link>static ;
}
if <toolset>gcc in $(properties)
@ -192,12 +185,10 @@ rule linking ( properties * )
else if <boost-link>shared in $(properties)
{
result += <library>boost_system/<link>shared ;
result += <library>boost_chrono/<link>shared ;
}
else
{
result += <library>boost_system ;
result += <library>boost_chrono ;
}
result += <define>BOOST_ALL_NO_LIB
@ -708,6 +699,10 @@ local usage-requirements =
<define>BOOST_EXCEPTION_DISABLE
# enable cancel support in asio
<define>BOOST_ASIO_ENABLE_CANCELIO
# make sure asio uses std::chrono
<define>BOOST_ASIO_HAS_STD_CHRONO
# this works around a bug in asio in boost-1.39
<define>BOOST_ASIO_HASH_MAP_BUCKETS=1021
<conditional>@linking
# these compiler settings just makes the compiler standard conforming
<toolset>msvc:<cflags>/Zc:wchar_t
@ -718,8 +713,6 @@ local usage-requirements =
<cxxflags>$(CXXFLAGS)
<linkflags>$(LDFLAGS)
# this works around a bug in asio in boost-1.39
<define>BOOST_ASIO_HASH_MAP_BUCKETS=1021
;
project torrent ;

View File

@ -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)
{

View File

@ -123,13 +123,9 @@ AX_BOOST_SYSTEM()
AS_IF([test -z "$BOOST_SYSTEM_LIB"],
[AC_MSG_ERROR(Boost.System library not found. Try using --with-boost-system=lib)])
AX_BOOST_CHRONO()
AS_IF([test -z "$BOOST_CHRONO_LIB"],
[AC_MSG_ERROR(Boost.Chrono library not found. Try using --with-boost-chrono=lib)])
CPPFLAGS="$BOOST_CPPFLAGS $CPPFLAGS"
LDFLAGS="$BOOST_LDFLAGS $LDFLAGS"
LIBS="$BOOST_CHRONO_LIB $BOOST_SYSTEM_LIB $LIBS"
LIBS="$BOOST_SYSTEM_LIB $LIBS"
###############################################################################
# Checking for functions and other stuffs
@ -532,6 +528,9 @@ AM_CONDITIONAL([WITH_OPENSSL], [test "x$ARG_ENABLE_ENCRYPTION" = "xyes" -o "x$AR
AC_DEFINE([BOOST_ASIO_HASH_MAP_BUCKETS],[1021],[Define to fix a wrong behavior in boost 1.39.])
COMPILETIME_OPTIONS="$COMPILETIME_OPTIONS -DBOOST_ASIO_HASH_MAP_BUCKETS=1021 "
AC_DEFINE([BOOST_ASIO_HAS_STD_CHRONO],[1],[Define to make sure asio uses std::chrono.])
COMPILETIME_OPTIONS="$COMPILETIME_OPTIONS -DBOOST_ASIO_HAS_STD_CHRONO=1 "
AC_DEFINE([BOOST_EXCEPTION_DISABLE],[1],[Define to disable the boost.exception features.])
COMPILETIME_OPTIONS="$COMPILETIME_OPTIONS -DBOOST_EXCEPTION_DISABLE "
@ -640,7 +639,6 @@ Boost libraries:
CPPFlags: ${BOOST_CPPFLAGS}
LDFlags: ${BOOST_LDFLAGS}
boost.system: ${BOOST_SYSTEM_LIB}
boost.chrono: ${BOOST_CHRONO_LIB}
END
AS_IF([test "x$ARG_ENABLE_PYTHON_BINDING" = "xyes"], [

View File

@ -30,5 +30,5 @@ LDADD = $(top_builddir)/src/libtorrent-rasterbar.la
AM_CPPFLAGS = -ftemplate-depth-50 -I$(top_srcdir)/include @DEBUGFLAGS@
AM_LDFLAGS = @BOOST_SYSTEM_LIB@ @BOOST_CHRONO_LIB@ @BOOST_RANDOM_LIB@ @OPENSSL_LDFLAGS@ @OPENSSL_LIBS@
AM_LDFLAGS = @BOOST_SYSTEM_LIB@ @BOOST_RANDOM_LIB@ @OPENSSL_LDFLAGS@ @OPENSSL_LIBS@

View File

@ -42,7 +42,7 @@ else()
if(NOT LibtorrentRasterbar_USE_STATIC_LIBS)
list(APPEND LibtorrentRasterbar_DEFINITIONS
-DTORRENT_LINKING_SHARED
-DBOOST_SYSTEM_DYN_LINK -DBOOST_CHRONO_DYN_LINK)
-DBOOST_SYSTEM_DYN_LINK)
endif()
endif()
@ -62,8 +62,8 @@ endif()
set(LibtorrentRasterbar_LIBRARIES ${LibtorrentRasterbar_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
set(LibtorrentRasterbar_INCLUDE_DIRS ${LibtorrentRasterbar_INCLUDE_DIR})
if(NOT Boost_SYSTEM_FOUND OR NOT Boost_CHRONO_FOUND OR NOT Boost_RANDOM_FOUND)
find_package(Boost REQUIRED COMPONENTS system chrono random)
if(NOT Boost_SYSTEM_FOUND OR NOT Boost_RANDOM_FOUND)
find_package(Boost REQUIRED COMPONENTS system random)
set(LibtorrentRasterbar_LIBRARIES
${LibtorrentRasterbar_LIBRARIES} ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
set(LibtorrentRasterbar_INCLUDE_DIRS
@ -85,7 +85,6 @@ find_package_handle_standard_args(LibtorrentRasterbar DEFAULT_MSG
LibtorrentRasterbar_LIBRARY
LibtorrentRasterbar_INCLUDE_DIR
Boost_SYSTEM_FOUND
Boost_CHRONO_FOUND
Boost_RANDOM_FOUND)
mark_as_advanced(LibtorrentRasterbar_INCLUDE_DIR LibtorrentRasterbar_LIBRARY

View File

@ -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();

View File

@ -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

View File

@ -39,11 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/cstdint.hpp>
#if defined BOOST_ASIO_HAS_STD_CHRONO
#include <chrono>
#else
#include <boost/chrono.hpp>
#endif
#if defined TORRENT_BUILD_SIMULATOR
#include "simulator/simulator.hpp"
@ -54,31 +50,20 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent {
#if defined TORRENT_BUILD_SIMULATOR
typedef sim::chrono::high_resolution_clock clock_type;
#elif defined BOOST_ASIO_HAS_STD_CHRONO
typedef std::chrono::high_resolution_clock clock_type;
using clock_type = sim::chrono::high_resolution_clock;
#else
typedef boost::chrono::high_resolution_clock clock_type;
using clock_type = std::chrono::high_resolution_clock;
#endif
typedef clock_type::time_point time_point;
typedef clock_type::duration time_duration;
using time_point = clock_type::time_point;
using time_duration = clock_type::duration;
#if defined BOOST_ASIO_HAS_STD_CHRONO
using std::chrono::seconds;
using std::chrono::milliseconds;
using std::chrono::microseconds;
using std::chrono::minutes;
using std::chrono::hours;
using std::chrono::duration_cast;
#else
using boost::chrono::seconds;
using boost::chrono::milliseconds;
using boost::chrono::microseconds;
using boost::chrono::minutes;
using boost::chrono::hours;
using boost::chrono::duration_cast;
#endif
// internal
inline time_point min_time() { return (time_point::min)(); }
@ -98,24 +83,6 @@ namespace libtorrent {
boost::int64_t total_microseconds(T td)
{ return duration_cast<microseconds>(td).count(); }
#ifndef TORRENT_NO_DEPRECATE
TORRENT_DEPRECATED
time_point time_now();
TORRENT_DEPRECATED
time_point time_now_hires();
inline time_point time_now()
{ return clock_type::now(); }
inline time_point time_now_hires()
{ return clock_type::now(); }
typedef time_point ptime;
#endif
}
#endif // TORRENT_TIME_HPP_INCLUDED

View File

@ -1,119 +0,0 @@
# ===========================================================================
# http://www.gnu.org/software/autoconf-archive/ax_boost_chrono.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_BOOST_CHRONO
#
# DESCRIPTION
#
# Test for System library from the Boost C++ libraries. The macro requires
# a preceding call to AX_BOOST_BASE. Further documentation is available at
# <http://randspringer.de/boost/index.html>.
#
# This macro calls:
#
# AC_SUBST(BOOST_CHRONO_LIB)
#
# And sets:
#
# HAVE_BOOST_CHRONO
#
# LICENSE
#
# Copyright (c) 2012 Xiyue Deng <manphiz@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 1
AC_DEFUN([AX_BOOST_CHRONO],
[
AC_ARG_WITH([boost-chrono],
AS_HELP_STRING([--with-boost-chrono@<:@=special-lib@:>@],
[use the Chrono library from boost - it is possible to specify a certain library for the linker
e.g. --with-boost-chrono=boost_chrono-gcc-mt ]),
[
if test "$withval" = "no"; then
want_boost="no"
elif test "$withval" = "yes"; then
want_boost="yes"
ax_boost_user_chrono_lib=""
else
want_boost="yes"
ax_boost_user_chrono_lib="$withval"
fi
],
[want_boost="yes"]
)
if test "x$want_boost" = "xyes"; then
AC_REQUIRE([AC_PROG_CC])
AC_REQUIRE([AC_CANONICAL_BUILD])
CPPFLAGS_SAVED="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
export CPPFLAGS
LDFLAGS_SAVED="$LDFLAGS"
LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
export LDFLAGS
AC_CACHE_CHECK(whether the Boost::Chrono library is available,
ax_cv_boost_chrono,
[AC_LANG_PUSH([C++])
CXXFLAGS_SAVE=$CXXFLAGS
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include <boost/chrono.hpp>]],
[[boost::chrono::system_clock::time_point time;]])],
ax_cv_boost_chrono=yes, ax_cv_boost_chrono=no)
CXXFLAGS=$CXXFLAGS_SAVE
AC_LANG_POP([C++])
])
if test "x$ax_cv_boost_chrono" = "xyes"; then
AC_SUBST(BOOST_CPPFLAGS)
AC_DEFINE(HAVE_BOOST_CHRONO,,[define if the Boost::Chrono library is available])
BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
LDFLAGS_SAVE=$LDFLAGS
if test "x$ax_boost_user_chrono_lib" = "x"; then
for libextension in `ls $BOOSTLIBDIR/libboost_chrono*.so* $BOOSTLIBDIR/libboost_chrono*.dylib* $BOOSTLIBDIR/libboost_chrono*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_chrono.*\)\.so.*$;\1;' -e 's;^lib\(boost_chrono.*\)\.dylib.*$;\1;' -e 's;^lib\(boost_chrono.*\)\.a.*$;\1;'` ; do
ax_lib=${libextension}
AC_CHECK_LIB($ax_lib, exit,
[BOOST_CHRONO_LIB="-l$ax_lib"; AC_SUBST(BOOST_CHRONO_LIB) link_chrono="yes"; break],
[link_chrono="no"])
done
if test "x$link_chrono" != "xyes"; then
for libextension in `ls $BOOSTLIBDIR/boost_chrono*.dll* $BOOSTLIBDIR/boost_chrono*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_chrono.*\)\.dll.*$;\1;' -e 's;^\(boost_chrono.*\)\.a.*$;\1;'` ; do
ax_lib=${libextension}
AC_CHECK_LIB($ax_lib, exit,
[BOOST_CHRONO_LIB="-l$ax_lib"; AC_SUBST(BOOST_CHRONO_LIB) link_chrono="yes"; break],
[link_chrono="no"])
done
fi
else
for ax_lib in $ax_boost_user_chrono_lib boost_chrono-$ax_boost_user_chrono_lib; do
AC_CHECK_LIB($ax_lib, exit,
[BOOST_CHRONO_LIB="-l$ax_lib"; AC_SUBST(BOOST_CHRONO_LIB) link_chrono="yes"; break],
[link_chrono="no"])
done
fi
if test "x$ax_lib" = "x"; then
AC_MSG_ERROR(Could not find a version of the library!)
fi
if test "x$link_chrono" = "xno"; then
AC_MSG_ERROR(Could not link against $ax_lib !)
fi
fi
CPPFLAGS="$CPPFLAGS_SAVED"
LDFLAGS="$LDFLAGS_SAVED"
fi
])

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/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
} }

View File

@ -231,6 +231,5 @@ LDADD = libtest.la $(top_builddir)/src/libtorrent-rasterbar.la
#AM_CXXFLAGS=-ftemplate-depth-50 -I$(top_srcdir)/include -I$(top_srcdir)/include/libtorrent @DEBUGFLAGS@ @PTHREAD_CFLAGS@
AM_CPPFLAGS=-ftemplate-depth-50 -I$(top_srcdir)/include @DEBUGFLAGS@
AM_LDFLAGS=@BOOST_SYSTEM_LIB@ @PTHREAD_LIBS@ @OPENSSL_LDFLAGS@ @OPENSSL_LIBS@ \
@BOOST_CHRONO_LIB@
AM_LDFLAGS=@BOOST_SYSTEM_LIB@ @PTHREAD_LIBS@ @OPENSSL_LDFLAGS@ @OPENSSL_LIBS@