From ce873f53390071c378654db2cfd276558b3f7a36 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sat, 5 Dec 2020 21:37:13 +0800 Subject: [PATCH] =?UTF-8?q?CommandQueue:=20read/write=20=E5=8A=A0=E9=94=81?= =?UTF-8?q?=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/flutter/command/command_queue.cpp | 5 +++++ src/ui/flutter/command/command_queue.h | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/ui/flutter/command/command_queue.cpp b/src/ui/flutter/command/command_queue.cpp index 572ddd18..5a1f1a8d 100644 --- a/src/ui/flutter/command/command_queue.cpp +++ b/src/ui/flutter/command/command_queue.cpp @@ -19,6 +19,7 @@ #include #include "command_queue.h" +#include "base.h" CommandQueue::CommandQueue() { @@ -32,6 +33,8 @@ CommandQueue::CommandQueue() bool CommandQueue::write(const char *command) { + std::unique_lock lk(mutex); + if (strlen(commands[writeIndex]) != 0) { return false; } @@ -51,6 +54,8 @@ bool CommandQueue::write(const char *command) bool CommandQueue::read(char *dest) { + std::unique_lock lk(mutex); + if (readIndex == -1) { return false; } diff --git a/src/ui/flutter/command/command_queue.h b/src/ui/flutter/command/command_queue.h index 916a89d8..686d70d9 100644 --- a/src/ui/flutter/command/command_queue.h +++ b/src/ui/flutter/command/command_queue.h @@ -20,6 +20,8 @@ #ifndef COMMAND_QUEUE_H #define COMMAND_QUEUE_H +#include + class CommandQueue { enum @@ -31,6 +33,8 @@ class CommandQueue char commands[MAX_COMMAND_COUNT][COMMAND_LENGTH]; int readIndex, writeIndex; + std::mutex mutex; + public: CommandQueue();