Skip to content

Commit 7429b63

Browse files
committed
Initial commit
0 parents  commit 7429b63

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CMakeLists.txt.user
2+
CMakeCache.txt
3+
CMakeFiles
4+
CMakeScripts
5+
Testing
6+
Makefile
7+
cmake_install.cmake
8+
install_manifest.txt
9+
compile_commands.json
10+
CTestTestfile.cmake
11+
_deps
12+
.vs/
13+
out/

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# CMakeList.txt : Top-level CMake project file, do global configuration
2+
# and include sub-projects here.
3+
#
4+
cmake_minimum_required (VERSION 3.8)
5+
6+
# Enable Hot Reload for MSVC compilers if supported.
7+
if (POLICY CMP0141)
8+
cmake_policy(SET CMP0141 NEW)
9+
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
10+
endif()
11+
12+
project ("relaxed_concurrent_fifo")
13+
14+
# Include sub-projects.
15+
add_subdirectory ("relaxed_concurrent_fifo")

CMakePresets.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"version": 3,
3+
"configurePresets": [
4+
{
5+
"name": "windows-base",
6+
"hidden": true,
7+
"generator": "Ninja",
8+
"binaryDir": "${sourceDir}/out/build/${presetName}",
9+
"installDir": "${sourceDir}/out/install/${presetName}",
10+
"cacheVariables": {
11+
"CMAKE_C_COMPILER": "cl.exe",
12+
"CMAKE_CXX_COMPILER": "cl.exe"
13+
},
14+
"condition": {
15+
"type": "equals",
16+
"lhs": "${hostSystemName}",
17+
"rhs": "Windows"
18+
}
19+
},
20+
{
21+
"name": "x64-debug",
22+
"displayName": "x64 Debug",
23+
"inherits": "windows-base",
24+
"architecture": {
25+
"value": "x64",
26+
"strategy": "external"
27+
},
28+
"cacheVariables": {
29+
"CMAKE_BUILD_TYPE": "Debug"
30+
}
31+
},
32+
{
33+
"name": "x64-release",
34+
"displayName": "x64 Release",
35+
"inherits": "x64-debug",
36+
"cacheVariables": {
37+
"CMAKE_BUILD_TYPE": "Release"
38+
}
39+
},
40+
{
41+
"name": "x86-debug",
42+
"displayName": "x86 Debug",
43+
"inherits": "windows-base",
44+
"architecture": {
45+
"value": "x86",
46+
"strategy": "external"
47+
},
48+
"cacheVariables": {
49+
"CMAKE_BUILD_TYPE": "Debug"
50+
}
51+
},
52+
{
53+
"name": "x86-release",
54+
"displayName": "x86 Release",
55+
"inherits": "x86-debug",
56+
"cacheVariables": {
57+
"CMAKE_BUILD_TYPE": "Release"
58+
}
59+
}
60+
]
61+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# CMakeList.txt : CMake project for relaxed_concurrent_fifo, include source and define
2+
# project specific logic here.
3+
#
4+
5+
# Add source to this project's executable.
6+
add_executable (relaxed_concurrent_fifo "relaxed_concurrent_fifo.cpp" "relaxed_concurrent_fifo.h")
7+
8+
if (CMAKE_VERSION VERSION_GREATER 3.12)
9+
set_property(TARGET relaxed_concurrent_fifo PROPERTY CXX_STANDARD 20)
10+
endif()
11+
12+
# TODO: Add tests and install targets if needed.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "relaxed_concurrent_fifo.h"
2+
3+
#include <ranges>
4+
#include <cassert>
5+
6+
static constexpr int COUNT = 512;
7+
8+
template <template <typename, size_t> typename T>
9+
void test_full_capacity() {
10+
T<int, COUNT + 1> buf;
11+
for (int i : std::views::iota(0, COUNT)) {
12+
buf.push(i);
13+
}
14+
for (int i : std::views::iota(0, COUNT)) {
15+
assert(buf.pop() == i);
16+
}
17+
}
18+
19+
template <template <typename, size_t> typename T>
20+
void test_single_element() {
21+
T<int, COUNT + 1> buf;
22+
for (int i : std::views::iota(0, COUNT * 10)) {
23+
buf.push(i);
24+
assert(buf.pop() == i);
25+
}
26+
}
27+
28+
template <template <typename, size_t> typename T>
29+
void test_empty_pop() {
30+
T<int, COUNT + 1> buf;
31+
assert(!buf.pop().has_value());
32+
buf.push(1);
33+
buf.pop();
34+
assert(!buf.pop().has_value());
35+
for (int i : std::views::iota(0, COUNT * 10)) {
36+
buf.push(i);
37+
buf.pop();
38+
}
39+
assert(!buf.pop().has_value());
40+
}
41+
42+
template <template <typename, size_t> typename T>
43+
void test_all() {
44+
test_full_capacity<T>();
45+
test_single_element<T>();
46+
test_empty_pop<T>();
47+
}
48+
49+
int main() {
50+
test_all<circular_buffer>();
51+
return 0;
52+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef RELAXED_CONCURRENT_FIFO_H_INCLUDED
2+
#define RELAXED_CONCURRENT_FIFO_H_INCLUDED
3+
4+
#include <utility>
5+
#include <optional>
6+
7+
// TODO: How should a full queue be handled?
8+
// Currently popping from a full queue is not possible.
9+
template <typename T, size_t SIZE>
10+
class circular_buffer {
11+
private:
12+
T buffer[SIZE];
13+
14+
size_t head = 0;
15+
size_t tail = 0;
16+
17+
inline size_t next(size_t curr) {
18+
return (curr + 1) % SIZE;
19+
}
20+
21+
public:
22+
void push(T t) {
23+
head = next(head);
24+
buffer[head] = std::move(t);
25+
}
26+
27+
std::optional<T> pop() {
28+
if (head == tail) {
29+
return std::nullopt;
30+
}
31+
32+
tail = next(tail);
33+
return std::move(buffer[tail]);
34+
}
35+
};
36+
37+
#endif // RELAXED_CONCURRENT_FIFO_H_INCLUDED

0 commit comments

Comments
 (0)