Added Dispatcher and fix sim (#54)
- Add dispatcher mechanism (a queue for function calls) and tests - Added tests for MessageQueue - Fix FreeRTOS config for simulator - Explicit dependencies for touch-related libs, because minor version changes caused broken builds on CI.
This commit is contained in:
committed by
GitHub
parent
69a0c01686
commit
b659d5b940
@@ -0,0 +1,48 @@
|
||||
#include "dispatcher.h"
|
||||
|
||||
#include "tactility_core.h"
|
||||
|
||||
Dispatcher* tt_dispatcher_alloc(uint32_t message_count) {
|
||||
Dispatcher* dispatcher = malloc(sizeof(Dispatcher));
|
||||
*dispatcher = (Dispatcher) {
|
||||
.queue = tt_message_queue_alloc(message_count, sizeof(DispatcherMessage)),
|
||||
.mutex = tt_mutex_alloc(MutexTypeNormal),
|
||||
.buffer = {
|
||||
.callback = NULL,
|
||||
.context = NULL
|
||||
}
|
||||
};
|
||||
return dispatcher;
|
||||
}
|
||||
|
||||
void tt_dispatcher_free(Dispatcher* dispatcher) {
|
||||
tt_mutex_acquire(dispatcher->mutex, TtWaitForever);
|
||||
tt_message_queue_reset(dispatcher->queue);
|
||||
tt_message_queue_free(dispatcher->queue);
|
||||
tt_mutex_release(dispatcher->mutex);
|
||||
tt_mutex_free(dispatcher->mutex);
|
||||
free(dispatcher);
|
||||
}
|
||||
|
||||
void tt_dispatcher_dispatch(Dispatcher* dispatcher, Callback callback, void* context) {
|
||||
DispatcherMessage message = {
|
||||
.callback = callback,
|
||||
.context = context
|
||||
};
|
||||
tt_mutex_acquire(dispatcher->mutex, TtWaitForever);
|
||||
tt_message_queue_put(dispatcher->queue, &message, TtWaitForever);
|
||||
tt_mutex_release(dispatcher->mutex);
|
||||
}
|
||||
|
||||
bool tt_dispatcher_consume(Dispatcher* dispatcher, uint32_t timeout_ticks) {
|
||||
tt_mutex_acquire(dispatcher->mutex, TtWaitForever);
|
||||
if (tt_message_queue_get(dispatcher->queue, &(dispatcher->buffer), timeout_ticks) == TtStatusOk) {
|
||||
DispatcherMessage* message = &(dispatcher->buffer);
|
||||
message->callback(message->context);
|
||||
tt_mutex_release(dispatcher->mutex);
|
||||
return true;
|
||||
} else {
|
||||
tt_mutex_release(dispatcher->mutex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @file message_queue.h
|
||||
*
|
||||
* Dispatcher is a thread-safe message queue implementation for callbacks.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "message_queue.h"
|
||||
#include "mutex.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void (*Callback)(void* data);
|
||||
|
||||
typedef struct {
|
||||
Callback callback;
|
||||
void* context;
|
||||
} DispatcherMessage;
|
||||
|
||||
typedef struct {
|
||||
MessageQueue* queue;
|
||||
Mutex* mutex;
|
||||
DispatcherMessage buffer; // Buffer for consuming a message
|
||||
} Dispatcher;
|
||||
|
||||
Dispatcher* tt_dispatcher_alloc(uint32_t message_count);
|
||||
void tt_dispatcher_free(Dispatcher* dispatcher);
|
||||
void tt_dispatcher_dispatch(Dispatcher* dispatcher, Callback callback, void* context);
|
||||
bool tt_dispatcher_consume(Dispatcher* dispatcher, uint32_t timeout_ticks);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,6 +1,9 @@
|
||||
/**
|
||||
* @file message_queue.h
|
||||
* MessageQueue
|
||||
*
|
||||
* MessageQueue is a wrapper for FreeRTOS xQueue functionality.
|
||||
* There is no additional thread-safety on top of the xQueue functionality,
|
||||
* so make sure you create a lock if needed.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
|
||||
Reference in New Issue
Block a user