Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Feb 17, 2024
1 parent 8652833 commit 0eb8061
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions include/libtorrent/aux_/disk_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ struct cached_piece_entry
, ph(hasher())
{}

span<cached_block_entry> get_blocks() const
{
return {blocks.get(), blocks_in_piece};
}

piece_location piece;

// this is set to true when the piece has been populated with all blocks
Expand All @@ -107,8 +112,6 @@ struct cached_piece_entry
// hashes as well
bool v2_hashes = false;

//#error we need a field indicating whether some blocks were flushed and removed from the cache before hashed, since otherwise we may hang the hash job on the piece but it never completes

int blocks_in_piece = 0;

// the number of blocks that have been hashed so far. Specifically for the
Expand Down Expand Up @@ -156,6 +159,13 @@ struct compare_storage
}
};

bool have_buffers(span<const cached_block_entry> blocks)
{
for (auto const& b : blocks)
if (b.buf == nullptr) return false;
return true;
}

struct disk_cache
{
using piece_container = mi::multi_index_container<
Expand Down Expand Up @@ -336,8 +346,21 @@ struct disk_cache
// we should only ask for the hash once
TORRENT_ASSERT(!i->piece_hash_returned);

if (!i->hashing && i->hasher_cursor == i->blocks_in_piece)
{
view.modify(i, [&](cached_piece_entry& e) {
e.piece_hash_returned = true;

job::hash& job = std::get<aux::job::hash>(hash_job->action);
job.piece_hash = e.ph.final();
});
return hash_result::job_completed;
}

if (i->hashing
|| i->hasher_cursor < i->blocks_in_piece)
&& i->hasher_cursor < i->blocks_in_piece
&& have_buffers(i->get_blocks().subspan(i->hasher_cursor))
)
{
// We're not done hashing yet, let the hashing thread post the
// completion once it's done
Expand All @@ -349,12 +372,6 @@ struct disk_cache
return hash_result::job_queued;
}

view.modify(i, [&](cached_piece_entry& e) {
e.piece_hash_returned = true;

job::hash& job = std::get<aux::job::hash>(hash_job->action);
job.piece_hash = e.ph.final();
});
return hash_result::post_job;
}

Expand Down Expand Up @@ -521,7 +538,6 @@ struct disk_cache
if (count < piece_iter->blocks_in_piece)
return;

// #error this is not thread safe!
if (piece_iter->piece_hash_returned)
piece_iter = view.erase(piece_iter);
else
Expand Down Expand Up @@ -751,7 +767,6 @@ struct disk_cache
return;
}

// #error this is not thread safe!
if (piece_iter->piece_hash_returned)
piece_iter = view.erase(piece_iter);
else
Expand Down

0 comments on commit 0eb8061

Please sign in to comment.