mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 19:03:16 +00:00
Improved the docs for the 3 main Tactility projects. I also fixed some inaccuracies and bugs in certain APIs as I went through the code.
65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
/**
|
|
* @file Dispatcher.h
|
|
*
|
|
* Dispatcher is a thread-safe code execution queue.
|
|
*/
|
|
#pragma once
|
|
|
|
#include "MessageQueue.h"
|
|
#include "Mutex.h"
|
|
#include "EventFlag.h"
|
|
#include <memory>
|
|
#include <queue>
|
|
|
|
namespace tt {
|
|
|
|
|
|
/**
|
|
* A thread-safe way to defer code execution.
|
|
* Generally, one task would dispatch the execution,
|
|
* while the other thread consumes and executes the work.
|
|
*/
|
|
class Dispatcher {
|
|
public:
|
|
|
|
typedef void (*Function)(std::shared_ptr<void> data);
|
|
|
|
private:
|
|
struct DispatcherMessage {
|
|
Function function;
|
|
std::shared_ptr<void> context; // Can't use unique_ptr with void, so we use shared_ptr
|
|
|
|
DispatcherMessage(Function function, std::shared_ptr<void> context) :
|
|
function(function),
|
|
context(std::move(context))
|
|
{}
|
|
|
|
~DispatcherMessage() = default;
|
|
};
|
|
|
|
Mutex mutex;
|
|
std::queue<std::shared_ptr<DispatcherMessage>> queue;
|
|
EventFlag eventFlag;
|
|
|
|
public:
|
|
|
|
explicit Dispatcher();
|
|
~Dispatcher();
|
|
|
|
/**
|
|
* Queue a function to be consumed elsewhere.
|
|
* @param[in] function the function to execute elsewhere
|
|
* @param[in] context the data to pass onto the function
|
|
*/
|
|
void dispatch(Function function, std::shared_ptr<void> context);
|
|
|
|
/**
|
|
* Consume a dispatched function (if any)
|
|
* @param[in] timeout the ticks to wait for a message
|
|
* @return the amount of messages that were consumed
|
|
*/
|
|
uint32_t consume(TickType_t timeout);
|
|
};
|
|
|
|
} // namespace
|