diff --git a/.gitmodules b/.gitmodules index 01f3ed5..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "cpp_impl/RingBuffer"] - path = cpp_impl/RingBuffer - url = https://github.com/Stephen-Seo/RingBuffer.git diff --git a/cpp_impl/CMakeLists.txt b/cpp_impl/CMakeLists.txt index cd671af..bf5b770 100644 --- a/cpp_impl/CMakeLists.txt +++ b/cpp_impl/CMakeLists.txt @@ -3,12 +3,6 @@ project(UDPConnection) set(UDPConnection_VERSION 1.0) -if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/RingBuffer/src) - message(FATAL_ERROR "RingBuffer is missing!\nPlease update the \ -RingBuffer submodule by running 'git submodule init' and 'git submodule \ -update'!") -endif() - set(UDPConnection_SOURCES src/UDPConnection.cpp ) @@ -46,16 +40,12 @@ target_link_libraries(UDPConnection PUBLIC ${LIBSODIUM_LIBRARIES}) target_include_directories(UDPConnection PUBLIC ${LIBSODIUM_INCLUDE_DIRS}) target_compile_options(UDPConnection PUBLIC ${LIBSODIUM_CFLAGS_OTHER}) -target_include_directories(UDPConnection PUBLIC - "${CMAKE_CURRENT_SOURCE_DIR}/RingBuffer/src") - if(CMAKE_BUILD_TYPE MATCHES "Debug") find_package(GTest QUIET) if(GTEST_FOUND) set(UDPC_UnitTest_SOURCES src/test/UDPC_UnitTest.cpp - src/test/TestTSQueue.cpp src/test/TestTSLQueue.cpp src/test/TestUDPC.cpp ) diff --git a/cpp_impl/RingBuffer b/cpp_impl/RingBuffer deleted file mode 160000 index 2873bfb..0000000 --- a/cpp_impl/RingBuffer +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2873bfb55467a9b236382b77db18408a661c4542 diff --git a/cpp_impl/src/TSQueue.hpp b/cpp_impl/src/TSQueue.hpp deleted file mode 100644 index 049916e..0000000 --- a/cpp_impl/src/TSQueue.hpp +++ /dev/null @@ -1,191 +0,0 @@ -#ifndef UDPC_THREADSAFE_QUEUE_HPP -#define UDPC_THREADSAFE_QUEUE_HPP - -#define UDPC_TSQUEUE_DEFAULT_CAPACITY 32 - -#include -#include -#include -#include - -#include - -template -class TSQueue { - public: - TSQueue(unsigned int capacity = UDPC_TSQUEUE_DEFAULT_CAPACITY); - ~TSQueue(); - - // disable copy - TSQueue(const TSQueue &other) = delete; - TSQueue &operator=(const TSQueue &other) = delete; - // enable move - TSQueue(TSQueue &&other); - TSQueue &operator=(TSQueue &&other); - - bool push(const T &data); - std::optional top(); - bool pop(); - std::optional top_and_pop(); - std::optional top_and_pop_and_rsize(unsigned int *rsize); - void clear(); - /* - * status == - * 0 - success - * 1 - success, but previous size was reduced - */ - void changeCapacity(unsigned int newCapacity, unsigned int *status); - unsigned int size(); - unsigned int capacity(); - unsigned int remaining_capacity(); - bool empty(); - bool full(); - - private: - std::mutex mutex; - RB::RingBuffer rb; -}; - -template -TSQueue::TSQueue(unsigned int capacity) : -mutex(), -rb(capacity) -{ - rb.setResizePolicy(false); -} - -template -TSQueue::TSQueue(TSQueue &&other) : -TSQueue::TSQueue(other.rb.getCapacity()) -{ - std::lock_guard lock(other.mutex); - for(unsigned int i = 0; i < other.rb.getSize(); ++i) { - rb.push(other.rb[i]); - } -} - -template -TSQueue& TSQueue::operator =(TSQueue &&other) -{ - std::scoped_lock lock(other.mutex, mutex); - rb.resize(0); - rb.changeCapacity(other.rb.getCapacity()); - for(unsigned int i = 0; i < other.rb.getSize(); ++i) { - rb.push(other.rb[i]); - } -} - -template -TSQueue::~TSQueue() -{} - -template -bool TSQueue::push(const T &data) { - std::lock_guard lock(mutex); - if(rb.getSize() == rb.getCapacity()) { - return false; - } - rb.push(data); - return true; -} - -template -std::optional TSQueue::top() { - std::lock_guard lock(mutex); - std::optional value = std::nullopt; - if(!rb.empty()) { - value = rb.top(); - } - return value; -} - -template -bool TSQueue::pop() { - std::lock_guard lock(mutex); - if(rb.empty()) { - return false; - } - rb.pop(); - return true; -} - -template -std::optional TSQueue::top_and_pop() { - std::lock_guard lock(mutex); - std::optional value = std::nullopt; - if(!rb.empty()) { - value = rb.top(); - rb.pop(); - } - return value; -} - -template -std::optional TSQueue::top_and_pop_and_rsize(unsigned int *rsize) { - std::lock_guard lock(mutex); - std::optional value = std::nullopt; - if(!rb.empty()) { - value = rb.top(); - rb.pop(); - } - if(rsize) { - *rsize = rb.getSize(); - } - return value; -} - -template -void TSQueue::clear() { - std::lock_guard lock(mutex); - rb.resize(0); -} - -template -void TSQueue::changeCapacity(unsigned int newCapacity, unsigned int *status) { - std::lock_guard lock(mutex); - if(status) { - if(rb.getSize() < newCapacity) { - *status = 1; - } else { - *status = 0; - } - } - rb.changeCapacity(newCapacity); -} - -template -unsigned int TSQueue::size() { - std::lock_guard lock(mutex); - unsigned int size = rb.getSize(); - return size; -} - -template -unsigned int TSQueue::capacity() { - std::lock_guard lock(mutex); - unsigned int capacity = rb.getCapacity(); - return capacity; -} - -template -unsigned int TSQueue::remaining_capacity() { - std::lock_guard lock(mutex); - unsigned int remaining = rb.getCapacity() - rb.getSize(); - return remaining; -} - -template -bool TSQueue::empty() { - // No lock required, since this is calling size() that uses a lock - unsigned int size = this->size(); - return size == 0; -} - -template -bool TSQueue::full() { - // No lock required, calling remaining_capacity() that uses a lock - unsigned int remaining = remaining_capacity(); - return remaining == 0; -} - -#endif diff --git a/cpp_impl/src/UDPC_Defines.hpp b/cpp_impl/src/UDPC_Defines.hpp index a4d5d5e..4ef289e 100644 --- a/cpp_impl/src/UDPC_Defines.hpp +++ b/cpp_impl/src/UDPC_Defines.hpp @@ -32,7 +32,6 @@ #include #include -#include "TSQueue.hpp" #include "TSLQueue.hpp" #include "UDPConnection.h" diff --git a/cpp_impl/src/test/TestTSQueue.cpp b/cpp_impl/src/test/TestTSQueue.cpp deleted file mode 100644 index 9721be4..0000000 --- a/cpp_impl/src/test/TestTSQueue.cpp +++ /dev/null @@ -1,145 +0,0 @@ -#include - -#include -#include - -#include "TSQueue.hpp" - -TEST(TSQueue, Usage) -{ - TSQueue q(4); - int temp = 100; - - EXPECT_EQ(q.size(), 0); - EXPECT_FALSE(q.pop()); - - EXPECT_TRUE(q.push(temp)); - EXPECT_EQ(q.size(), 1); - - // { 100 } - - temp = 200; - EXPECT_TRUE(q.push(temp)); - EXPECT_EQ(q.size(), 2); - EXPECT_EQ(100, q.top()); - - // { 100, 200 } - - temp = 300; - EXPECT_TRUE(q.push(temp)); - EXPECT_EQ(q.size(), 3); - - // { 100, 200, 300 } - - temp = 400; - EXPECT_TRUE(q.push(temp)); - EXPECT_EQ(q.size(), 4); - - // { 100, 200, 300, 400 } - - temp = 500; - EXPECT_FALSE(q.push(temp)); - EXPECT_EQ(q.size(), 4); - - EXPECT_EQ(100, q.top()); - - EXPECT_TRUE(q.pop()); - EXPECT_EQ(q.size(), 3); - - // { 200, 300, 400 } - - EXPECT_EQ(200, q.top()); - - temp = 1; - EXPECT_TRUE(q.push(temp)); - EXPECT_EQ(q.size(), 4); - - // { 200, 300, 400, 1 } - - EXPECT_EQ(200, q.top()); - - temp = 2; - EXPECT_FALSE(q.push(temp)); - EXPECT_EQ(q.size(), 4); - - q.changeCapacity(8, nullptr); - EXPECT_EQ(q.size(), 4); - - temp = 10; - EXPECT_TRUE(q.push(temp)); - EXPECT_EQ(q.size(), 5); - - // { 200, 300, 400, 1, 10 } - - EXPECT_EQ(200, q.top()); - - EXPECT_TRUE(q.pop()); - EXPECT_EQ(q.size(), 4); - - // { 300, 400, 1, 10 } - - EXPECT_EQ(300, q.top()); - - EXPECT_TRUE(q.pop()); - EXPECT_EQ(q.size(), 3); - - // { 400, 1, 10 } - - EXPECT_EQ(400, q.top()); - - q.changeCapacity(1, nullptr); - - // { 10 } - - EXPECT_EQ(q.size(), 1); - - EXPECT_EQ(10, q.top()); - - EXPECT_TRUE(q.pop()); - - // { } - - EXPECT_FALSE(q.pop()); - EXPECT_EQ(0, q.size()); -} - -TEST(TSQueue, Concurrent) -{ - TSQueue q(4); - - auto a0 = std::async(std::launch::async, [&q] () {int i = 0; return q.push(i); }); - auto a1 = std::async(std::launch::async, [&q] () {int i = 1; return q.push(i); }); - auto a2 = std::async(std::launch::async, [&q] () {int i = 2; return q.push(i); }); - auto a3 = std::async(std::launch::async, [&q] () {int i = 3; return q.push(i); }); - auto a4 = std::async(std::launch::async, [&q] () {int i = 4; return q.push(i); }); - - bool results[] = { - a0.get(), - a1.get(), - a2.get(), - a3.get(), - a4.get() - }; - - int insertCount = 0; - for(int i = 0; i < 5; ++i) { - if(results[i]) { - ++insertCount; - } - } - - EXPECT_EQ(insertCount, 4); - EXPECT_EQ(q.size(), 4); - - int top; - for(int i = 0; i < 4; ++i) { - top = q.top().value(); - EXPECT_TRUE(q.pop()); - EXPECT_EQ(q.size(), 3 - i); - printf("%d ", top); - } - printf("\n"); - - EXPECT_FALSE(q.pop()); - EXPECT_EQ(q.size(), 0); -}