[FL-1880] Dialogs: fix semaphore lock (#722)

* Dialogs: fix queued message lock
* Dialogs: file select, fix queued message lock
* Dialogs: file select, free context after freeing callback holder
* Dialogs: better lock, separated wait and free

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG 2021-09-25 02:36:53 +10:00 committed by GitHub
parent d3b58f732f
commit acb8508249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 27 deletions

View File

@ -1,8 +1,18 @@
#pragma once #pragma once
#define API_LOCK_INIT_LOCKED() osSemaphoreNew(1, 0, NULL);
typedef osEventFlagsId_t FuriApiLock;
#define API_LOCK_EVENT (1U << 0)
#define API_LOCK_INIT_LOCKED() osEventFlagsNew(NULL);
#define API_LOCK_WAIT_UNTIL_UNLOCK(_lock) \
osEventFlagsWait(_lock, API_LOCK_EVENT, osFlagsWaitAny, osWaitForever);
#define API_LOCK_FREE(_lock) osEventFlagsDelete(_lock);
#define API_LOCK_UNLOCK(_lock) osEventFlagsSet(_lock, API_LOCK_EVENT);
#define API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(_lock) \ #define API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(_lock) \
osSemaphoreAcquire(_lock, osWaitForever); \ API_LOCK_WAIT_UNTIL_UNLOCK(_lock); \
osSemaphoreDelete(_lock); API_LOCK_FREE(_lock);
#define API_LOCK_UNLOCK(_lock) osSemaphoreRelease(_lock);

View File

@ -10,8 +10,8 @@ bool dialog_file_select_show(
char* result, char* result,
uint8_t result_size, uint8_t result_size,
const char* preselected_filename) { const char* preselected_filename) {
osSemaphoreId_t semaphore = API_LOCK_INIT_LOCKED(); FuriApiLock lock = API_LOCK_INIT_LOCKED();
furi_check(semaphore != NULL); furi_check(lock != NULL);
DialogsAppData data = { DialogsAppData data = {
.file_select = { .file_select = {
@ -24,14 +24,14 @@ bool dialog_file_select_show(
DialogsAppReturn return_data; DialogsAppReturn return_data;
DialogsAppMessage message = { DialogsAppMessage message = {
.semaphore = semaphore, .lock = lock,
.command = DialogsAppCommandFileOpen, .command = DialogsAppCommandFileOpen,
.data = &data, .data = &data,
.return_data = &return_data, .return_data = &return_data,
}; };
furi_check(osMessageQueuePut(context->message_queue, &message, 0, osWaitForever) == osOK); furi_check(osMessageQueuePut(context->message_queue, &message, 0, osWaitForever) == osOK);
API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(semaphore); API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(lock);
return return_data.bool_value; return return_data.bool_value;
} }
@ -39,8 +39,8 @@ bool dialog_file_select_show(
/****************** Message ******************/ /****************** Message ******************/
DialogMessageButton dialog_message_show(DialogsApp* context, const DialogMessage* dialog_message) { DialogMessageButton dialog_message_show(DialogsApp* context, const DialogMessage* dialog_message) {
osSemaphoreId_t semaphore = API_LOCK_INIT_LOCKED(); FuriApiLock lock = API_LOCK_INIT_LOCKED();
furi_check(semaphore != NULL); furi_check(lock != NULL);
DialogsAppData data = { DialogsAppData data = {
.dialog = { .dialog = {
@ -49,14 +49,14 @@ DialogMessageButton dialog_message_show(DialogsApp* context, const DialogMessage
DialogsAppReturn return_data; DialogsAppReturn return_data;
DialogsAppMessage message = { DialogsAppMessage message = {
.semaphore = semaphore, .lock = lock,
.command = DialogsAppCommandDialog, .command = DialogsAppCommandDialog,
.data = &data, .data = &data,
.return_data = &return_data, .return_data = &return_data,
}; };
furi_check(osMessageQueuePut(context->message_queue, &message, 0, osWaitForever) == osOK); furi_check(osMessageQueuePut(context->message_queue, &message, 0, osWaitForever) == osOK);
API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(semaphore); API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(lock);
return return_data.dialog_value; return return_data.dialog_value;
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <furi.h> #include <furi.h>
#include "dialogs-i.h" #include "dialogs-i.h"
#include "dialogs-api-lock.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -34,7 +35,7 @@ typedef enum {
} DialogsAppCommand; } DialogsAppCommand;
typedef struct { typedef struct {
osSemaphoreId_t semaphore; FuriApiLock lock;
DialogsAppCommand command; DialogsAppCommand command;
DialogsAppData* data; DialogsAppData* data;
DialogsAppReturn* return_data; DialogsAppReturn* return_data;

View File

@ -3,7 +3,7 @@
#include <gui/modules/file_select.h> #include <gui/modules/file_select.h>
typedef struct { typedef struct {
osSemaphoreId_t semaphore; FuriApiLock lock;
bool result; bool result;
} DialogsAppFileSelectContext; } DialogsAppFileSelectContext;
@ -11,14 +11,14 @@ static void dialogs_app_file_select_back_callback(void* context) {
furi_assert(context); furi_assert(context);
DialogsAppFileSelectContext* file_select_context = context; DialogsAppFileSelectContext* file_select_context = context;
file_select_context->result = false; file_select_context->result = false;
API_LOCK_UNLOCK(file_select_context->semaphore); API_LOCK_UNLOCK(file_select_context->lock);
} }
static void dialogs_app_file_select_callback(bool result, void* context) { static void dialogs_app_file_select_callback(bool result, void* context) {
furi_assert(context); furi_assert(context);
DialogsAppFileSelectContext* file_select_context = context; DialogsAppFileSelectContext* file_select_context = context;
file_select_context->result = result; file_select_context->result = result;
API_LOCK_UNLOCK(file_select_context->semaphore); API_LOCK_UNLOCK(file_select_context->lock);
} }
bool dialogs_app_process_module_file_select(const DialogsAppMessageDataFileSelect* data) { bool dialogs_app_process_module_file_select(const DialogsAppMessageDataFileSelect* data) {
@ -27,7 +27,7 @@ bool dialogs_app_process_module_file_select(const DialogsAppMessageDataFileSelec
DialogsAppFileSelectContext* file_select_context = DialogsAppFileSelectContext* file_select_context =
furi_alloc(sizeof(DialogsAppFileSelectContext)); furi_alloc(sizeof(DialogsAppFileSelectContext));
file_select_context->semaphore = API_LOCK_INIT_LOCKED(); file_select_context->lock = API_LOCK_INIT_LOCKED();
ViewHolder* view_holder = view_holder_alloc(); ViewHolder* view_holder = view_holder_alloc();
view_holder_attach_to_gui(view_holder, gui); view_holder_attach_to_gui(view_holder, gui);
@ -45,14 +45,15 @@ bool dialogs_app_process_module_file_select(const DialogsAppMessageDataFileSelec
view_holder_set_view(view_holder, file_select_get_view(file_select)); view_holder_set_view(view_holder, file_select_get_view(file_select));
view_holder_start(view_holder); view_holder_start(view_holder);
API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(file_select_context->semaphore); API_LOCK_WAIT_UNTIL_UNLOCK(file_select_context->lock);
ret = file_select_context->result; ret = file_select_context->result;
free(file_select_context);
view_holder_stop(view_holder); view_holder_stop(view_holder);
view_holder_free(view_holder); view_holder_free(view_holder);
file_select_free(file_select); file_select_free(file_select);
API_LOCK_FREE(file_select_context->lock);
free(file_select_context);
furi_record_close("gui"); furi_record_close("gui");
return ret; return ret;

View File

@ -3,7 +3,7 @@
#include <gui/modules/dialog_ex.h> #include <gui/modules/dialog_ex.h>
typedef struct { typedef struct {
osSemaphoreId_t semaphore; FuriApiLock lock;
DialogMessageButton result; DialogMessageButton result;
} DialogsAppMessageContext; } DialogsAppMessageContext;
@ -30,7 +30,7 @@ static void dialogs_app_message_back_callback(void* context) {
furi_assert(context); furi_assert(context);
DialogsAppMessageContext* message_context = context; DialogsAppMessageContext* message_context = context;
message_context->result = DialogMessageButtonBack; message_context->result = DialogMessageButtonBack;
API_LOCK_UNLOCK(message_context->semaphore); API_LOCK_UNLOCK(message_context->lock);
} }
static void dialogs_app_message_callback(DialogExResult result, void* context) { static void dialogs_app_message_callback(DialogExResult result, void* context) {
@ -47,7 +47,7 @@ static void dialogs_app_message_callback(DialogExResult result, void* context) {
message_context->result = DialogMessageButtonCenter; message_context->result = DialogMessageButtonCenter;
break; break;
} }
API_LOCK_UNLOCK(message_context->semaphore); API_LOCK_UNLOCK(message_context->lock);
} }
DialogMessageButton dialogs_app_process_module_message(const DialogsAppMessageDataDialog* data) { DialogMessageButton dialogs_app_process_module_message(const DialogsAppMessageDataDialog* data) {
@ -55,7 +55,7 @@ DialogMessageButton dialogs_app_process_module_message(const DialogsAppMessageDa
Gui* gui = furi_record_open("gui"); Gui* gui = furi_record_open("gui");
const DialogMessage* message = data->message; const DialogMessage* message = data->message;
DialogsAppMessageContext* message_context = furi_alloc(sizeof(DialogsAppMessageContext)); DialogsAppMessageContext* message_context = furi_alloc(sizeof(DialogsAppMessageContext));
message_context->semaphore = API_LOCK_INIT_LOCKED(); message_context->lock = API_LOCK_INIT_LOCKED();
ViewHolder* view_holder = view_holder_alloc(); ViewHolder* view_holder = view_holder_alloc();
view_holder_attach_to_gui(view_holder, gui); view_holder_attach_to_gui(view_holder, gui);
@ -85,14 +85,15 @@ DialogMessageButton dialogs_app_process_module_message(const DialogsAppMessageDa
view_holder_set_view(view_holder, dialog_ex_get_view(dialog_ex)); view_holder_set_view(view_holder, dialog_ex_get_view(dialog_ex));
view_holder_start(view_holder); view_holder_start(view_holder);
API_LOCK_WAIT_UNTIL_UNLOCK_AND_FREE(message_context->semaphore); API_LOCK_WAIT_UNTIL_UNLOCK(message_context->lock);
ret = message_context->result; ret = message_context->result;
free(message_context);
view_holder_stop(view_holder); view_holder_stop(view_holder);
view_holder_free(view_holder); view_holder_free(view_holder);
dialog_ex_free(dialog_ex); dialog_ex_free(dialog_ex);
API_LOCK_FREE(message_context->lock);
free(message_context);
furi_record_close("gui"); furi_record_close("gui");
return ret; return ret;

View File

@ -21,7 +21,7 @@ static void dialogs_app_process_message(DialogsApp* app, DialogsAppMessage* mess
dialogs_app_process_module_message(&message->data->dialog); dialogs_app_process_module_message(&message->data->dialog);
break; break;
} }
API_LOCK_UNLOCK(message->semaphore); API_LOCK_UNLOCK(message->lock);
} }
int32_t dialogs_srv(void* p) { int32_t dialogs_srv(void* p) {