2010-10-10 04:22:57 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2011-2018, Arvid Norberg
|
2010-10-10 04:22:57 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/disk_io_job.hpp"
|
|
|
|
#include "libtorrent/block_cache.hpp" // for cached_piece_entry
|
2017-04-16 22:37:39 +02:00
|
|
|
#include "libtorrent/disk_buffer_holder.hpp"
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2018-03-20 09:12:47 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2017-04-16 22:37:39 +02:00
|
|
|
#include <boost/variant/get.hpp>
|
2018-03-20 09:12:47 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2017-04-12 19:00:57 +02:00
|
|
|
|
2017-04-16 22:37:39 +02:00
|
|
|
namespace libtorrent {
|
2016-12-29 02:47:18 +01:00
|
|
|
|
2016-11-21 05:58:48 +01:00
|
|
|
namespace {
|
|
|
|
struct caller_visitor : boost::static_visitor<>
|
|
|
|
{
|
2017-04-16 22:37:39 +02:00
|
|
|
explicit caller_visitor(disk_io_job& j)
|
|
|
|
: m_job(j) {}
|
2016-11-21 05:58:48 +01:00
|
|
|
|
|
|
|
void operator()(disk_io_job::read_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
2017-04-16 22:37:39 +02:00
|
|
|
h(std::move(boost::get<disk_buffer_holder>(m_job.argument))
|
|
|
|
, m_job.flags, m_job.error);
|
2016-11-21 05:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::write_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h(m_job.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::hash_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
2017-10-22 15:24:12 +02:00
|
|
|
h(m_job.piece, m_job.d.piece_hash, m_job.error);
|
2016-11-21 05:58:48 +01:00
|
|
|
}
|
|
|
|
|
2016-11-22 07:48:14 +01:00
|
|
|
void operator()(disk_io_job::move_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
2017-04-16 22:37:39 +02:00
|
|
|
h(m_job.ret, std::move(boost::get<std::string>(m_job.argument))
|
|
|
|
, m_job.error);
|
2016-11-22 07:48:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::release_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h();
|
|
|
|
}
|
|
|
|
|
2016-11-23 01:03:27 +01:00
|
|
|
void operator()(disk_io_job::check_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h(m_job.ret, m_job.error);
|
|
|
|
}
|
|
|
|
|
2016-11-23 07:43:57 +01:00
|
|
|
void operator()(disk_io_job::rename_handler& h) const
|
2016-11-21 05:58:48 +01:00
|
|
|
{
|
|
|
|
if (!h) return;
|
2017-04-16 22:37:39 +02:00
|
|
|
h(std::move(boost::get<std::string>(m_job.argument))
|
|
|
|
, m_job.file_index, m_job.error);
|
2016-11-23 07:43:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(disk_io_job::clear_piece_handler& h) const
|
|
|
|
{
|
|
|
|
if (!h) return;
|
|
|
|
h(m_job.piece);
|
2016-11-21 05:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
disk_io_job& m_job;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-09 19:43:54 +02:00
|
|
|
constexpr disk_job_flags_t disk_io_job::fence;
|
|
|
|
constexpr disk_job_flags_t disk_io_job::in_progress;
|
2017-10-22 14:03:49 +02:00
|
|
|
constexpr disk_job_flags_t disk_io_job::aborted;
|
2017-09-09 19:43:54 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
disk_io_job::disk_io_job()
|
2017-07-30 18:13:22 +02:00
|
|
|
: argument(remove_flags_t{})
|
2017-04-16 22:37:39 +02:00
|
|
|
, piece(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
d.io.offset = 0;
|
|
|
|
d.io.buffer_size = 0;
|
|
|
|
}
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2017-04-16 22:37:39 +02:00
|
|
|
void disk_io_job::call_callback()
|
2016-11-21 05:58:48 +01:00
|
|
|
{
|
2017-04-16 22:37:39 +02:00
|
|
|
boost::apply_visitor(caller_visitor(*this), callback);
|
2016-11-21 05:58:48 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 12:54:03 +01:00
|
|
|
bool disk_io_job::completed(cached_piece_entry const* pe)
|
2010-10-10 04:22:57 +02:00
|
|
|
{
|
2017-06-11 19:53:15 +02:00
|
|
|
if (action != job_action_t::write) return false;
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2018-01-03 12:54:03 +01:00
|
|
|
int block_offset = d.io.offset & (default_block_size - 1);
|
2014-07-06 21:18:00 +02:00
|
|
|
int size = d.io.buffer_size;
|
2018-01-03 12:54:03 +01:00
|
|
|
int start = d.io.offset / default_block_size;
|
|
|
|
int end = block_offset > 0 && (size > default_block_size - block_offset) ? start + 2 : start + 1;
|
2015-05-16 22:41:37 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = start; i < end; ++i)
|
2017-01-15 00:53:25 +01:00
|
|
|
{
|
2017-01-26 14:31:20 +01:00
|
|
|
cached_block_entry const& b = pe->blocks[i];
|
2017-01-15 00:53:25 +01:00
|
|
|
if (b.dirty || b.pending) return false;
|
|
|
|
}
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// if all our blocks are not pending and not dirty, it means they
|
|
|
|
// were successfully written to disk. This job is complete
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|