Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid bind and unique_ptr #288

Merged
merged 7 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/data/chunk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ bool
Chunk::sync(int flags) {
bool success = true;

for (iterator itr = begin(), last = end(); itr != last; ++itr)
if (!itr->chunk().sync(0, itr->chunk().size(), flags))
for (auto& c : *this)
if (!c.chunk().sync(0, c.chunk().size(), flags))
success = false;

return success;
Expand Down
22 changes: 12 additions & 10 deletions src/data/chunk_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ ChunkList::resize(size_type to_size) {

uint32_t index = 0;

for (iterator itr = begin(), last = end(); itr != last; ++itr, ++index)
itr->set_index(index);
for (auto& chunk : *this) {
chunk.set_index(index);
++index;
}
}

void
Expand All @@ -95,26 +97,26 @@ ChunkList::clear() {

// Don't do any sync'ing as whomever decided to shut down really
// doesn't care, so just de-reference all chunks in queue.
for (Queue::iterator itr = m_queue.begin(), last = m_queue.end(); itr != last; ++itr) {
if ((*itr)->references() != 1 || (*itr)->writable() != 1)
for (auto chunk : m_queue) {
if (chunk->references() != 1 || chunk->writable() != 1)
throw internal_error("ChunkList::clear() called but a node in the queue is still referenced.");

(*itr)->dec_rw();
clear_chunk(*itr);
chunk->dec_rw();
clear_chunk(chunk);
}

m_queue.clear();

if (std::find_if(begin(), end(), std::mem_fn(&ChunkListNode::chunk)) != end())
if (std::any_of(begin(), end(), std::mem_fn(&ChunkListNode::chunk)))
throw internal_error("ChunkList::clear() called but a node with a valid chunk was found.");

if (std::find_if(begin(), end(), std::mem_fn(&ChunkListNode::references)) != end())
if (std::any_of(begin(), end(), std::mem_fn(&ChunkListNode::references)))
throw internal_error("ChunkList::clear() called but a node with references != 0 was found.");

if (std::find_if(begin(), end(), std::mem_fn(&ChunkListNode::writable)) != end())
if (std::any_of(begin(), end(), std::mem_fn(&ChunkListNode::writable)))
throw internal_error("ChunkList::clear() called but a node with writable != 0 was found.");

if (std::find_if(begin(), end(), std::mem_fn(&ChunkListNode::blocking)) != end())
if (std::any_of(begin(), end(), std::mem_fn(&ChunkListNode::blocking)))
throw internal_error("ChunkList::clear() called but a node with blocking != 0 was found.");

base_type::clear();
Expand Down
24 changes: 7 additions & 17 deletions src/data/hash_check_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,16 @@

namespace torrent {

HashCheckQueue::HashCheckQueue() {
pthread_mutex_init(&m_lock, NULL);
}

HashCheckQueue::~HashCheckQueue() {
pthread_mutex_destroy(&m_lock);
}
HashCheckQueue::HashCheckQueue() = default;
HashCheckQueue::~HashCheckQueue() = default;

// Always poke thread_disk after calling this.
void
HashCheckQueue::push_back(HashChunk* hash_chunk) {
if (hash_chunk == NULL || !hash_chunk->chunk()->is_loaded() || !hash_chunk->chunk()->is_blocking())
throw internal_error("Invalid hash chunk passed to HashCheckQueue.");

pthread_mutex_lock(&m_lock);
auto lock = std::scoped_lock(m_lock);

// Set blocking...(? this needs to be possible to do after getting
// the chunk) When doing this make sure we verify that the handle is
Expand All @@ -69,8 +64,6 @@ HashCheckQueue::push_back(HashChunk* hash_chunk) {
int64_t size = hash_chunk->chunk()->chunk()->chunk_size();
instrumentation_update(INSTRUMENTATION_MEMORY_HASHING_CHUNK_COUNT, 1);
instrumentation_update(INSTRUMENTATION_MEMORY_HASHING_CHUNK_USAGE, size);

pthread_mutex_unlock(&m_lock);
}

// erase...
Expand All @@ -89,7 +82,7 @@ HashCheckQueue::push_back(HashChunk* hash_chunk) {

bool
HashCheckQueue::remove(HashChunk* hash_chunk) {
pthread_mutex_lock(&m_lock);
auto lock = std::scoped_lock(m_lock);

bool result;
iterator itr = std::find(begin(), end(), hash_chunk);
Expand All @@ -106,13 +99,12 @@ HashCheckQueue::remove(HashChunk* hash_chunk) {
result = false;
}

pthread_mutex_unlock(&m_lock);
return result;
}

void
HashCheckQueue::perform() {
pthread_mutex_lock(&m_lock);
auto lock = std::unique_lock(m_lock);

while (!empty()) {
HashChunk* hash_chunk = base_type::front();
Expand All @@ -125,7 +117,7 @@ HashCheckQueue::perform() {
instrumentation_update(INSTRUMENTATION_MEMORY_HASHING_CHUNK_COUNT, -1);
instrumentation_update(INSTRUMENTATION_MEMORY_HASHING_CHUNK_USAGE, -size);

pthread_mutex_unlock(&m_lock);
lock.unlock();

if (!hash_chunk->perform(~uint32_t(), true))
throw internal_error("HashCheckQueue::perform(): !hash_chunk->perform(~uint32_t(), true).");
Expand All @@ -134,10 +126,8 @@ HashCheckQueue::perform() {
hash_chunk->hash_c(hash.data());

m_slot_chunk_done(hash_chunk, hash);
pthread_mutex_lock(&m_lock);
lock.lock();
}

pthread_mutex_unlock(&m_lock);
}

}
4 changes: 2 additions & 2 deletions src/data/hash_check_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

#include <deque>
#include <functional>
#include <pthread.h>
#include <mutex>

#include "rak/allocators.h"

Expand Down Expand Up @@ -77,7 +77,7 @@ class lt_cacheline_aligned HashCheckQueue : private std::deque<HashChunk*, rak::

private:
slot_chunk_handle m_slot_chunk_done;
pthread_mutex_t m_lock;
std::mutex m_lock;
};

}
Expand Down
33 changes: 10 additions & 23 deletions src/data/hash_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,10 @@ struct HashQueueWillneed {
// everything is in memory, thus we need to throttle.

HashQueue::HashQueue(thread_disk* thread) :
m_thread_disk(thread) {

pthread_mutex_init(&m_done_chunks_lock, NULL);
m_thread_disk->hash_queue()->slot_chunk_done() = std::bind(&HashQueue::chunk_done, this, std::placeholders::_1, std::placeholders::_2);
m_thread_disk(thread) {
m_thread_disk->hash_queue()->slot_chunk_done() = [this](auto hc, const auto& hv) { chunk_done(hc, hv); };
}


// If we're done immediately, move the chunk to the front of the list so
// the next work cycle gets stuff done.
void
Expand All @@ -110,7 +107,7 @@ HashQueue::has(HashQueueNode::id_type id) {

bool
HashQueue::has(HashQueueNode::id_type id, uint32_t index) {
return std::find_if(begin(), end(), HashQueueEqual(id, index)) != end();
return std::any_of(begin(), end(), HashQueueEqual(id, index));
}

void
Expand All @@ -130,17 +127,10 @@ HashQueue::remove(HashQueueNode::id_type id) {
// The hash chunk was not found, so we need to wait until the hash
// check finishes.
if (!result) {
pthread_mutex_lock(&m_done_chunks_lock);
done_chunks_type::iterator done_itr;

while ((done_itr = m_done_chunks.find(hash_chunk)) == m_done_chunks.end()) {
pthread_mutex_unlock(&m_done_chunks_lock);
usleep(100);
pthread_mutex_lock(&m_done_chunks_lock);
}
auto lock = std::unique_lock(m_done_chunks_lock);

m_done_chunks.erase(done_itr);
pthread_mutex_unlock(&m_done_chunks_lock);
m_cv.wait(lock, [this, hash_chunk] { return m_done_chunks.find(hash_chunk) != m_done_chunks.end(); });
m_done_chunks.erase(hash_chunk);
}

itr.slot_done()(*hash_chunk->chunk(), NULL);
Expand All @@ -163,8 +153,8 @@ HashQueue::clear() {

void
HashQueue::work() {
pthread_mutex_lock(&m_done_chunks_lock);
auto lock = std::scoped_lock(m_done_chunks_lock);

while (!m_done_chunks.empty()) {
HashChunk* hash_chunk = m_done_chunks.begin()->first;
HashString hash_value = m_done_chunks.begin()->second;
Expand All @@ -191,18 +181,15 @@ HashQueue::work() {
slotDone(hash_chunk->handle(), hash_value.c_str());
delete hash_chunk;
}

pthread_mutex_unlock(&m_done_chunks_lock);
}

void
HashQueue::chunk_done(HashChunk* hash_chunk, const HashString& hash_value) {
pthread_mutex_lock(&m_done_chunks_lock);
auto lock = std::scoped_lock(m_done_chunks_lock);

m_done_chunks[hash_chunk] = hash_value;
m_slot_has_work(m_done_chunks.empty());

pthread_mutex_unlock(&m_done_chunks_lock);
m_cv.notify_all();
}

}
8 changes: 5 additions & 3 deletions src/data/hash_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
#ifndef LIBTORRENT_DATA_HASH_QUEUE_H
#define LIBTORRENT_DATA_HASH_QUEUE_H

#include <condition_variable>
#include <deque>
#include <functional>
#include <map>
#include <pthread.h>
#include <mutex>

#include "torrent/hash_string.h"
#include "hash_queue_node.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ class lt_cacheline_aligned HashQueue : private std::deque<HashQueueNode> {
using base_type::back;

HashQueue(thread_disk* thread);
~HashQueue() { clear(); pthread_mutex_destroy(&m_done_chunks_lock); }
~HashQueue() { clear(); }

void push_back(ChunkHandle handle, HashQueueNode::id_type id, slot_done_type d);

Expand All @@ -98,7 +99,8 @@ class lt_cacheline_aligned HashQueue : private std::deque<HashQueueNode> {
done_chunks_type m_done_chunks;
slot_bool m_slot_has_work;

pthread_mutex_t m_done_chunks_lock lt_cacheline_aligned;
std::mutex m_done_chunks_lock;
std::condition_variable m_cv;
};

}
Expand Down
34 changes: 17 additions & 17 deletions src/dht/dht_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ DhtRouter::DhtRouter(const Object& cache, const rak::socket_address* sa) :

LT_LOG_THIS("adding nodes (size:%zu)", nodes.size());

for (Object::map_type::const_iterator itr = nodes.begin(); itr != nodes.end(); ++itr) {
if (itr->first.length() != HashString::size_data)
for (const auto& node : nodes) {
if (node.first.length() != HashString::size_data)
throw bencode_error("Loading cache: Invalid node hash.");

add_node_to_bucket(m_nodes.add_node(new DhtNode(itr->first, itr->second)));
add_node_to_bucket(m_nodes.add_node(new DhtNode(node.first, node.second)));
}
}

Expand All @@ -116,8 +116,8 @@ DhtRouter::DhtRouter(const Object& cache, const rak::socket_address* sa) :
if (cache.has_key("contacts")) {
const Object::list_type& contacts = cache.get_key_list("contacts");

for (Object::list_type::const_iterator itr = contacts.begin(); itr != contacts.end(); ++itr) {
Object::list_type::const_iterator litr = itr->as_list().begin();
for (const auto& contact : contacts) {
auto litr = contact.as_list().begin();
const std::string& host = litr->as_string();
int port = (++litr)->as_value();
m_contacts->emplace_back(host, port);
Expand All @@ -130,14 +130,14 @@ DhtRouter::~DhtRouter() {
stop();
delete m_contacts;

for (DhtBucketList::iterator itr = m_routingTable.begin(), last = m_routingTable.end(); itr != last; itr++)
delete itr->second;
for (auto& route : m_routingTable)
delete route.second;

for (DhtTrackerList::iterator itr = m_trackers.begin(), last = m_trackers.end(); itr != last; itr++)
delete itr->second;
for (auto& tracker : m_trackers)
delete tracker.second;

for (DhtNodeList::iterator itr = m_nodes.begin(), last = m_nodes.end(); itr != last; itr++)
delete itr->second;
for (auto& node : m_nodes)
delete node.second;
}

void
Expand Down Expand Up @@ -361,10 +361,10 @@ DhtRouter::store_cache(Object* container) const {
if (m_contacts != NULL) {
Object& contacts = container->insert_key("contacts", Object::create_list());

for (std::deque<contact_t>::const_iterator itr = m_contacts->begin(); itr != m_contacts->end(); ++itr) {
for (const auto& m_contact : *m_contacts) {
Object::list_type& list = contacts.insert_back(Object::create_list()).as_list();
list.emplace_back(itr->first);
list.emplace_back(itr->second);
list.emplace_back(m_contact.first);
list.emplace_back(m_contact.second);
}
}

Expand Down Expand Up @@ -633,9 +633,9 @@ DhtRouter::bootstrap() {

// Aggressively ping all questionable nodes in our own bucket to weed
// out bad nodes as early as possible and make room for fresh nodes.
for (DhtBucket::iterator itr = bucket()->begin(); itr != bucket()->end(); ++itr)
if (!(*itr)->is_good())
m_server.ping((*itr)->id(), (*itr)->address());
for (auto node : *bucket())
if (!node->is_good())
m_server.ping(node->id(), node->address());

// Also bootstrap a random bucket, if there are others.
if (m_routingTable.size() < 2)
Expand Down
14 changes: 7 additions & 7 deletions src/dht/dht_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,10 @@ DhtServer::parse_find_node_reply(DhtTransactionSearch* transaction, raw_string n
reinterpret_cast<const compact_node_info*>(nodes.data() + nodes.size() - nodes.size() % sizeof(compact_node_info)),
std::back_inserter(list));

for (node_info_list::iterator itr = list.begin(); itr != list.end(); ++itr) {
if (itr->id() != m_router->id()) {
rak::socket_address sa = itr->address();
transaction->search()->add_contact(itr->id(), &sa);
for (auto& node : list) {
if (node.id() != m_router->id()) {
rak::socket_address sa = node.address();
transaction->search()->add_contact(node.id(), &sa);
}
}

Expand Down Expand Up @@ -705,9 +705,9 @@ DhtServer::failed_transaction(transaction_itr itr, bool quick) {

void
DhtServer::clear_transactions() {
for (transaction_map::iterator itr = m_transactions.begin(), last = m_transactions.end(); itr != last; itr++) {
drop_packet(itr->second->packet());
delete itr->second;
for (auto& transaction : m_transactions) {
drop_packet(transaction.second->packet());
delete transaction.second;
}

m_transactions.clear();
Expand Down
7 changes: 4 additions & 3 deletions src/download/chunk_statistics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

#include "chunk_statistics.h"

#include <algorithm>

namespace torrent {

inline bool
Expand Down Expand Up @@ -145,9 +147,8 @@ ChunkStatistics::received_have_chunk(PeerChunks* pc, uint32_t index, uint32_t le

m_complete++;
m_accounted--;

for (iterator itr = base_type::begin(), last = base_type::end(); itr != last; ++itr)
*itr -= 1;

std::transform(base_type::begin(), base_type::end(), base_type::begin(), [] (auto c) { return c - 1; });
}

} else {
Expand Down
Loading