Various services improved (#110)

This commit is contained in:
Ken Van Hoeylandt
2024-12-06 23:16:29 +01:00
committed by GitHub
parent 36bb25deba
commit d52fe52d96
32 changed files with 771 additions and 636 deletions
+19 -9
View File
@@ -7,29 +7,39 @@
#include "MessageQueue.h"
#include "Mutex.h"
#include "EventFlag.h"
#include <memory>
#include <queue>
namespace tt {
typedef void (*Callback)(void* data);
typedef void (*Callback)(std::shared_ptr<void> data);
class Dispatcher {
private:
typedef struct {
struct DispatcherMessage {
Callback callback;
void* context;
} DispatcherMessage;
std::shared_ptr<void> context; // Can't use unique_ptr with void, so we use shared_ptr
DispatcherMessage(Callback callback, std::shared_ptr<void> context) :
callback(callback),
context(std::move(context))
{}
~DispatcherMessage() = default;
};
MessageQueue queue;
Mutex mutex;
DispatcherMessage buffer; // Buffer for consuming a message
std::queue<std::shared_ptr<DispatcherMessage>> queue;
EventFlag eventFlag;
public:
explicit Dispatcher(size_t queueLimit = 8);
explicit Dispatcher();
~Dispatcher();
void dispatch(Callback callback, void* context);
bool consume(uint32_t timeout_ticks);
void dispatch(Callback callback, std::shared_ptr<void> context);
uint32_t consume(uint32_t timeout_ticks);
};
} // namespace