mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-18 23:26:17 +00:00
Minor fixes to WaitpidQueue.
This commit is contained in:
+14
-22
@@ -9,38 +9,37 @@ 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;
|
|
||||||
auto wait_time = 50ms;
|
auto wait_time = 50ms;
|
||||||
|
|
||||||
while (is_running) {
|
while (true) {
|
||||||
if (!m_queue.empty()) {
|
if (!m_queue.empty()) {
|
||||||
auto start_time = std::chrono::steady_clock::now();
|
auto start_time = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
while (!m_wakeup_worker.load(std::memory_order_acquire) && !m_should_shutdown) {
|
while (std::chrono::steady_clock::now() - start_time < wait_time) {
|
||||||
auto elapsed = std::chrono::steady_clock::now() - start_time;
|
if (m_should_shutdown.load(std::memory_order_acquire))
|
||||||
|
return;
|
||||||
if (elapsed >= wait_time)
|
|
||||||
break;
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(50ms);
|
std::this_thread::sleep_for(50ms);
|
||||||
|
|
||||||
|
if (m_wakeup_worker.load(std::memory_order_acquire))
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adds a small delay to allow new processes to finish if they're quickly spawned and
|
||||||
|
// terminated.
|
||||||
|
std::this_thread::sleep_for(50ms);
|
||||||
|
|
||||||
std::set<pid_t> queue;
|
std::set<pid_t> queue;
|
||||||
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_mutex);
|
std::lock_guard<std::mutex> guard(m_mutex);
|
||||||
|
|
||||||
if (m_should_shutdown) {
|
if (m_should_shutdown)
|
||||||
if (m_queue.empty())
|
return;
|
||||||
return;
|
|
||||||
|
|
||||||
is_running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_queue.empty())
|
if (m_queue.empty())
|
||||||
throw torrent::internal_error("WaitpidQueue worker thread woke up but queue is empty.");
|
throw torrent::internal_error("WaitpidQueue worker thread woke up but queue is empty.");
|
||||||
@@ -72,6 +71,7 @@ WaitpidQueue::WaitpidQueue() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We don't wait for the worker thread to finish as waitpid isn't needed to be called on shutdown.
|
||||||
WaitpidQueue::~WaitpidQueue() {
|
WaitpidQueue::~WaitpidQueue() {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_mutex);
|
std::lock_guard<std::mutex> guard(m_mutex);
|
||||||
@@ -80,8 +80,6 @@ WaitpidQueue::~WaitpidQueue() {
|
|||||||
|
|
||||||
m_wakeup_worker.store(true, std::memory_order_release);
|
m_wakeup_worker.store(true, std::memory_order_release);
|
||||||
m_wakeup_worker.notify_all();
|
m_wakeup_worker.notify_all();
|
||||||
|
|
||||||
// m_worker.wait();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -93,12 +91,6 @@ WaitpidQueue::close_pid(pid_t pid) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_mutex);
|
std::lock_guard<std::mutex> guard(m_mutex);
|
||||||
|
|
||||||
// if (!m_queue.empty()) {
|
|
||||||
// m_queue.push_back(pid);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
m_queue.insert(pid);
|
m_queue.insert(pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,11 +29,11 @@ private:
|
|||||||
std::mutex m_mutex;
|
std::mutex m_mutex;
|
||||||
std::set<pid_t> m_queue;
|
std::set<pid_t> m_queue;
|
||||||
|
|
||||||
bool m_should_shutdown{};
|
|
||||||
|
|
||||||
align_cacheline
|
align_cacheline
|
||||||
|
|
||||||
std::atomic<bool> m_wakeup_worker{};
|
std::atomic<bool> m_wakeup_worker{};
|
||||||
|
std::atomic<bool> m_should_shutdown{};
|
||||||
|
|
||||||
std::atomic<uint32_t> m_remaining{};
|
std::atomic<uint32_t> m_remaining{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user