Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
concurrent_queue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_packet/concurrent_queue.h
10 //! @brief Concurrent blocking packet queue.
11 
12 #ifndef ROC_PACKET_CONCURRENT_QUEUE_H_
13 #define ROC_PACKET_CONCURRENT_QUEUE_H_
14 
15 #include "roc_core/mpsc_queue.h"
16 #include "roc_core/mutex.h"
17 #include "roc_core/noncopyable.h"
18 #include "roc_core/optional.h"
19 #include "roc_core/semaphore.h"
20 #include "roc_packet/ireader.h"
21 #include "roc_packet/iwriter.h"
22 #include "roc_packet/packet.h"
23 
24 namespace roc {
25 namespace packet {
26 
27 //! Concurrent blocking packet queue.
28 class ConcurrentQueue : public IReader, public IWriter, public core::NonCopyable<> {
29 public:
30  //! Queue mode.
31  enum Mode {
32  Blocking, //!< Read operation blocks until queue is non-empty.
33  NonBlocking //!< Read operation returns null if queue is empty.
34  };
35 
36  //! Initialize.
37  //! @p mode defines whether reads will be blocking.
38  explicit ConcurrentQueue(Mode mode);
39 
40  //! Read next packet.
41  //! If reads are not concurrent, and queue is non-blocking, then
42  //! reads are wait-free. Otherwise they may block.
43  //! @see Mode.
45 
46  //! Add packet to the queue.
47  //! Wait-free operation.
49 
50 private:
52  core::Mutex read_mutex_;
54 };
55 
56 } // namespace packet
57 } // namespace roc
58 
59 #endif // ROC_PACKET_CONCURRENT_QUEUE_H_
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Thread-safe lock-free node-based intrusive multi-producer single-consumer queue.
Definition: mpsc_queue.h:45
Mutex.
Definition: mutex.h:31
Base class for non-copyable objects.
Definition: noncopyable.h:23
Optionally constructed object.
Definition: optional.h:25
Concurrent blocking packet queue.
virtual ROC_ATTR_NODISCARD status::StatusCode write(const PacketPtr &packet)
Add packet to the queue. Wait-free operation.
ConcurrentQueue(Mode mode)
Initialize. mode defines whether reads will be blocking.
virtual ROC_ATTR_NODISCARD status::StatusCode read(PacketPtr &)
Read next packet. If reads are not concurrent, and queue is non-blocking, then reads are wait-free....
@ NonBlocking
Read operation returns null if queue is empty.
@ Blocking
Read operation blocks until queue is non-empty.
Packet reader interface.
Definition: ireader.h:23
Packet writer interface.
Definition: iwriter.h:23
Packet reader interface.
Packet writer interface.
Multi-producer single-consumer queue.
Mutex.
Root namespace.
Non-copyable object.
Optionally constructed object.
Packet.
StatusCode
Status code.
Definition: status_code.h:19