-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
209 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2024 Sapphire's Suite. All Rights Reserved. | ||
|
||
namespace SA | ||
{ | ||
template <typename T> | ||
RingBuffer<T>::RingBuffer(uint32_t _capacity) : | ||
mCapacity { _capacity } | ||
{ | ||
mHandleBuffer = static_cast<T*>(::operator new(_capacity * sizeof(T))); | ||
mPushCompleted = new std::atomic<bool>[_capacity]; | ||
} | ||
|
||
template <typename T> | ||
RingBuffer<T>::~RingBuffer() | ||
{ | ||
::operator delete(mHandleBuffer); | ||
delete[] mPushCompleted; | ||
} | ||
|
||
|
||
template <typename T> | ||
void RingBuffer<T>::Push(T&& _obj) | ||
{ | ||
while (IsFull()) | ||
std::this_thread::yield(); | ||
|
||
const uint32_t index = mPushCursor = (mPushCursor + 1) % Capacity(); | ||
|
||
new(&mHandleBuffer[index]) T(std::move(_obj)); | ||
|
||
#if SA_DEBUG || 1 | ||
|
||
if (mPushCompleted[index]) | ||
{ | ||
throw std::string("DATA RACE"); | ||
} | ||
|
||
#endif | ||
|
||
mPushCompleted[index] = true; | ||
} | ||
|
||
template <typename T> | ||
T RingBuffer<T>::Pop() | ||
{ | ||
//while (IsEmpty()) | ||
// std::this_thread::yield(); | ||
|
||
const uint32_t index = mPopCursor = (mPopCursor + 1) % Capacity(); | ||
|
||
while(!mPushCompleted[index]) | ||
std::this_thread::yield(); | ||
|
||
// Reset for next use. | ||
mPushCompleted[index] = false; | ||
|
||
T output = std::move(mHandleBuffer[index]); | ||
|
||
mHandleBuffer[index].~T(); | ||
|
||
return std::move(output); | ||
} | ||
|
||
|
||
template <typename T> | ||
uint32_t RingBuffer<T>::Size() const noexcept | ||
{ | ||
return mPushCursor - mPopCursor; | ||
} | ||
|
||
template <typename T> | ||
uint32_t RingBuffer<T>::Capacity() const noexcept | ||
{ | ||
return mCapacity; | ||
} | ||
|
||
|
||
template <typename T> | ||
bool RingBuffer<T>::IsEmpty() const noexcept | ||
{ | ||
return Size() == 0; | ||
} | ||
|
||
template <typename T> | ||
bool RingBuffer<T>::IsFull() const noexcept | ||
{ | ||
return Size() == Capacity(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2024 Sapphire's Suite. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#ifndef SAPPHIRE_LOGGER_RING_BUFFER_GUARD | ||
#define SAPPHIRE_LOGGER_RING_BUFFER_GUARD | ||
|
||
#include <atomic> | ||
#include <thread> | ||
|
||
namespace SA | ||
{ | ||
template <typename T> | ||
class RingBuffer | ||
{ | ||
T* mHandleBuffer = nullptr; | ||
std::atomic<bool>* mPushCompleted = nullptr; | ||
|
||
const uint32_t mCapacity = 0; | ||
|
||
std::atomic<uint32_t> mPushCursor = 0; | ||
std::atomic<uint32_t> mPopCursor = 0; | ||
|
||
public: | ||
RingBuffer(uint32_t _capacity = 32); | ||
~RingBuffer(); | ||
|
||
void Push(T&& _obj); | ||
T Pop(); | ||
|
||
uint32_t Size() const noexcept; | ||
uint32_t Capacity() const noexcept; | ||
|
||
bool IsEmpty() const noexcept; | ||
bool IsFull() const noexcept; | ||
}; | ||
} | ||
|
||
#include "RindBuffer.inl" | ||
|
||
#endif // SAPPHIRE_LOGGER_RING_BUFFER_GUARD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
# Entrypoints | ||
|
||
add_subdirectory(Prototype) | ||
add_subdirectory(PrototypeMT) | ||
add_subdirectory(UnitTests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright (c) 2023 Sapphire's Suite. All Rights Reserved. | ||
|
||
|
||
|
||
# Project | ||
|
||
project(SA_LoggerProtoMT) | ||
|
||
|
||
|
||
# Executable | ||
|
||
## Add executable target. | ||
add_executable(SA_LoggerProtoMT "main.cpp") | ||
|
||
|
||
|
||
# Dependencies | ||
|
||
### Add library dependencies. | ||
target_link_libraries(SA_LoggerProtoMT PRIVATE SA_Logger) | ||
|
||
|
||
|
||
# Tests | ||
|
||
add_test(NAME CSA_LoggerProtoMT COMMAND SA_LoggerProto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2023 Sapphire's Suite. All Rights Reserved. | ||
|
||
#include <iostream> | ||
|
||
#include <SA/Collections/Debug> | ||
|
||
void LoggingThread() | ||
{ | ||
for (int i = 0; i < 1000; ++i) | ||
SA_LOG(("HELLO %1", i)); | ||
} | ||
|
||
int main() | ||
{ | ||
SA::Debug::InitDefaultLoggerThread; | ||
|
||
std::thread t1(LoggingThread); | ||
std::thread t2(LoggingThread); | ||
std::thread t3(LoggingThread); | ||
std::thread t4(LoggingThread); | ||
|
||
if (t1.joinable()) | ||
t1.join(); | ||
|
||
if (t2.joinable()) | ||
t2.join(); | ||
|
||
if (t3.joinable()) | ||
t3.join(); | ||
|
||
if (t4.joinable()) | ||
t4.join(); | ||
|
||
return 0; | ||
} |