mirror of
https://github.com/eried/portapack-mayhem.git
synced 2024-12-24 23:09:26 -05:00
Wrap MessageQueue.push() with mutex.
This addresses issue #61, occasional FIFO/data corruption. With the mutex, any thread on one core can write to the FIFO. But still, only one thread on one core should read from the FIFO.
This commit is contained in:
parent
7937ea7327
commit
035ec84f04
@ -30,9 +30,15 @@
|
|||||||
#include "lpc43xx_cpp.hpp"
|
#include "lpc43xx_cpp.hpp"
|
||||||
using namespace lpc43xx;
|
using namespace lpc43xx;
|
||||||
|
|
||||||
|
#include <ch.h>
|
||||||
|
|
||||||
template<size_t K>
|
template<size_t K>
|
||||||
class MessageQueue {
|
class MessageQueue {
|
||||||
public:
|
public:
|
||||||
|
MessageQueue() {
|
||||||
|
chMtxInit(&mutex_write);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool push(const T& message) {
|
bool push(const T& message) {
|
||||||
static_assert(sizeof(T) <= Message::MAX_SIZE, "Message::MAX_SIZE too small for message type");
|
static_assert(sizeof(T) <= Message::MAX_SIZE, "Message::MAX_SIZE too small for message type");
|
||||||
@ -55,9 +61,13 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
FIFO<uint8_t, K> fifo;
|
FIFO<uint8_t, K> fifo;
|
||||||
|
Mutex mutex_write;
|
||||||
|
|
||||||
bool push(const void* const buf, const size_t len) {
|
bool push(const void* const buf, const size_t len) {
|
||||||
|
chMtxLock(&mutex_write);
|
||||||
const auto result = fifo.in_r(buf, len);
|
const auto result = fifo.in_r(buf, len);
|
||||||
|
chMtxUnlock();
|
||||||
|
|
||||||
const bool success = (result == len);
|
const bool success = (result == len);
|
||||||
if( success ) {
|
if( success ) {
|
||||||
signal();
|
signal();
|
||||||
|
Loading…
Reference in New Issue
Block a user