Use VecDeque in aoa_hid

Replace cbuf by VecDeque in aoa_hid
This commit is contained in:
Romain Vimont 2023-03-01 22:50:56 +01:00
parent a0a65b3c4d
commit f978e4d6de
2 changed files with 29 additions and 16 deletions

View file

@ -14,6 +14,8 @@
#define DEFAULT_TIMEOUT 1000 #define DEFAULT_TIMEOUT 1000
#define SC_HID_EVENT_QUEUE_MAX 64
static void static void
sc_hid_event_log(const struct sc_hid_event *event) { sc_hid_event_log(const struct sc_hid_event *event) {
// HID Event: [00] FF FF FF FF... // HID Event: [00] FF FF FF FF...
@ -48,14 +50,20 @@ sc_hid_event_destroy(struct sc_hid_event *hid_event) {
bool bool
sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb, sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb,
struct sc_acksync *acksync) { struct sc_acksync *acksync) {
cbuf_init(&aoa->queue); sc_vecdeque_init(&aoa->queue);
if (!sc_vecdeque_reserve(&aoa->queue, SC_HID_EVENT_QUEUE_MAX)) {
return false;
}
if (!sc_mutex_init(&aoa->mutex)) { if (!sc_mutex_init(&aoa->mutex)) {
sc_vecdeque_destroy(&aoa->queue);
return false; return false;
} }
if (!sc_cond_init(&aoa->event_cond)) { if (!sc_cond_init(&aoa->event_cond)) {
sc_mutex_destroy(&aoa->mutex); sc_mutex_destroy(&aoa->mutex);
sc_vecdeque_destroy(&aoa->queue);
return false; return false;
} }
@ -69,9 +77,10 @@ sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb,
void void
sc_aoa_destroy(struct sc_aoa *aoa) { sc_aoa_destroy(struct sc_aoa *aoa) {
// Destroy remaining events // Destroy remaining events
struct sc_hid_event event; while (!sc_vecdeque_is_empty(&aoa->queue)) {
while (cbuf_take(&aoa->queue, &event)) { struct sc_hid_event *event = sc_vecdeque_popref(&aoa->queue);
sc_hid_event_destroy(&event); assert(event);
sc_hid_event_destroy(event);
} }
sc_cond_destroy(&aoa->event_cond); sc_cond_destroy(&aoa->event_cond);
@ -212,13 +221,19 @@ sc_aoa_push_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
} }
sc_mutex_lock(&aoa->mutex); sc_mutex_lock(&aoa->mutex);
bool was_empty = cbuf_is_empty(&aoa->queue); bool full = sc_vecdeque_is_full(&aoa->queue);
bool res = cbuf_push(&aoa->queue, *event); if (!full) {
bool was_empty = sc_vecdeque_is_empty(&aoa->queue);
sc_vecdeque_push_noresize(&aoa->queue, *event);
if (was_empty) { if (was_empty) {
sc_cond_signal(&aoa->event_cond); sc_cond_signal(&aoa->event_cond);
} }
}
// Otherwise (if the queue is full), the event is discarded
sc_mutex_unlock(&aoa->mutex); sc_mutex_unlock(&aoa->mutex);
return res;
return !full;
} }
static int static int
@ -227,7 +242,7 @@ run_aoa_thread(void *data) {
for (;;) { for (;;) {
sc_mutex_lock(&aoa->mutex); sc_mutex_lock(&aoa->mutex);
while (!aoa->stopped && cbuf_is_empty(&aoa->queue)) { while (!aoa->stopped && sc_vecdeque_is_empty(&aoa->queue)) {
sc_cond_wait(&aoa->event_cond, &aoa->mutex); sc_cond_wait(&aoa->event_cond, &aoa->mutex);
} }
if (aoa->stopped) { if (aoa->stopped) {
@ -235,11 +250,9 @@ run_aoa_thread(void *data) {
sc_mutex_unlock(&aoa->mutex); sc_mutex_unlock(&aoa->mutex);
break; break;
} }
struct sc_hid_event event;
bool non_empty = cbuf_take(&aoa->queue, &event);
assert(non_empty);
(void) non_empty;
assert(!sc_vecdeque_is_empty(&aoa->queue));
struct sc_hid_event event = sc_vecdeque_pop(&aoa->queue);
uint64_t ack_to_wait = event.ack_to_wait; uint64_t ack_to_wait = event.ack_to_wait;
sc_mutex_unlock(&aoa->mutex); sc_mutex_unlock(&aoa->mutex);

View file

@ -8,9 +8,9 @@
#include "usb.h" #include "usb.h"
#include "util/acksync.h" #include "util/acksync.h"
#include "util/cbuf.h"
#include "util/thread.h" #include "util/thread.h"
#include "util/tick.h" #include "util/tick.h"
#include "util/vecdeque.h"
struct sc_hid_event { struct sc_hid_event {
uint16_t accessory_id; uint16_t accessory_id;
@ -27,7 +27,7 @@ sc_hid_event_init(struct sc_hid_event *hid_event, uint16_t accessory_id,
void void
sc_hid_event_destroy(struct sc_hid_event *hid_event); sc_hid_event_destroy(struct sc_hid_event *hid_event);
struct sc_hid_event_queue CBUF(struct sc_hid_event, 64); struct sc_hid_event_queue SC_VECDEQUE(struct sc_hid_event);
struct sc_aoa { struct sc_aoa {
struct sc_usb *usb; struct sc_usb *usb;