Use cbuf for file handler request queue

Replace the file_handler_request_queue implementation by cbuf.
This commit is contained in:
Romain Vimont 2019-05-29 21:46:16 +02:00
parent 241a3dcba5
commit 073181b294
2 changed files with 36 additions and 105 deletions

View file

@ -2,90 +2,22 @@
#include <string.h> #include <string.h>
#include <SDL2/SDL_assert.h> #include <SDL2/SDL_assert.h>
#include "config.h" #include "config.h"
#include "command.h" #include "command.h"
#include "device.h" #include "device.h"
#include "lock_util.h" #include "lock_util.h"
#include "log.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 static void
request_free(struct request *req) { file_handler_request_destroy(struct file_handler_request *req) {
if (!req) {
return;
}
SDL_free(req->file); 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 bool
file_handler_init(struct file_handler *file_handler, const char *serial) { file_handler_init(struct file_handler *file_handler, const char *serial) {
if (!request_queue_init(&file_handler->queue)) { cbuf_init(&file_handler->queue);
return false;
}
if (!(file_handler->mutex = SDL_CreateMutex())) { if (!(file_handler->mutex = SDL_CreateMutex())) {
return false; return false;
@ -121,8 +53,12 @@ void
file_handler_destroy(struct file_handler *file_handler) { file_handler_destroy(struct file_handler *file_handler) {
SDL_DestroyCond(file_handler->event_cond); SDL_DestroyCond(file_handler->event_cond);
SDL_DestroyMutex(file_handler->mutex); SDL_DestroyMutex(file_handler->mutex);
request_queue_destroy(&file_handler->queue);
SDL_free(file_handler->serial); 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 static process_t
@ -137,10 +73,7 @@ push_file(const char *serial, const char *file) {
bool bool
file_handler_request(struct file_handler *file_handler, file_handler_request(struct file_handler *file_handler,
file_handler_action_t action, file_handler_action_t action, char *file) {
char *file) {
bool res;
// start file_handler if it's used for the first time // start file_handler if it's used for the first time
if (!file_handler->initialized) { if (!file_handler->initialized) {
if (!file_handler_start(file_handler)) { 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", LOGI("Request to %s %s", action == ACTION_INSTALL_APK ? "install" : "push",
file); file);
struct request *req = request_new(action, file); struct file_handler_request req = {
if (!req) { .action = action,
LOGE("Could not create request"); .file = file,
return false; };
}
mutex_lock(file_handler->mutex); mutex_lock(file_handler->mutex);
bool was_empty = request_queue_is_empty(&file_handler->queue); bool was_empty = cbuf_is_empty(&file_handler->queue);
res = request_queue_push(&file_handler->queue, req); bool res = cbuf_push(&file_handler->queue, req);
if (was_empty) { if (was_empty) {
cond_signal(file_handler->event_cond); cond_signal(file_handler->event_cond);
} }
@ -174,8 +106,7 @@ run_file_handler(void *data) {
for (;;) { for (;;) {
mutex_lock(file_handler->mutex); mutex_lock(file_handler->mutex);
file_handler->current_process = PROCESS_NONE; file_handler->current_process = PROCESS_NONE;
while (!file_handler->stopped while (!file_handler->stopped && cbuf_is_empty(&file_handler->queue)) {
&& request_queue_is_empty(&file_handler->queue)) {
cond_wait(file_handler->event_cond, file_handler->mutex); cond_wait(file_handler->event_cond, file_handler->mutex);
} }
if (file_handler->stopped) { if (file_handler->stopped) {
@ -183,36 +114,36 @@ run_file_handler(void *data) {
mutex_unlock(file_handler->mutex); mutex_unlock(file_handler->mutex);
break; break;
} }
struct request *req; struct file_handler_request req;
bool non_empty = request_queue_take(&file_handler->queue, &req); bool non_empty = cbuf_take(&file_handler->queue, &req);
SDL_assert(non_empty); SDL_assert(non_empty);
process_t process; process_t process;
if (req->action == ACTION_INSTALL_APK) { if (req.action == ACTION_INSTALL_APK) {
LOGI("Installing %s...", req->file); LOGI("Installing %s...", req.file);
process = install_apk(file_handler->serial, req->file); process = install_apk(file_handler->serial, req.file);
} else { } else {
LOGI("Pushing %s...", req->file); LOGI("Pushing %s...", req.file);
process = push_file(file_handler->serial, req->file); process = push_file(file_handler->serial, req.file);
} }
file_handler->current_process = process; file_handler->current_process = process;
mutex_unlock(file_handler->mutex); mutex_unlock(file_handler->mutex);
if (req->action == ACTION_INSTALL_APK) { if (req.action == ACTION_INSTALL_APK) {
if (process_check_success(process, "adb install")) { if (process_check_success(process, "adb install")) {
LOGI("%s successfully installed", req->file); LOGI("%s successfully installed", req.file);
} else { } else {
LOGE("Failed to install %s", req->file); LOGE("Failed to install %s", req.file);
} }
} else { } else {
if (process_check_success(process, "adb push")) { if (process_check_success(process, "adb push")) {
LOGI("%s successfully pushed to /sdcard/", req->file); LOGI("%s successfully pushed to /sdcard/", req.file);
} else { } 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; return 0;
} }

View file

@ -5,21 +5,21 @@
#include <SDL2/SDL_mutex.h> #include <SDL2/SDL_mutex.h>
#include <SDL2/SDL_thread.h> #include <SDL2/SDL_thread.h>
#include "cbuf.h"
#include "command.h" #include "command.h"
#define REQUEST_QUEUE_SIZE 16
typedef enum { typedef enum {
ACTION_INSTALL_APK, ACTION_INSTALL_APK,
ACTION_PUSH_FILE, ACTION_PUSH_FILE,
} file_handler_action_t; } file_handler_action_t;
struct request_queue { struct file_handler_request {
struct request *reqs[REQUEST_QUEUE_SIZE]; file_handler_action_t action;
int tail; char *file;
int head;
}; };
struct file_handler_request_queue CBUF(struct file_handler_request, 16);
struct file_handler { struct file_handler {
char *serial; char *serial;
SDL_Thread *thread; SDL_Thread *thread;
@ -28,7 +28,7 @@ struct file_handler {
bool stopped; bool stopped;
bool initialized; bool initialized;
process_t current_process; process_t current_process;
struct request_queue queue; struct file_handler_request_queue queue;
}; };
bool bool