[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
#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) \
osSemaphoreAcquire(_lock, osWaitForever); \
osSemaphoreDelete(_lock);
#define API_LOCK_UNLOCK(_lock) osSemaphoreRelease(_lock);
API_LOCK_WAIT_UNTIL_UNLOCK(_lock); \
API_LOCK_FREE(_lock);

View File

@ -10,8 +10,8 @@ bool dialog_file_select_show(
char* result,
uint8_t result_size,
const char* preselected_filename) {
osSemaphoreId_t semaphore = API_LOCK_INIT_LOCKED();
furi_check(semaphore != NULL);
FuriApiLock lock = API_LOCK_INIT_LOCKED();
furi_check(lock != NULL);
DialogsAppData data = {
.file_select = {
@ -24,14 +24,14 @@ bool dialog_file_select_show(
DialogsAppReturn return_data;
DialogsAppMessage message = {
.semaphore = semaphore,
.lock = lock,
.command = DialogsAppCommandFileOpen,
.data = &data,
.return_data = &return_data,
};
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;
}
@ -39,8 +39,8 @@ bool dialog_file_select_show(
/****************** Message ******************/
DialogMessageButton dialog_message_show(DialogsApp* context, const DialogMessage* dialog_message) {
osSemaphoreId_t semaphore = API_LOCK_INIT_LOCKED();
furi_check(semaphore != NULL);
FuriApiLock lock = API_LOCK_INIT_LOCKED();
furi_check(lock != NULL);
DialogsAppData data = {
.dialog = {
@ -49,14 +49,14 @@ DialogMessageButton dialog_message_show(DialogsApp* context, const DialogMessage
DialogsAppReturn return_data;
DialogsAppMessage message = {
.semaphore = semaphore,
.lock = lock,
.command = DialogsAppCommandDialog,
.data = &data,
.return_data = &return_data,
};
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;
}

View File

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

View File

@ -3,7 +3,7 @@
#include <gui/modules/file_select.h>
typedef struct {
osSemaphoreId_t semaphore;
FuriApiLock lock;
bool result;
} DialogsAppFileSelectContext;
@ -11,14 +11,14 @@ static void dialogs_app_file_select_back_callback(void* context) {
furi_assert(context);
DialogsAppFileSelectContext* file_select_context = context;
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) {
furi_assert(context);
DialogsAppFileSelectContext* file_select_context = context;
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) {
@ -27,7 +27,7 @@ bool dialogs_app_process_module_file_select(const DialogsAppMessageDataFileSelec
DialogsAppFileSelectContext* file_select_context =
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();
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_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;
free(file_select_context);
view_holder_stop(view_holder);
view_holder_free(view_holder);
file_select_free(file_select);
API_LOCK_FREE(file_select_context->lock);
free(file_select_context);
furi_record_close("gui");
return ret;

View File

@ -3,7 +3,7 @@
#include <gui/modules/dialog_ex.h>
typedef struct {
osSemaphoreId_t semaphore;
FuriApiLock lock;
DialogMessageButton result;
} DialogsAppMessageContext;
@ -30,7 +30,7 @@ static void dialogs_app_message_back_callback(void* context) {
furi_assert(context);
DialogsAppMessageContext* message_context = context;
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) {
@ -47,7 +47,7 @@ static void dialogs_app_message_callback(DialogExResult result, void* context) {
message_context->result = DialogMessageButtonCenter;
break;
}
API_LOCK_UNLOCK(message_context->semaphore);
API_LOCK_UNLOCK(message_context->lock);
}
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");
const DialogMessage* message = data->message;
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();
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_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;
free(message_context);
view_holder_stop(view_holder);
view_holder_free(view_holder);
dialog_ex_free(dialog_ex);
API_LOCK_FREE(message_context->lock);
free(message_context);
furi_record_close("gui");
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);
break;
}
API_LOCK_UNLOCK(message->semaphore);
API_LOCK_UNLOCK(message->lock);
}
int32_t dialogs_srv(void* p) {