diff --git a/.vscode/launch.json b/.vscode/launch.json index b8bea12..67f4738 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "Debug", "type": "cppdbg", "request": "launch", - "program": "${workspaceRoot}/build/logger", + "program": "${workspaceRoot}/build/funcaller", "args": [], "stopAtEntry": true, "cwd": "${workspaceRoot}/build", diff --git a/CMakeLists.txt b/CMakeLists.txt index eb5de86..5e19a13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,15 @@ set(CMAKE_CXX_STANDARD 20) add_executable(funcall src/ContextSwitch.h - src/Foo.h - src/FooContextSwitch.h - src/IFoo.h src/IFunctionQueue.h - src/main.cpp - src/ThreadedFunctionQueue.cpp - src/ThreadedFunctionQueue.h + src/ThreadFunctionQueue.cpp + src/ThreadFunctionQueue.h + test/ThreadFunctionQueueTest.cpp + example/Foo.h + example/Bar.h + example/FooContextSwitch.h + example/IFoo.h + example/main.cpp ) + +target_include_directories(funcall PRIVATE src example) diff --git a/example/Bar.h b/example/Bar.h new file mode 100644 index 0000000..3f36756 --- /dev/null +++ b/example/Bar.h @@ -0,0 +1,19 @@ +#pragma once + +#include "IFoo.h" +#include + +class Bar +{ +public: + explicit Bar(std::shared_ptr foo) + : foo(std::move(foo)) + {} + + void bar1() const { foo->foo1(); } + void bar2(const int a) const { foo->foo2(a); } + void bar3(std::shared_ptr ptr) const { foo->foo3(std::move(ptr)); } + +private: + std::shared_ptr foo; +}; diff --git a/src/Foo.h b/example/Foo.h similarity index 100% rename from src/Foo.h rename to example/Foo.h diff --git a/src/FooContextSwitch.h b/example/FooContextSwitch.h similarity index 76% rename from src/FooContextSwitch.h rename to example/FooContextSwitch.h index b74ac0a..f521c7f 100644 --- a/src/FooContextSwitch.h +++ b/example/FooContextSwitch.h @@ -3,7 +3,7 @@ #include "ContextSwitch.h" #include "IFoo.h" -class FooContextSwitch : public funcall::ContextSwitch +class FooContextSwitch final : public funcall::ContextSwitch { public: void foo1() override QUEUE(foo1); diff --git a/src/IFoo.h b/example/IFoo.h similarity index 100% rename from src/IFoo.h rename to example/IFoo.h diff --git a/src/main.cpp b/example/main.cpp similarity index 74% rename from src/main.cpp rename to example/main.cpp index 487ecb5..f233a8e 100644 --- a/src/main.cpp +++ b/example/main.cpp @@ -1,34 +1,20 @@ +#include "Bar.h" #include "Foo.h" #include "FooContextSwitch.h" -#include "ThreadedFunctionQueue.h" +#include "ThreadFunctionQueue.h" #include void signalHandler(int signal); std::jthread mainThread; -class Bar -{ -public: - explicit Bar(std::shared_ptr foo) - : foo(std::move(foo)) - {} - - void bar1() const { foo->foo1(); } - void bar2(const int a) const { foo->foo2(a); } - void bar3(std::shared_ptr ptr) const { foo->foo3(std::move(ptr)); } - -private: - std::shared_ptr foo; -}; - int main() { // Set up signal handlers std::signal(SIGINT, signalHandler); std::signal(SIGTERM, signalHandler); - const auto MainQueue = std::make_shared( + const auto MainQueue = std::make_shared( [](std::string &&message) { printf("%s\n", message.c_str()); }); MainQueue->start(); diff --git a/src/ThreadedFunctionQueue.cpp b/src/ThreadFunctionQueue.cpp similarity index 90% rename from src/ThreadedFunctionQueue.cpp rename to src/ThreadFunctionQueue.cpp index d2efdcf..2297375 100644 --- a/src/ThreadedFunctionQueue.cpp +++ b/src/ThreadFunctionQueue.cpp @@ -1,8 +1,8 @@ -#include "ThreadedFunctionQueue.h" +#include "ThreadFunctionQueue.h" using namespace funcall; -std::thread::id ThreadedFunctionQueue::start() noexcept +std::thread::id ThreadFunctionQueue::start() noexcept { // Prevent multiple threads from starting/stopping the queue at the same time std::scoped_lock lock(mMutex); @@ -15,7 +15,7 @@ std::thread::id ThreadedFunctionQueue::start() noexcept // Create a new context and start the thread ctx = std::make_shared(); - ctx->thread = std::thread(&ThreadedFunctionQueue::processQueue, ctx, mLogger); + ctx->thread = std::thread(&ThreadFunctionQueue::processQueue, ctx, mLogger); // Wait for the thread to start. // If it starts, store the context and return the thread ID @@ -40,7 +40,7 @@ std::thread::id ThreadedFunctionQueue::start() noexcept return {}; } -bool ThreadedFunctionQueue::stop() noexcept +bool ThreadFunctionQueue::stop() noexcept { // Prevent multiple threads from starting/stopping the queue at the same time std::scoped_lock lock(mMutex); @@ -78,7 +78,7 @@ bool ThreadedFunctionQueue::stop() noexcept return false; } -void ThreadedFunctionQueue::add(Function &&function) noexcept +void ThreadFunctionQueue::add(Function &&function) noexcept { // Note: if the stop occurred during this call, the last // context will be deleted at the end of this function @@ -95,7 +95,7 @@ void ThreadedFunctionQueue::add(Function &&function) noexcept ctx->condition.notify_one(); } -void ThreadedFunctionQueue::waitFor(const std::atomic_bool &var, const bool value) noexcept +void ThreadFunctionQueue::waitFor(const std::atomic_bool &var, const bool value) noexcept { // Wait for the variable to reach the desired value before timeout const auto start = std::chrono::system_clock::now(); @@ -106,7 +106,7 @@ void ThreadedFunctionQueue::waitFor(const std::atomic_bool &var, const bool valu } } -void ThreadedFunctionQueue::processQueue( +void ThreadFunctionQueue::processQueue( std::shared_ptr context, std::function _logger) noexcept { diff --git a/src/ThreadedFunctionQueue.h b/src/ThreadFunctionQueue.h similarity index 79% rename from src/ThreadedFunctionQueue.h rename to src/ThreadFunctionQueue.h index 05714e9..26d24ff 100644 --- a/src/ThreadedFunctionQueue.h +++ b/src/ThreadFunctionQueue.h @@ -6,26 +6,26 @@ #include #include #include -#include #include +#include namespace funcall { // A thread-safe object that queues and execute functions in a separate thread. -class ThreadedFunctionQueue final : public IFunctionQueue +class ThreadFunctionQueue final : public IFunctionQueue { public: - ThreadedFunctionQueue() noexcept + ThreadFunctionQueue() noexcept : mLogger(nullptr) {} // Constructor with a static logger function - explicit ThreadedFunctionQueue(void (*log)(std::string &&)) noexcept + explicit ThreadFunctionQueue(void (*log)(std::string &&)) noexcept : mLogger(log) {} // Destructor, will stop the thread if running - ~ThreadedFunctionQueue() noexcept override { stop(); } + ~ThreadFunctionQueue() noexcept override { stop(); } // Start the thread and return the thread id, // or return an empty id if the thread is already running @@ -40,10 +40,10 @@ class ThreadedFunctionQueue final : public IFunctionQueue void add(Function &&function) noexcept override; // Copy and move constructors and assignment operators are not allowed - ThreadedFunctionQueue(const ThreadedFunctionQueue &) = delete; - ThreadedFunctionQueue &operator=(const ThreadedFunctionQueue &) = delete; - ThreadedFunctionQueue(ThreadedFunctionQueue &&) = delete; - ThreadedFunctionQueue &operator=(ThreadedFunctionQueue &&) = delete; + ThreadFunctionQueue(const ThreadFunctionQueue &) = delete; + ThreadFunctionQueue &operator=(const ThreadFunctionQueue &) = delete; + ThreadFunctionQueue(ThreadFunctionQueue &&) = delete; + ThreadFunctionQueue &operator=(ThreadFunctionQueue &&) = delete; private: std::function mLogger; diff --git a/test/ThreadFunctionQueueTest.cpp b/test/ThreadFunctionQueueTest.cpp new file mode 100644 index 0000000..e69de29