From 6597eaf09a727cec750cfbb19caa34dfb793589a Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 3 Feb 2014 01:55:26 +0000 Subject: [PATCH] there doesn't seem to be any point in exporting piece_block_progress, so don't do that. Introduce magic number check in disk_io_thread (mysterious crash/corruption happens in storage unit test on mac, but not very often and hard to reproduce). --- include/libtorrent/disk_io_thread.hpp | 4 +++ include/libtorrent/peer_request.hpp | 3 +++ include/libtorrent/piece_block_progress.hpp | 2 +- src/disk_io_thread.cpp | 30 +++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 1f922e4af..47f5384c5 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -519,6 +519,10 @@ namespace libtorrent // in this list std::list > m_queued_completions; +#if TORRENT_USE_ASSERTS + int m_magic; +#endif + // thread for performing blocking disk io operations thread m_disk_io_thread; }; diff --git a/include/libtorrent/peer_request.hpp b/include/libtorrent/peer_request.hpp index e94a3e9a8..b5bdf446a 100644 --- a/include/libtorrent/peer_request.hpp +++ b/include/libtorrent/peer_request.hpp @@ -46,6 +46,9 @@ namespace libtorrent int start; // the size of the range, in bytes. int length; + + // returns true if the right hand side peer_request refers to the same + // range as this does. bool operator==(peer_request const& r) const { return piece == r.piece && start == r.start && length == r.length; } }; diff --git a/include/libtorrent/piece_block_progress.hpp b/include/libtorrent/piece_block_progress.hpp index 47b8bb66e..f3d3583b5 100644 --- a/include/libtorrent/piece_block_progress.hpp +++ b/include/libtorrent/piece_block_progress.hpp @@ -37,7 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - struct TORRENT_EXPORT piece_block_progress + struct piece_block_progress { // the piece and block index // determines exactly which diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 111a0f2aa..167f9b31f 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -87,6 +87,9 @@ namespace libtorrent , m_queue_callback(queue_callback) , m_work(io_service::work(m_ios)) , m_file_pool(fp) +#if TORRENT_USE_ASSERTS + , m_magic(0x1337) +#endif , m_disk_io_thread(boost::bind(&disk_io_thread::thread_fun, this)) { // don't do anything in here. Essentially all members @@ -96,11 +99,17 @@ namespace libtorrent disk_io_thread::~disk_io_thread() { + TORRENT_ASSERT(m_magic == 0x1337); +#if TORRENT_USE_ASSERTS + m_magic = 0xdead; +#endif TORRENT_ASSERT(m_abort == true); } void disk_io_thread::abort() { + TORRENT_ASSERT(m_magic == 0x1337); + mutex::scoped_lock l(m_queue_mutex); disk_io_job j; m_waiting_to_shutdown = true; @@ -114,6 +123,8 @@ namespace libtorrent void disk_io_thread::join() { + TORRENT_ASSERT(m_magic == 0x1337); + m_disk_io_thread.join(); mutex::scoped_lock l(m_queue_mutex); TORRENT_ASSERT(m_abort == true); @@ -122,12 +133,15 @@ namespace libtorrent bool disk_io_thread::can_write() const { + TORRENT_ASSERT(m_magic == 0x1337); mutex::scoped_lock l(m_queue_mutex); return !m_exceeded_write_queue; } void disk_io_thread::flip_stats(ptime now) { + TORRENT_ASSERT(m_magic == 0x1337); + // calling mean() will actually reset the accumulators m_cache_stats.average_queue_time = m_queue_time.mean(); m_cache_stats.average_read_time = m_read_time.mean(); @@ -141,6 +155,8 @@ namespace libtorrent void disk_io_thread::get_cache_info(sha1_hash const& ih, std::vector& ret) const { + TORRENT_ASSERT(m_magic == 0x1337); + mutex::scoped_lock l(m_piece_mutex); ret.clear(); ret.reserve(m_pieces.size()); @@ -1242,6 +1258,8 @@ namespace libtorrent int disk_io_thread::try_read_from_cache(disk_io_job const& j, bool& hit, int flags) { + TORRENT_ASSERT(m_magic == 0x1337); + TORRENT_ASSERT(j.buffer); TORRENT_ASSERT(j.cache_min_time >= 0); @@ -1297,6 +1315,8 @@ namespace libtorrent size_type disk_io_thread::queue_buffer_size() const { + TORRENT_ASSERT(m_magic == 0x1337); + mutex::scoped_lock l(m_queue_mutex); return m_queue_buffer_size; } @@ -1322,6 +1342,8 @@ namespace libtorrent , mutex::scoped_lock& l , boost::function const& f) { + TORRENT_ASSERT(m_magic == 0x1337); + const_cast(j).start_time = time_now_hires(); if (j.action == disk_io_job::write) @@ -1370,17 +1392,21 @@ namespace libtorrent int disk_io_thread::add_job(disk_io_job const& j , boost::function const& f) { + TORRENT_ASSERT(m_magic == 0x1337); TORRENT_ASSERT(!m_abort); TORRENT_ASSERT(j.storage || j.action == disk_io_job::abort_thread || j.action == disk_io_job::update_settings); TORRENT_ASSERT(j.buffer_size <= m_block_size); mutex::scoped_lock l(m_queue_mutex); + TORRENT_ASSERT(m_magic == 0x1337); return add_job(j, l, f); } bool disk_io_thread::test_error(disk_io_job& j) { + TORRENT_ASSERT(m_magic == 0x1337); + TORRENT_ASSERT(j.storage); error_code const& ec = j.storage->error(); if (ec) @@ -1510,6 +1536,7 @@ namespace libtorrent m_log << log_time() << " idle" << std::endl; #endif + TORRENT_ASSERT(m_magic == 0x1337); mutex::scoped_lock jl(m_queue_mutex); @@ -1563,6 +1590,9 @@ namespace libtorrent // release the io_service to allow the run() call to return // we do this once we stop posting new callbacks to it. m_work.reset(); + + TORRENT_ASSERT(m_magic == 0x1337); + return; }