From a0a65b3c4da6b56ca320959451fdf2979a34738d Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 1 Mar 2023 22:46:10 +0100 Subject: [PATCH] Use VecDeque in file_pusher Replace cbuf by VecDeque in file_pusher. As a side-effect, the new implementation does not limit the queue to an arbitrary value. --- app/src/file_pusher.c | 31 +++++++++++++++++++------------ app/src/file_pusher.h | 6 +++--- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/src/file_pusher.c b/app/src/file_pusher.c index f6757870..b49e93e5 100644 --- a/app/src/file_pusher.c +++ b/app/src/file_pusher.c @@ -19,7 +19,7 @@ sc_file_pusher_init(struct sc_file_pusher *fp, const char *serial, const char *push_target) { assert(serial); - cbuf_init(&fp->queue); + sc_vecdeque_init(&fp->queue); bool ok = sc_mutex_init(&fp->mutex); if (!ok) { @@ -65,9 +65,10 @@ sc_file_pusher_destroy(struct sc_file_pusher *fp) { sc_intr_destroy(&fp->intr); free(fp->serial); - struct sc_file_pusher_request req; - while (cbuf_take(&fp->queue, &req)) { - sc_file_pusher_request_destroy(&req); + while (!sc_vecdeque_is_empty(&fp->queue)) { + struct sc_file_pusher_request *req = sc_vecdeque_popref(&fp->queue); + assert(req); + sc_file_pusher_request_destroy(req); } } @@ -91,13 +92,20 @@ sc_file_pusher_request(struct sc_file_pusher *fp, }; sc_mutex_lock(&fp->mutex); - bool was_empty = cbuf_is_empty(&fp->queue); - bool res = cbuf_push(&fp->queue, req); + bool was_empty = sc_vecdeque_is_empty(&fp->queue); + bool res = sc_vecdeque_push(&fp->queue, req); + if (!res) { + LOG_OOM(); + sc_mutex_unlock(&fp->mutex); + return false; + } + if (was_empty) { sc_cond_signal(&fp->event_cond); } sc_mutex_unlock(&fp->mutex); - return res; + + return true; } static int @@ -113,7 +121,7 @@ run_file_pusher(void *data) { for (;;) { sc_mutex_lock(&fp->mutex); - while (!fp->stopped && cbuf_is_empty(&fp->queue)) { + while (!fp->stopped && sc_vecdeque_is_empty(&fp->queue)) { sc_cond_wait(&fp->event_cond, &fp->mutex); } if (fp->stopped) { @@ -121,10 +129,9 @@ run_file_pusher(void *data) { sc_mutex_unlock(&fp->mutex); break; } - struct sc_file_pusher_request req; - bool non_empty = cbuf_take(&fp->queue, &req); - assert(non_empty); - (void) non_empty; + + assert(!sc_vecdeque_is_empty(&fp->queue)); + struct sc_file_pusher_request req = sc_vecdeque_pop(&fp->queue); sc_mutex_unlock(&fp->mutex); if (req.action == SC_FILE_PUSHER_ACTION_INSTALL_APK) { diff --git a/app/src/file_pusher.h b/app/src/file_pusher.h index 0d934d6c..0ffb3721 100644 --- a/app/src/file_pusher.h +++ b/app/src/file_pusher.h @@ -5,9 +5,9 @@ #include -#include "util/cbuf.h" -#include "util/thread.h" #include "util/intr.h" +#include "util/thread.h" +#include "util/vecdeque.h" enum sc_file_pusher_action { SC_FILE_PUSHER_ACTION_INSTALL_APK, @@ -19,7 +19,7 @@ struct sc_file_pusher_request { char *file; }; -struct sc_file_pusher_request_queue CBUF(struct sc_file_pusher_request, 16); +struct sc_file_pusher_request_queue SC_VECDEQUE(struct sc_file_pusher_request); struct sc_file_pusher { char *serial;