Skip to content

Commit

Permalink
Add comments, remove some redundant template fiddling
Browse files Browse the repository at this point in the history
Also make ThreadSafeQueueSPSC not copyable and non movable
  • Loading branch information
tamasmeszaros committed Dec 7, 2023
1 parent 155b152 commit 7975dfa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/slic3r/GUI/Jobs/BoostThreadWorker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class BoostThreadWorker : public Worker, private Job::Ctl
void deliver(BoostThreadWorker &runner);
};

// The m_running state flag needs special attention. Previously, it was set simply in the run()
// method whenever a new job was taken from the input queue and unset after the finalize message
// was pushed into the output queue. This was not correct. It must not be possible to consume
// the finalize message before the flag gets unset, these two operations must be done atomically
// So the underlying queues are here extended to support handling of this m_running flag.
template<class El>
class RawQueue: public std::deque<El> {
std::atomic<bool> *m_running_ptr;
Expand All @@ -79,6 +84,7 @@ class BoostThreadWorker : public Worker, private Job::Ctl
void set_stopped() { m_running_ptr->store(false); }
};

// The running flag is set if a job is popped from the queue
template<class El>
class RawJobQueue: public RawQueue<El> {
public:
Expand All @@ -90,6 +96,7 @@ class BoostThreadWorker : public Worker, private Job::Ctl
}
};

// The running flag is unset when the finalize message is pushed into the queue
template<class El>
class RawMsgQueue: public RawQueue<El> {
public:
Expand Down
17 changes: 7 additions & 10 deletions src/slic3r/GUI/Jobs/ThreadSafeQueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ struct BlockingWait
unsigned timeout_ms = 0;
};

template<class T, class... Args>
using NonSpecialMembersOnly = std::enable_if_t<
(sizeof...(Args) >= 1) && !(... || std::is_convertible_v<Args, T>)>;

// A thread safe queue for one producer and one consumer.
template<class T,
template<class, class...> class Container = std::deque,
Expand All @@ -36,15 +32,16 @@ class ThreadSafeQueueSPSC
std::condition_variable m_cond_var;

public:
template<class...Qargs, class = NonSpecialMembersOnly<ThreadSafeQueueSPSC, Qargs...>>

// Forward arguments to the underlying queue
template<class...Qargs>
ThreadSafeQueueSPSC(Qargs &&...qargs)
: m_queue{Container<T, ContainerArgs...>{std::forward<Qargs>(qargs)...}} {}

ThreadSafeQueueSPSC() = default;
ThreadSafeQueueSPSC(const ThreadSafeQueueSPSC&) = default;
ThreadSafeQueueSPSC(ThreadSafeQueueSPSC&&) = default;
ThreadSafeQueueSPSC& operator=(const ThreadSafeQueueSPSC&) = default;
ThreadSafeQueueSPSC& operator=(ThreadSafeQueueSPSC &&) = default;
ThreadSafeQueueSPSC(const ThreadSafeQueueSPSC&) = delete;
ThreadSafeQueueSPSC(ThreadSafeQueueSPSC&&) = delete;
ThreadSafeQueueSPSC& operator=(const ThreadSafeQueueSPSC&) = delete;
ThreadSafeQueueSPSC& operator=(ThreadSafeQueueSPSC &&) = delete;

// Consume one element, block if the queue is empty.
template<class Fn>
Expand Down

0 comments on commit 7975dfa

Please sign in to comment.