Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Channel.hpp « libslic3r « src - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68369af63837576e987f8773a0e816c9c308aedb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef slic3r_Channel_hpp_
#define slic3r_Channel_hpp_

#include <memory>
#include <deque>
#include <condition_variable>
#include <mutex>
#include <utility>
#include <boost/optional.hpp>


namespace Slic3r {


template<class T> class Channel
{
public:
    using UniqueLock = std::unique_lock<std::mutex>;

    template<class Ptr> class Unlocker
    {
    public:
        Unlocker(UniqueLock lock) : m_lock(std::move(lock)) {}
        Unlocker(const Unlocker &other) noexcept : m_lock(std::move(other.m_lock)) {}     // XXX: done beacuse of MSVC 2013 not supporting init of deleter by move
        Unlocker(Unlocker &&other) noexcept : m_lock(std::move(other.m_lock)) {}
        Unlocker& operator=(const Unlocker &other) = delete;
        Unlocker& operator=(Unlocker &&other) { m_lock = std::move(other.m_lock); }

        void operator()(Ptr*) { m_lock.unlock(); }
    private:
        mutable UniqueLock m_lock;    // XXX: mutable: see above
    };

    using Queue = std::deque<T>;
    using LockedConstPtr = std::unique_ptr<const Queue, Unlocker<const Queue>>;
    using LockedPtr = std::unique_ptr<Queue, Unlocker<Queue>>;

    Channel() {}
    ~Channel() {}

    void push(const T& item, bool silent = false)
    {
        {
            UniqueLock lock(m_mutex);
            m_queue.push_back(item);
        }
        if (! silent) { m_condition.notify_one(); }
    }

    void push(T &&item, bool silent = false)
    {
        {
            UniqueLock lock(m_mutex);
            m_queue.push_back(std::forward<T>(item));
        }
        if (! silent) { m_condition.notify_one(); }
    }

    T pop()
    {
        UniqueLock lock(m_mutex);
        m_condition.wait(lock, [this]() { return !m_queue.empty(); });
        auto item = std::move(m_queue.front());
        m_queue.pop_front();
        return item;
    }

    boost::optional<T> try_pop()
    {
        UniqueLock lock(m_mutex);
        if (m_queue.empty()) {
            return boost::none;
        } else {
            auto item = std::move(m_queue.front());
            m_queue.pop();
            return item;
        }
    }

    // Unlocked observer/hint. Thread unsafe! Keep in mind you need to re-verify the result after locking.
    size_t size_hint() const noexcept { return m_queue.size(); }

    LockedConstPtr lock_read() const
    {
        return LockedConstPtr(&m_queue, Unlocker<const Queue>(UniqueLock(m_mutex)));
    }

    LockedPtr lock_rw()
    {
        return LockedPtr(&m_queue, Unlocker<Queue>(UniqueLock(m_mutex)));
    }
private:
    Queue m_queue;
    mutable std::mutex m_mutex;
    std::condition_variable m_condition;
};


} // namespace Slic3r

#endif // slic3r_Channel_hpp_