diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 10d3b6ac7..ce9709905 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -550,6 +550,13 @@ namespace libtorrent // disk cache mutable mutex m_cache_mutex; block_cache m_disk_cache; + enum + { + cache_check_idle, + cache_check_active, + cache_check_reinvoke + }; + int m_cache_check_state; // total number of blocks in use by both the read // and the write cache. This is not supposed to diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index edbea3b94..e36b1e2b3 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -160,6 +160,7 @@ namespace libtorrent , m_last_file_check(clock_type::now()) , m_file_pool(40) , m_disk_cache(block_size, ios, boost::bind(&disk_io_thread::trigger_cache_trim, this)) + , m_cache_check_state(cache_check_idle) , m_stats_counters(cnt) , m_ios(ios) , m_last_disk_aio_performance_warning(min_time()) @@ -1126,7 +1127,20 @@ namespace libtorrent m_stats_counters.inc_stats_counter(counters::num_running_disk_jobs, -1); mutex::scoped_lock l(m_cache_mutex); - check_cache_level(l, completed_jobs); + if (m_cache_check_state == cache_check_idle) + { + m_cache_check_state = cache_check_active; + while (m_cache_check_state != cache_check_idle) + { + check_cache_level(l, completed_jobs); + TORRENT_ASSERT(l.locked()); + --m_cache_check_state; + } + } + else + { + m_cache_check_state = cache_check_reinvoke; + } l.unlock(); if (ret == retry_job)