Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Toggle main menu visibility
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
"
16
#include "
roc_core/noncopyable.h
"
17
#include "
roc_core/stddefs.h
"
18
19
namespace
roc
{
20
namespace
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.
28
class
SpscByteBuffer
:
public
NonCopyable<> {
29
public
:
30
//! Initialize.
31
SpscByteBuffer
(
IArena
& arena,
size_t
chunk_size,
size_t
n_chunks);
32
33
//! Deinitialize.
34
~SpscByteBuffer
();
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
66
private
:
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_
roc::core::IArena
Memory arena interface.
Definition
iarena.h:23
roc::core::SpscByteBuffer::end_write
void end_write()
End writing of a chunk. Should be called if and only if begin_write() returned non-NULL....
roc::core::SpscByteBuffer::begin_read
uint8_t * begin_read()
Begin reading of a chunk. If buffer is empty, returns NULL. Should be called from reader thread....
roc::core::SpscByteBuffer::SpscByteBuffer
SpscByteBuffer(IArena &arena, size_t chunk_size, size_t n_chunks)
Initialize.
roc::core::SpscByteBuffer::~SpscByteBuffer
~SpscByteBuffer()
Deinitialize.
roc::core::SpscByteBuffer::begin_write
uint8_t * begin_write()
Begin writing of a chunk. If buffer is full, returns NULL. Should be called from writer thread....
roc::core::SpscByteBuffer::is_empty
bool is_empty() const
Check if buffer is empty.
roc::core::SpscByteBuffer::is_valid
bool is_valid() const
Check that initial allocation succeeded.
roc::core::SpscByteBuffer::end_read
void end_read()
End reading of a chunk. Should be called if and only if begin_read() returned non-NULL....
iarena.h
Memory arena interface.
roc::core
General-purpose building blocks and platform abstraction layer.
roc
Root namespace.
noncopyable.h
Non-copyable object.
stddefs.h
Commonly used types and functions.
roc_core
spsc_byte_buffer.h
Generated by
1.17.0