From acb8508249c6222727d09f49b69f0389646c1573 Mon Sep 17 00:00:00 2001 From: SG Date: Sat, 25 Sep 2021 02:36:53 +1000 Subject: [PATCH] [FL-1880] Dialogs: fix semaphore lock (#722) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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: あく --- applications/dialogs/dialogs-api-lock.h | 20 ++++++++++++++----- applications/dialogs/dialogs-api.c | 16 +++++++-------- applications/dialogs/dialogs-message.h | 3 ++- .../dialogs/dialogs-module-file-select.c | 13 ++++++------ applications/dialogs/dialogs-module-message.c | 13 ++++++------ applications/dialogs/dialogs.c | 2 +- 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/applications/dialogs/dialogs-api-lock.h b/applications/dialogs/dialogs-api-lock.h index fe9e934f..ad3c78e5 100644 --- a/applications/dialogs/dialogs-api-lock.h +++ b/applications/dialogs/dialogs-api-lock.h @@ -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); \ No newline at end of file + API_LOCK_WAIT_UNTIL_UNLOCK(_lock); \ + API_LOCK_FREE(_lock); diff --git a/applications/dialogs/dialogs-api.c b/applications/dialogs/dialogs-api.c index 5ad9cedc..681724fa 100644 --- a/applications/dialogs/dialogs-api.c +++ b/applications/dialogs/dialogs-api.c @@ -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; } diff --git a/applications/dialogs/dialogs-message.h b/applications/dialogs/dialogs-message.h index 0bb6d86f..e3bd6c79 100644 --- a/applications/dialogs/dialogs-message.h +++ b/applications/dialogs/dialogs-message.h @@ -1,6 +1,7 @@ #pragma once #include #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; diff --git a/applications/dialogs/dialogs-module-file-select.c b/applications/dialogs/dialogs-module-file-select.c index 1f2abb40..473f1451 100644 --- a/applications/dialogs/dialogs-module-file-select.c +++ b/applications/dialogs/dialogs-module-file-select.c @@ -3,7 +3,7 @@ #include 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; diff --git a/applications/dialogs/dialogs-module-message.c b/applications/dialogs/dialogs-module-message.c index 77e727ef..f31396df 100644 --- a/applications/dialogs/dialogs-module-message.c +++ b/applications/dialogs/dialogs-module-message.c @@ -3,7 +3,7 @@ #include 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; diff --git a/applications/dialogs/dialogs.c b/applications/dialogs/dialogs.c index 8395e852..1b64ca77 100644 --- a/applications/dialogs/dialogs.c +++ b/applications/dialogs/dialogs.c @@ -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) {