This commit is contained in:
rakshasa
2026-08-04 11:04:20 +02:00
parent 5ab973504a
commit 49d45c1088
2 changed files with 11 additions and 16 deletions
+8 -12
View File
@@ -2,7 +2,7 @@
#include "utils/waitpid_queue.h" #include "utils/waitpid_queue.h"
#include <unistd.h> #include <sys/wait.h>
#include <torrent/exceptions.h> #include <torrent/exceptions.h>
namespace utils { namespace utils {
@@ -10,7 +10,7 @@ namespace utils {
WaitpidQueue::WaitpidQueue() { WaitpidQueue::WaitpidQueue() {
m_worker = std::async(std::launch::async, [this]() { m_worker = std::async(std::launch::async, [this]() {
bool is_running = true; bool is_running = true;
auto wait_time = 1s; auto wait_time = 50ms;
while (is_running) { while (is_running) {
if (!m_queue.empty()) { if (!m_queue.empty()) {
@@ -27,9 +27,10 @@ WaitpidQueue::WaitpidQueue() {
} else { } else {
m_wakeup_worker.wait(false, std::memory_order_acquire); m_wakeup_worker.wait(false, std::memory_order_acquire);
std::this_thread::sleep_for(50ms);
} }
std::vector<int> queue; std::set<pid_t> queue;
{ {
std::lock_guard<std::mutex> guard(m_mutex); std::lock_guard<std::mutex> guard(m_mutex);
@@ -49,7 +50,7 @@ WaitpidQueue::WaitpidQueue() {
m_wakeup_worker.store(false, std::memory_order_release); m_wakeup_worker.store(false, std::memory_order_release);
} }
wait_time = std::min(10s, wait_time * 2); wait_time = std::min(10 * 1000ms, wait_time * 2);
for (int pid : queue) { for (int pid : queue) {
if (::waitpid(pid, nullptr, WNOHANG) == 0) if (::waitpid(pid, nullptr, WNOHANG) == 0)
@@ -58,16 +59,11 @@ WaitpidQueue::WaitpidQueue() {
{ {
std::lock_guard<std::mutex> guard(m_mutex); std::lock_guard<std::mutex> guard(m_mutex);
auto itr = std::find(m_queue.begin(), m_queue.end(), pid); if (m_queue.erase(pid) != 1)
if (itr == m_queue.end())
throw torrent::internal_error("WaitpidQueue worker thread could not find pid in queue."); throw torrent::internal_error("WaitpidQueue worker thread could not find pid in queue.");
*itr = m_queue.back();
m_queue.pop_back();
} }
wait_time = 1s; wait_time = std::max(50ms, wait_time / 2);
m_remaining.fetch_sub(1, std::memory_order_release); m_remaining.fetch_sub(1, std::memory_order_release);
m_remaining.notify_all(); m_remaining.notify_all();
@@ -103,7 +99,7 @@ WaitpidQueue::close_pid(pid_t pid) {
// return; // return;
// } // }
m_queue.push_back(pid); m_queue.insert(pid);
} }
m_wakeup_worker.store(true, std::memory_order_release); m_wakeup_worker.store(true, std::memory_order_release);
+3 -4
View File
@@ -1,9 +1,8 @@
#ifndef RTORRENT_UTILS_WAITPID_QUEUE_H #ifndef RTORRENT_UTILS_WAITPID_QUEUE_H
#define RTORRENT_UTILS_WAITPID_QUEUE_H #define RTORRENT_UTILS_WAITPID_QUEUE_H
#include <deque>
#include <future> #include <future>
#include <vector> #include <set>
#include <torrent/system/common.h> #include <torrent/system/common.h>
namespace utils { namespace utils {
@@ -28,7 +27,7 @@ private:
align_cacheline align_cacheline
std::mutex m_mutex; std::mutex m_mutex;
std::vector<pid_t> m_queue; std::set<pid_t> m_queue;
bool m_should_shutdown{}; bool m_should_shutdown{};
@@ -40,6 +39,6 @@ private:
inline uint32_t WaitpidQueue::size() const { return m_remaining.load(std::memory_order_acquire); } inline uint32_t WaitpidQueue::size() const { return m_remaining.load(std::memory_order_acquire); }
} // namespace torrent::utils } // namespace utils
#endif #endif