Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
spsc_byte_buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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_core/spsc_byte_buffer.h
10//! @brief Single-producer single-consumer circular buffer of byte chunks.
11
12#ifndef ROC_CORE_SPSC_BYTE_BUFFER_H_
13#define ROC_CORE_SPSC_BYTE_BUFFER_H_
14
15#include "roc_core/iarena.h"
17#include "roc_core/stddefs.h"
18
19namespace roc {
20namespace core {
21
22//! Thread-safe lock-free single-producer single-consumer
23//! circular buffer of byte chunks.
24//!
25//! Allows access from two concurrent threads: writer and reader.
26//! Both writer and reader are never blocked.
27//! Provides sequential consistency.
28class SpscByteBuffer : public NonCopyable<> {
29public:
30 //! Initialize.
31 SpscByteBuffer(IArena& arena, size_t chunk_size, size_t n_chunks);
32
33 //! Deinitialize.
35
36 //! Check that initial allocation succeeded.
37 bool is_valid() const;
38
39 //! Check if buffer is empty.
40 bool is_empty() const;
41
42 //! Begin writing of a chunk.
43 //! If buffer is full, returns NULL.
44 //! Should be called from writer thread.
45 //! Lock-free.
46 uint8_t* begin_write();
47
48 //! End writing of a chunk.
49 //! Should be called if and only if begin_write() returned non-NULL.
50 //! Should be called from writer thread.
51 //! Lock-free.
52 void end_write();
53
54 //! Begin reading of a chunk.
55 //! If buffer is empty, returns NULL.
56 //! Should be called from reader thread.
57 //! Lock-free.
58 uint8_t* begin_read();
59
60 //! End reading of a chunk.
61 //! Should be called if and only if begin_read() returned non-NULL.
62 //! Should be called from reader thread.
63 //! Lock-free.
64 void end_read();
65
66private:
67 IArena& arena_;
68
69 const size_t chunk_size_;
70 const size_t chunk_count_;
71
72 void* memory_;
73 uint8_t** chunks_;
74
75 uint32_t read_pos_;
76 uint32_t write_pos_;
77};
78
79} // namespace core
80} // namespace roc
81
82#endif // ROC_CORE_SPSC_BYTE_BUFFER_H_
Memory arena interface.
Definition iarena.h:23
Base class for non-copyable objects.
Definition noncopyable.h:23
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
Thread-safe lock-free single-producer single-consumer circular buffer of byte chunks.
void end_write()
End writing of a chunk. Should be called if and only if begin_write() returned non-NULL....
uint8_t * begin_read()
Begin reading of a chunk. If buffer is empty, returns NULL. Should be called from reader thread....
SpscByteBuffer(IArena &arena, size_t chunk_size, size_t n_chunks)
Initialize.
~SpscByteBuffer()
Deinitialize.
uint8_t * begin_write()
Begin writing of a chunk. If buffer is full, returns NULL. Should be called from writer thread....
bool is_empty() const
Check if buffer is empty.
bool is_valid() const
Check that initial allocation succeeded.
void end_read()
End reading of a chunk. Should be called if and only if begin_read() returned non-NULL....
Memory arena interface.
Root namespace.
Non-copyable object.
Commonly used types and functions.