Various scheduler and therad cleanups.

This commit is contained in:
Jari Sundell
2025-05-01 09:38:49 +02:00
committed by GitHub
parent 3d9c083032
commit 97415c656b
10 changed files with 55 additions and 247 deletions
+35 -31
View File
@@ -1,8 +1,6 @@
#include "config.h"
#include "thread_worker.h"
#include "globals.h"
#include "control.h"
#include <fcntl.h>
#include <unistd.h>
@@ -11,12 +9,14 @@
#include <torrent/exceptions.h>
#include <torrent/poll.h>
#include "globals.h"
#include "control.h"
#include "core/manager.h"
#include "rpc/scgi.h"
#include "rpc/parse_commands.h"
ThreadWorker::~ThreadWorker() {
if (m_scgi)
if (m_scgi != nullptr)
m_scgi.load()->deactivate();
}
@@ -29,13 +29,17 @@ ThreadWorker::init_thread() {
bool
ThreadWorker::set_scgi(rpc::SCgi* scgi) {
rpc::SCgi* expected = nullptr;
if (!m_scgi.compare_exchange_strong(expected, scgi))
return false;
change_rpc_log();
callback(nullptr, [this]() {
start_scgi(this);
if (m_scgi == NULL)
throw torrent::internal_error("Tried to start SCGI but object was not present.");
m_scgi.load()->activate();
});
return true;
@@ -43,32 +47,12 @@ ThreadWorker::set_scgi(rpc::SCgi* scgi) {
void
ThreadWorker::set_rpc_log(const std::string& filename) {
m_rpcLog = filename;
callback(nullptr, [this]() {
msg_change_rpc_log(this);
callback(nullptr, [this, filename]() {
m_rpc_log_filename = filename;
change_rpc_log();
});
}
void
ThreadWorker::start_scgi(ThreadBase* baseThread) {
ThreadWorker* thread = (ThreadWorker*)baseThread;
if (thread->scgi() == NULL)
throw torrent::internal_error("Tried to start SCGI but object was not present.");
thread->scgi()->activate();
}
void
ThreadWorker::msg_change_rpc_log(ThreadBase* baseThread) {
ThreadWorker* thread = (ThreadWorker*)baseThread;
acquire_global_lock();
thread->change_rpc_log();
release_global_lock();
}
void
ThreadWorker::change_rpc_log() {
if (scgi() == NULL)
@@ -80,15 +64,35 @@ ThreadWorker::change_rpc_log() {
control->core()->push_log("Closed RPC log.");
}
if (m_rpcLog.empty())
if (m_rpc_log_filename.empty())
return;
scgi()->set_log_fd(open(rak::path_expand(m_rpcLog).c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644));
scgi()->set_log_fd(open(rak::path_expand(m_rpc_log_filename).c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644));
if (scgi()->log_fd() == -1) {
control->core()->push_log_std("Could not open RPC log file '" + m_rpcLog + "'.");
control->core()->push_log_std("Could not open RPC log file '" + m_rpc_log_filename + "'.");
return;
}
control->core()->push_log_std("Logging RPC events to '" + m_rpcLog + "'.");
control->core()->push_log_std("Logging RPC events to '" + m_rpc_log_filename + "'.");
}
void
ThreadWorker::call_events() {
if ((m_flags & flag_do_shutdown)) {
if ((m_flags & flag_did_shutdown))
throw torrent::internal_error("Already trigged shutdown.");
m_flags |= flag_did_shutdown;
throw torrent::shutdown_exception();
}
cachedTime = rak::timer::current();
process_callbacks();
}
std::chrono::microseconds
ThreadWorker::next_timeout() {
return std::chrono::microseconds(10min);
}