From a02c1d2164a6a1d1c57750a190d3ab556bad2ee4 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 5 Mar 2016 17:16:46 -0500 Subject: [PATCH] record cache settings in the job instead of checking it twice for more robust behavior --- include/libtorrent/disk_io_job.hpp | 6 +++++- simulation/test_transfer.cpp | 17 +++++++++++++++++ src/disk_io_thread.cpp | 21 +++++++++++---------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/include/libtorrent/disk_io_job.hpp b/include/libtorrent/disk_io_job.hpp index fdb693f2c..ded630b6f 100644 --- a/include/libtorrent/disk_io_job.hpp +++ b/include/libtorrent/disk_io_job.hpp @@ -136,7 +136,11 @@ namespace libtorrent in_progress = 0x20, // turns into file::coalesce_buffers in the file operation - coalesce_buffers = 0x40 + coalesce_buffers = 0x40, + + // the disk cache was enabled when this job was issued, it should use + // the disk cache once it's handled by a disk thread + use_disk_cache = 0x80 }; // for write jobs, returns true if its block diff --git a/simulation/test_transfer.cpp b/simulation/test_transfer.cpp index a783e4b8a..ddae108ba 100644 --- a/simulation/test_transfer.cpp +++ b/simulation/test_transfer.cpp @@ -441,3 +441,20 @@ TORRENT_TEST(auto_disk_cache_size) } ); } + +TORRENT_TEST(disable_disk_cache) +{ + using namespace libtorrent; + run_test( + [](lt::session& ses0, lt::session& ses1) { set_cache_size(ses0, 0); }, + [](lt::session& ses, lt::alert const* alert) {}, + [](std::shared_ptr ses[2]) { + TEST_EQUAL(is_seed(*ses[0]), true); + + int const cache_size = get_cache_size(*ses[0]); + printf("cache size: %d\n", cache_size); + TEST_EQUAL(cache_size, 0); + } + ); +} + diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index f45d83d48..57ac499c0 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -1188,16 +1188,11 @@ namespace libtorrent int disk_io_thread::do_read(disk_io_job* j, jobqueue_t& completed_jobs) { - if (m_settings.get_bool(settings_pack::use_read_cache) == false - || m_settings.get_int(settings_pack::cache_size) == 0) + if ((j->flags & disk_io_job::use_disk_cache) == 0) { // we're not using a cache. This is the simple path // just read straight from the file int ret = do_uncached_read(j); - - mutex::scoped_lock l(m_cache_mutex); - cached_piece_entry* pe = m_disk_cache.find_piece(j); - if (pe) maybe_issue_queued_read_jobs(pe, completed_jobs); return ret; } @@ -1461,8 +1456,7 @@ namespace libtorrent // should we put this write job in the cache? // if we don't use the cache we shouldn't. - if (m_settings.get_bool(settings_pack::use_write_cache) - && m_settings.get_int(settings_pack::cache_size) != 0) + if (j->flags & disk_io_job::use_disk_cache) { mutex::scoped_lock l(m_cache_mutex); @@ -1615,7 +1609,7 @@ namespace libtorrent j->error.operation = storage_error::read; return 0; } - + j->flags |= disk_io_job::use_disk_cache; if (pe->outstanding_read) { TORRENT_PIECE_ASSERT(j->piece == pe->piece, pe); @@ -1695,6 +1689,7 @@ namespace libtorrent && m_settings.get_bool(settings_pack::use_write_cache)) { TORRENT_ASSERT((r.start % m_disk_cache.block_size()) == 0); + j->flags |= disk_io_job::use_disk_cache; if (storage->is_blocked(j)) { @@ -1784,6 +1779,12 @@ namespace libtorrent } l.unlock(); + if (m_settings.get_bool(settings_pack::use_read_cache) + && m_settings.get_int(settings_pack::cache_size) != 0) + { + j->flags |= disk_io_job::use_disk_cache; + } + add_job(j); } @@ -2284,7 +2285,7 @@ namespace libtorrent { INVARIANT_CHECK; - if (m_settings.get_int(settings_pack::cache_size) == 0) + if ((j->flags & disk_io_job::use_disk_cache) == 0) return do_uncached_hash(j); int const piece_size = j->storage->files()->piece_size(j->piece);