diff --git a/src/pid_watcher.cc b/src/pid_watcher.cc new file mode 100644 index 00000000..cc3d5b1e --- /dev/null +++ b/src/pid_watcher.cc @@ -0,0 +1,20 @@ +#include "config.h" + +#include "pid_watcher.h" + +#include +#include + +PidWatcher::PidWatcher(std::thread::id thread_id) + : m_thread_id(thread_id) { +} + +PidWatcher::~PidWatcher() = default; + +void +PidWatcher::prepare_spawn() { + assert(std::this_thread::get_id() == m_thread_id); + + if (m_spawn_in_progress.exchange(true, std::memory_order_acquire)) + throw torrent::internal_error("PidWatcher::prepare_spawn() called while spawn already in progress."); +} diff --git a/src/pid_watcher.h b/src/pid_watcher.h new file mode 100644 index 00000000..93ea70d3 --- /dev/null +++ b/src/pid_watcher.h @@ -0,0 +1,27 @@ +#ifndef RTORRENT_PID_WATCHER_H +#define RTORRENT_PID_WATCHER_H + +#include + +struct atomic_queue { + std::array, 16> queue{}; + + +class PidWatcher { +public: + PidWatcher(std::thread::id thread_id); + ~PidWatcher(); + + void prepare_spawn(); + void completed_spawn(pid_t pid); + +private: + std::thread::id m_thread_id; + + align_cacheline + + std::atomic m_spawn_in_progress{}; + +}; + +#endif