From e8c1f3ed2c45351262ea520fa731411b45901e69 Mon Sep 17 00:00:00 2001 From: Jari Sundell Date: Sat, 31 May 2025 15:22:31 +0200 Subject: [PATCH] Update to use new this_thread::Poll(). --- src/control.cc | 5 ++--- src/core/curl_socket.cc | 23 ++++++++++------------- src/core/curl_socket.h | 37 +------------------------------------ src/rpc/scgi.cc | 16 ++++++++++------ src/rpc/scgi.h | 10 +++++----- src/rpc/scgi_task.cc | 17 ++++++++--------- 6 files changed, 36 insertions(+), 72 deletions(-) diff --git a/src/control.cc b/src/control.cc index c5b93483..9242efd3 100644 --- a/src/control.cc +++ b/src/control.cc @@ -23,7 +23,6 @@ #include "rpc/command_scheduler.h" #include "rpc/lua.h" #include "rpc/parse_commands.h" -#include "rpc/scgi.h" #include "rpc/object_storage.h" #include "ui/root.h" @@ -90,7 +89,7 @@ Control::initialize() { m_ui->init(this); if(!display::Canvas::daemon()) { - m_inputStdin->insert(torrent::main_thread()->poll()); + m_inputStdin->insert(torrent::this_thread::poll()); } } @@ -101,7 +100,7 @@ Control::cleanup() { torrent::this_thread::scheduler()->erase(&m_task_shutdown); if(!display::Canvas::daemon()) { - m_inputStdin->remove(torrent::main_thread()->poll()); + m_inputStdin->remove(torrent::this_thread::poll()); } m_core->download_store()->disable(); diff --git a/src/core/curl_socket.cc b/src/core/curl_socket.cc index b7b5af66..b72e3e57 100644 --- a/src/core/curl_socket.cc +++ b/src/core/curl_socket.cc @@ -1,18 +1,15 @@ #include "config.h" +#include "curl_socket.h" + #include - -#include #include - #include #include #include #include "control.h" - -#include "curl_socket.h" -#include "curl_stack.h" +#include "core/curl_stack.h" namespace core { @@ -39,22 +36,22 @@ CurlSocket::receive_socket([[maybe_unused]] void* easy_handle, curl_socket_t fd, if (socket == NULL) { socket = stack->new_socket(fd); - torrent::main_thread()->poll()->open(socket); + torrent::this_thread::poll()->open(socket); // No interface for libcurl to signal when it's interested in error events. // Assume that hence it must always be interested in them. - torrent::main_thread()->poll()->insert_error(socket); + torrent::this_thread::poll()->insert_error(socket); } if (what == CURL_POLL_NONE || what == CURL_POLL_OUT) - torrent::main_thread()->poll()->remove_read(socket); + torrent::this_thread::poll()->remove_read(socket); else - torrent::main_thread()->poll()->insert_read(socket); + torrent::this_thread::poll()->insert_read(socket); if (what == CURL_POLL_NONE || what == CURL_POLL_IN) - torrent::main_thread()->poll()->remove_write(socket); + torrent::this_thread::poll()->remove_write(socket); else - torrent::main_thread()->poll()->insert_write(socket); + torrent::this_thread::poll()->insert_write(socket); return 0; } @@ -68,7 +65,7 @@ CurlSocket::close() { if (m_fileDesc == -1) throw torrent::internal_error("CurlSocket::close() m_fileDesc == -1."); - torrent::main_thread()->poll()->closed(this); + torrent::this_thread::poll()->closed(this); m_fileDesc = -1; } diff --git a/src/core/curl_socket.h b/src/core/curl_socket.h index fef4bff8..20999c65 100644 --- a/src/core/curl_socket.h +++ b/src/core/curl_socket.h @@ -1,42 +1,7 @@ -// rTorrent - BitTorrent client -// Copyright (C) 2005-2008, Jari Sundell -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// In addition, as a special exception, the copyright holders give -// permission to link the code of portions of this program with the -// OpenSSL library under certain conditions as described in each -// individual source file, and distribute linked combinations -// including the two. -// -// You must obey the GNU General Public License in all respects for -// all of the code used other than OpenSSL. If you modify file(s) -// with this exception, you may extend this exception to your version -// of the file(s), but you are not obligated to do so. If you do not -// wish to do so, delete this exception statement from your version. -// If you delete this exception statement from all source files in the -// program, then also delete it here. -// -// Contact: Jari Sundell -// -// Skomakerveien 33 -// 3185 Skoppum, NORWAY - #ifndef RTORRENT_CORE_CURL_SOCKET_H #define RTORRENT_CORE_CURL_SOCKET_H +#include #include #include "globals.h" diff --git a/src/rpc/scgi.cc b/src/rpc/scgi.cc index 79c50071..9b972585 100644 --- a/src/rpc/scgi.cc +++ b/src/rpc/scgi.cc @@ -1,5 +1,6 @@ #include "config.h" +#include #include #include #include @@ -13,6 +14,7 @@ #include "rpc/scgi_task.h" #include "utils/socket_fd.h" +// TODO: Figure out why moving this to the top causes a build error. #include "rpc/scgi.h" namespace rpc { @@ -90,16 +92,18 @@ SCgi::open(void* sa, unsigned int length) { void SCgi::activate() { - worker_thread->poll()->open(this); - worker_thread->poll()->insert_read(this); - worker_thread->poll()->insert_error(this); + assert(std::this_thread::get_id() == worker_thread->thread_id() && "SCgi::activate() must be called from the worker thread."); + + torrent::this_thread::poll()->open(this); + torrent::this_thread::poll()->insert_read(this); + torrent::this_thread::poll()->insert_error(this); } void SCgi::deactivate() { - worker_thread->poll()->remove_read(this); - worker_thread->poll()->remove_error(this); - worker_thread->poll()->close(this); + assert(std::this_thread::get_id() == worker_thread->thread_id() && "SCgi::deactivate() must be called from the worker thread."); + + torrent::this_thread::poll()->remove_and_close(this); } void diff --git a/src/rpc/scgi.h b/src/rpc/scgi.h index 69b01b34..afb0340e 100644 --- a/src/rpc/scgi.h +++ b/src/rpc/scgi.h @@ -18,9 +18,9 @@ public: static const int max_tasks = 100; SCgi() : m_logFd(-1) {} - virtual ~SCgi(); + ~SCgi() override; - virtual const char* type_name() const { return "scgi"; } + const char* type_name() const override { return "scgi"; } void open_port(void* sa, unsigned int length, bool dontRoute); void open_named(const std::string& filename); @@ -33,9 +33,9 @@ public: int log_fd() const { return m_logFd; } void set_log_fd(int fd) { m_logFd = fd; } - virtual void event_read(); - virtual void event_write(); - virtual void event_error(); + void event_read() override; + void event_write() override; + void event_error() override; utils::SocketFd& get_fd() { return *reinterpret_cast(&m_fileDesc); } diff --git a/src/rpc/scgi_task.cc b/src/rpc/scgi_task.cc index 28aae989..05df7c90 100644 --- a/src/rpc/scgi_task.cc +++ b/src/rpc/scgi_task.cc @@ -1,5 +1,7 @@ #include "config.h" +#include "rpc/scgi_task.h" + #include #include #include @@ -29,9 +31,9 @@ SCgiTask::open(SCgi* parent, int fd) { m_position = m_buffer; m_body = NULL; - torrent::thread_self()->poll()->open(this); - torrent::thread_self()->poll()->insert_read(this); - torrent::thread_self()->poll()->insert_error(this); + torrent::this_thread::poll()->open(this); + torrent::this_thread::poll()->insert_read(this); + torrent::this_thread::poll()->insert_error(this); } void @@ -42,10 +44,7 @@ SCgiTask::close() { torrent::main_thread()->cancel_callback_and_wait(this); torrent::thread_self()->cancel_callback(this); - torrent::thread_self()->poll()->remove_read(this); - torrent::thread_self()->poll()->remove_write(this); - torrent::thread_self()->poll()->remove_error(this); - torrent::thread_self()->poll()->close(this); + torrent::this_thread::poll()->remove_and_close(this); get_fd().close(); get_fd().clear(); @@ -164,7 +163,7 @@ SCgiTask::event_read() { if ((unsigned int)std::distance(m_buffer, m_position) != m_buffer_size) return; - torrent::thread_self()->poll()->remove_read(this); + torrent::this_thread::poll()->remove_read(this); if (m_parent->log_fd() >= 0) { int __UNUSED result; @@ -272,7 +271,7 @@ SCgiTask::receive_call(const char* buffer, uint32_t length) { m_result_mutex.lock(); m_result_mutex.unlock(); - torrent::thread_self()->poll()->insert_write(this); + torrent::this_thread::poll()->insert_write(this); }); };