From 073181b294c27e2abd7581a9dcef0de9b1762156 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 29 May 2019 21:46:16 +0200 Subject: [PATCH] Use cbuf for file handler request queue Replace the file_handler_request_queue implementation by cbuf. --- app/src/file_handler.c | 127 ++++++++++------------------------------- app/src/file_handler.h | 14 ++--- 2 files changed, 36 insertions(+), 105 deletions(-) diff --git a/app/src/file_handler.c b/app/src/file_handler.c index 79763c6e..051db897 100644 --- a/app/src/file_handler.c +++ b/app/src/file_handler.c @@ -2,90 +2,22 @@ #include #include + #include "config.h" #include "command.h" #include "device.h" #include "lock_util.h" #include "log.h" -struct request { - file_handler_action_t action; - char *file; -}; - -static struct request * -request_new(file_handler_action_t action, char *file) { - struct request *req = SDL_malloc(sizeof(*req)); - if (!req) { - return NULL; - } - req->action = action; - req->file = file; - return req; -} - static void -request_free(struct request *req) { - if (!req) { - return; - } +file_handler_request_destroy(struct file_handler_request *req) { SDL_free(req->file); - SDL_free(req); -} - -static bool -request_queue_is_empty(const struct request_queue *queue) { - return queue->head == queue->tail; -} - -static bool -request_queue_is_full(const struct request_queue *queue) { - return (queue->head + 1) % REQUEST_QUEUE_SIZE == queue->tail; -} - -static bool -request_queue_init(struct request_queue *queue) { - queue->head = 0; - queue->tail = 0; - return true; -} - -static void -request_queue_destroy(struct request_queue *queue) { - int i = queue->tail; - while (i != queue->head) { - request_free(queue->reqs[i]); - i = (i + 1) % REQUEST_QUEUE_SIZE; - } -} - -static bool -request_queue_push(struct request_queue *queue, struct request *req) { - if (request_queue_is_full(queue)) { - return false; - } - queue->reqs[queue->head] = req; - queue->head = (queue->head + 1) % REQUEST_QUEUE_SIZE; - return true; -} - -static bool -request_queue_take(struct request_queue *queue, struct request **req) { - if (request_queue_is_empty(queue)) { - return false; - } - // transfer ownership - *req = queue->reqs[queue->tail]; - queue->tail = (queue->tail + 1) % REQUEST_QUEUE_SIZE; - return true; } bool file_handler_init(struct file_handler *file_handler, const char *serial) { - if (!request_queue_init(&file_handler->queue)) { - return false; - } + cbuf_init(&file_handler->queue); if (!(file_handler->mutex = SDL_CreateMutex())) { return false; @@ -121,8 +53,12 @@ void file_handler_destroy(struct file_handler *file_handler) { SDL_DestroyCond(file_handler->event_cond); SDL_DestroyMutex(file_handler->mutex); - request_queue_destroy(&file_handler->queue); SDL_free(file_handler->serial); + + struct file_handler_request req; + while (cbuf_take(&file_handler->queue, &req)) { + file_handler_request_destroy(&req); + } } static process_t @@ -137,10 +73,7 @@ push_file(const char *serial, const char *file) { bool file_handler_request(struct file_handler *file_handler, - file_handler_action_t action, - char *file) { - bool res; - + file_handler_action_t action, char *file) { // start file_handler if it's used for the first time if (!file_handler->initialized) { if (!file_handler_start(file_handler)) { @@ -151,15 +84,14 @@ file_handler_request(struct file_handler *file_handler, LOGI("Request to %s %s", action == ACTION_INSTALL_APK ? "install" : "push", file); - struct request *req = request_new(action, file); - if (!req) { - LOGE("Could not create request"); - return false; - } + struct file_handler_request req = { + .action = action, + .file = file, + }; mutex_lock(file_handler->mutex); - bool was_empty = request_queue_is_empty(&file_handler->queue); - res = request_queue_push(&file_handler->queue, req); + bool was_empty = cbuf_is_empty(&file_handler->queue); + bool res = cbuf_push(&file_handler->queue, req); if (was_empty) { cond_signal(file_handler->event_cond); } @@ -174,8 +106,7 @@ run_file_handler(void *data) { for (;;) { mutex_lock(file_handler->mutex); file_handler->current_process = PROCESS_NONE; - while (!file_handler->stopped - && request_queue_is_empty(&file_handler->queue)) { + while (!file_handler->stopped && cbuf_is_empty(&file_handler->queue)) { cond_wait(file_handler->event_cond, file_handler->mutex); } if (file_handler->stopped) { @@ -183,36 +114,36 @@ run_file_handler(void *data) { mutex_unlock(file_handler->mutex); break; } - struct request *req; - bool non_empty = request_queue_take(&file_handler->queue, &req); + struct file_handler_request req; + bool non_empty = cbuf_take(&file_handler->queue, &req); SDL_assert(non_empty); process_t process; - if (req->action == ACTION_INSTALL_APK) { - LOGI("Installing %s...", req->file); - process = install_apk(file_handler->serial, req->file); + if (req.action == ACTION_INSTALL_APK) { + LOGI("Installing %s...", req.file); + process = install_apk(file_handler->serial, req.file); } else { - LOGI("Pushing %s...", req->file); - process = push_file(file_handler->serial, req->file); + LOGI("Pushing %s...", req.file); + process = push_file(file_handler->serial, req.file); } file_handler->current_process = process; mutex_unlock(file_handler->mutex); - if (req->action == ACTION_INSTALL_APK) { + if (req.action == ACTION_INSTALL_APK) { if (process_check_success(process, "adb install")) { - LOGI("%s successfully installed", req->file); + LOGI("%s successfully installed", req.file); } else { - LOGE("Failed to install %s", req->file); + LOGE("Failed to install %s", req.file); } } else { if (process_check_success(process, "adb push")) { - LOGI("%s successfully pushed to /sdcard/", req->file); + LOGI("%s successfully pushed to /sdcard/", req.file); } else { - LOGE("Failed to push %s to /sdcard/", req->file); + LOGE("Failed to push %s to /sdcard/", req.file); } } - request_free(req); + file_handler_request_destroy(&req); } return 0; } diff --git a/app/src/file_handler.h b/app/src/file_handler.h index 382477d8..22245105 100644 --- a/app/src/file_handler.h +++ b/app/src/file_handler.h @@ -5,21 +5,21 @@ #include #include +#include "cbuf.h" #include "command.h" -#define REQUEST_QUEUE_SIZE 16 - typedef enum { ACTION_INSTALL_APK, ACTION_PUSH_FILE, } file_handler_action_t; -struct request_queue { - struct request *reqs[REQUEST_QUEUE_SIZE]; - int tail; - int head; +struct file_handler_request { + file_handler_action_t action; + char *file; }; +struct file_handler_request_queue CBUF(struct file_handler_request, 16); + struct file_handler { char *serial; SDL_Thread *thread; @@ -28,7 +28,7 @@ struct file_handler { bool stopped; bool initialized; process_t current_process; - struct request_queue queue; + struct file_handler_request_queue queue; }; bool