Skip to content

Commit

Permalink
dir and file movement
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentD committed Dec 19, 2024
1 parent 28dc5da commit 143e2a8
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build/logger",
"program": "${workspaceRoot}/build/funcaller",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceRoot}/build",
Expand Down
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 19 additions & 0 deletions example/Bar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "IFoo.h"
#include <memory>

class Bar
{
public:
explicit Bar(std::shared_ptr<IFoo> foo)
: foo(std::move(foo))
{}

void bar1() const { foo->foo1(); }
void bar2(const int a) const { foo->foo2(a); }
void bar3(std::shared_ptr<int> ptr) const { foo->foo3(std::move(ptr)); }

private:
std::shared_ptr<IFoo> foo;
};
File renamed without changes.
2 changes: 1 addition & 1 deletion src/FooContextSwitch.h → example/FooContextSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "ContextSwitch.h"
#include "IFoo.h"

class FooContextSwitch : public funcall::ContextSwitch<IFoo>
class FooContextSwitch final : public funcall::ContextSwitch<IFoo>
{
public:
void foo1() override QUEUE(foo1);
Expand Down
File renamed without changes.
20 changes: 3 additions & 17 deletions src/main.cpp → example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
#include "Bar.h"
#include "Foo.h"
#include "FooContextSwitch.h"
#include "ThreadedFunctionQueue.h"
#include "ThreadFunctionQueue.h"

#include <csignal>

void signalHandler(int signal);
std::jthread mainThread;

class Bar
{
public:
explicit Bar(std::shared_ptr<IFoo> foo)
: foo(std::move(foo))
{}

void bar1() const { foo->foo1(); }
void bar2(const int a) const { foo->foo2(a); }
void bar3(std::shared_ptr<int> ptr) const { foo->foo3(std::move(ptr)); }

private:
std::shared_ptr<IFoo> foo;
};

int main()
{
// Set up signal handlers
std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler);

const auto MainQueue = std::make_shared<funcall::ThreadedFunctionQueue>(
const auto MainQueue = std::make_shared<funcall::ThreadFunctionQueue>(
[](std::string &&message) { printf("%s\n", message.c_str()); });
MainQueue->start();

Expand Down
14 changes: 7 additions & 7 deletions src/ThreadedFunctionQueue.cpp → src/ThreadFunctionQueue.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -15,7 +15,7 @@ std::thread::id ThreadedFunctionQueue::start() noexcept

// Create a new context and start the thread
ctx = std::make_shared<Context>();
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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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> context,
std::function<void(std::string &&)> _logger) noexcept
{
Expand Down
18 changes: 9 additions & 9 deletions src/ThreadedFunctionQueue.h → src/ThreadFunctionQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>
#include <queue>
#include <thread>

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
Expand All @@ -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<void(std::string &&)> mLogger;
Expand Down
Empty file.

0 comments on commit 143e2a8

Please sign in to comment.