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.
This commit is contained in:
Romain Vimont 2023-03-01 22:46:10 +01:00
parent 4d989de9ae
commit a0a65b3c4d
2 changed files with 22 additions and 15 deletions

View file

@ -19,7 +19,7 @@ sc_file_pusher_init(struct sc_file_pusher *fp, const char *serial,
const char *push_target) { const char *push_target) {
assert(serial); assert(serial);
cbuf_init(&fp->queue); sc_vecdeque_init(&fp->queue);
bool ok = sc_mutex_init(&fp->mutex); bool ok = sc_mutex_init(&fp->mutex);
if (!ok) { if (!ok) {
@ -65,9 +65,10 @@ sc_file_pusher_destroy(struct sc_file_pusher *fp) {
sc_intr_destroy(&fp->intr); sc_intr_destroy(&fp->intr);
free(fp->serial); free(fp->serial);
struct sc_file_pusher_request req; while (!sc_vecdeque_is_empty(&fp->queue)) {
while (cbuf_take(&fp->queue, &req)) { struct sc_file_pusher_request *req = sc_vecdeque_popref(&fp->queue);
sc_file_pusher_request_destroy(&req); 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); sc_mutex_lock(&fp->mutex);
bool was_empty = cbuf_is_empty(&fp->queue); bool was_empty = sc_vecdeque_is_empty(&fp->queue);
bool res = cbuf_push(&fp->queue, req); bool res = sc_vecdeque_push(&fp->queue, req);
if (!res) {
LOG_OOM();
sc_mutex_unlock(&fp->mutex);
return false;
}
if (was_empty) { if (was_empty) {
sc_cond_signal(&fp->event_cond); sc_cond_signal(&fp->event_cond);
} }
sc_mutex_unlock(&fp->mutex); sc_mutex_unlock(&fp->mutex);
return res;
return true;
} }
static int static int
@ -113,7 +121,7 @@ run_file_pusher(void *data) {
for (;;) { for (;;) {
sc_mutex_lock(&fp->mutex); 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); sc_cond_wait(&fp->event_cond, &fp->mutex);
} }
if (fp->stopped) { if (fp->stopped) {
@ -121,10 +129,9 @@ run_file_pusher(void *data) {
sc_mutex_unlock(&fp->mutex); sc_mutex_unlock(&fp->mutex);
break; break;
} }
struct sc_file_pusher_request req;
bool non_empty = cbuf_take(&fp->queue, &req); assert(!sc_vecdeque_is_empty(&fp->queue));
assert(non_empty); struct sc_file_pusher_request req = sc_vecdeque_pop(&fp->queue);
(void) non_empty;
sc_mutex_unlock(&fp->mutex); sc_mutex_unlock(&fp->mutex);
if (req.action == SC_FILE_PUSHER_ACTION_INSTALL_APK) { if (req.action == SC_FILE_PUSHER_ACTION_INSTALL_APK) {

View file

@ -5,9 +5,9 @@
#include <stdbool.h> #include <stdbool.h>
#include "util/cbuf.h"
#include "util/thread.h"
#include "util/intr.h" #include "util/intr.h"
#include "util/thread.h"
#include "util/vecdeque.h"
enum sc_file_pusher_action { enum sc_file_pusher_action {
SC_FILE_PUSHER_ACTION_INSTALL_APK, SC_FILE_PUSHER_ACTION_INSTALL_APK,
@ -19,7 +19,7 @@ struct sc_file_pusher_request {
char *file; 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 { struct sc_file_pusher {
char *serial; char *serial;