damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit c8d88058d46089e11d32140a35ffb7e425589734
parent b8bef86ea1aa34044ebdfe2862386108fc22a76d
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  5 Feb 2024 16:47:10 -0800

nostrdb: queue: switch to prot_queue_try_pop_all

This allows you to `try pop` multiple items instead of 1

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mnostrdb/src/protected_queue.h | 14++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/nostrdb/src/protected_queue.h b/nostrdb/src/protected_queue.h @@ -154,7 +154,9 @@ static int prot_queue_push_all(struct prot_queue* q, void *data, int count) * data - Pointer to where the popped data will be stored. * Returns 1 if successful, 0 if the queue is empty. */ -static inline int prot_queue_try_pop(struct prot_queue *q, void *data) { +static inline int prot_queue_try_pop_all(struct prot_queue *q, void *data, int max_items) { + int items_to_pop, items_until_end; + pthread_mutex_lock(&q->mutex); if (q->count == 0) { @@ -162,9 +164,13 @@ static inline int prot_queue_try_pop(struct prot_queue *q, void *data) { return 0; } - memcpy(data, &q->buf[q->head * q->elem_size], q->elem_size); - q->head = (q->head + 1) % prot_queue_capacity(q); - q->count--; + items_until_end = (q->buflen - q->head * q->elem_size) / q->elem_size; + items_to_pop = min(q->count, max_items); + items_to_pop = min(items_to_pop, items_until_end); + + memcpy(data, &q->buf[q->head * q->elem_size], items_to_pop * q->elem_size); + q->head = (q->head + items_to_pop) % prot_queue_capacity(q); + q->count -= items_to_pop; pthread_mutex_unlock(&q->mutex); return 1;