File: queue.hpp

package info (click to toggle)
snapcast 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,252 kB
  • sloc: cpp: 40,067; python: 3,260; sh: 455; makefile: 16
file content (180 lines) | stat: -rw-r--r-- 4,870 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***
    This file is part of snapcast
    Copyright (C) 2014-2025  Johannes Pohl

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
***/

#pragma once

// standard headers
#include <atomic>
#include <condition_variable>
#include <deque>
#include <mutex>


/// Queue with "wait for new element" functionality
template <typename T>
class Queue
{
public:
    /// c'tor
    Queue() = default;
    Queue(const Queue&) = delete;            ///< disable copying
    Queue& operator=(const Queue&) = delete; ///< disable assignment

    /// @return next element, delete from queue
    T pop()
    {
        std::unique_lock<std::mutex> mlock(mutex_);
        cond_.wait(mlock, [this]() { return queue_.empty(); });

        //		std::lock_guard<std::mutex> lock(mutex_);
        auto val = queue_.front();
        queue_.pop_front();
        return val;
    }

    /// abort wait
    void abort_wait()
    {
        {
            std::lock_guard<std::mutex> mlock(mutex_);
            abort_ = true;
        }
        cond_.notify_one();
    }

    /// wait for @p timeout for new element, @return if an element has been added
    bool wait_for(const std::chrono::microseconds& timeout) const
    {
        std::unique_lock<std::mutex> mlock(mutex_);
        abort_ = false;
        if (!cond_.wait_for(mlock, timeout, [this]() { return (!queue_.empty() || abort_); }))
            return false;

        return !queue_.empty() && !abort_;
    }

    /// @return if an element has been returned in @p item within @p timeout
    bool try_pop(T& item, const std::chrono::microseconds& timeout = std::chrono::microseconds(0))
    {
        std::unique_lock<std::mutex> mlock(mutex_);
        abort_ = false;
        if (timeout.count() > 0)
        {
            if (!cond_.wait_for(mlock, timeout, [this]() { return (!queue_.empty() || abort_); }))
                return false;
        }

        if (queue_.empty() || abort_)
            return false;

        item = std::move(queue_.front());
        queue_.pop_front();

        return true;
    }

    /// return next element in @p item, wait for an element if queue is empty
    void pop(T& item)
    {
        std::unique_lock<std::mutex> mlock(mutex_);
        while (queue_.empty())
            cond_.wait(mlock);

        item = queue_.front();
        queue_.pop_front();
    }

    /// Add @p item to the queue
    void push_front(const T& item)
    {
        {
            std::lock_guard<std::mutex> mlock(mutex_);
            queue_.push_front(item);
        }
        cond_.notify_one();
    }

    /// return a copy of the next element in @p copy, @return false if the queue is empty
    bool back_copy(T& copy)
    {
        std::lock_guard<std::mutex> mlock(mutex_);
        if (queue_.empty())
            return false;
        copy = queue_.back();
        return true;
    }

    /// return a copy of the last element in @p copy, @return false if the queue is empty
    bool front_copy(T& copy)
    {
        std::lock_guard<std::mutex> mlock(mutex_);
        if (queue_.empty())
            return false;
        copy = queue_.front();
        return true;
    }

    /// Add element @p item at the front of the queue
    void push_front(T&& item)
    {
        {
            std::lock_guard<std::mutex> mlock(mutex_);
            queue_.push_front(std::move(item));
        }
        cond_.notify_one();
    }

    /// Add element @p item at the end of the queue
    void push(const T& item)
    {
        {
            std::lock_guard<std::mutex> mlock(mutex_);
            queue_.push_back(item);
        }
        cond_.notify_one();
    }

    /// Add element @p item at the end of the queue
    void push(T&& item)
    {
        {
            std::lock_guard<std::mutex> mlock(mutex_);
            queue_.push_back(std::move(item));
        }
        cond_.notify_one();
    }

    /// @return number of elements in the queue
    size_t size() const
    {
        std::lock_guard<std::mutex> mlock(mutex_);
        return queue_.size();
    }

    /// @return if the queue is empty
    bool empty() const
    {
        return (size() == 0);
    }

private:
    std::deque<T> queue_;
    mutable std::atomic<bool> abort_;
    mutable std::mutex mutex_;
    mutable std::condition_variable cond_;
};