From fcfd829040d53c72e8e9211f96534ff12d1d0110 Mon Sep 17 00:00:00 2001 From: wargio Date: Thu, 4 Jan 2024 23:18:29 +0800 Subject: [PATCH 1/2] Add rz_th_queue_new3 to initialize a queue from a pvector --- librz/include/rz_th.h | 1 + librz/util/thread_queue.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/librz/include/rz_th.h b/librz/include/rz_th.h index 070afbc7f17..ed1e1dcccc4 100644 --- a/librz/include/rz_th.h +++ b/librz/include/rz_th.h @@ -73,6 +73,7 @@ RZ_API size_t rz_th_pool_size(RZ_NONNULL RzThreadPool *pool); RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new(size_t max_size, RZ_NULLABLE RzListFree qfree); RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /**/ *list); +RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new3(RZ_NONNULL RZ_BORROW RzPVector /**/ *vector, RZ_NULLABLE RzListFree qfree); RZ_API void rz_th_queue_free(RZ_NULLABLE RzThreadQueue *queue); RZ_API bool rz_th_queue_push(RZ_NONNULL RzThreadQueue *queue, RZ_NONNULL void *user, bool tail); RZ_API RZ_OWN void *rz_th_queue_pop(RZ_NONNULL RzThreadQueue *queue, bool tail); diff --git a/librz/util/thread_queue.c b/librz/util/thread_queue.c index b56d6436cdc..b9abda0c484 100644 --- a/librz/util/thread_queue.c +++ b/librz/util/thread_queue.c @@ -73,6 +73,36 @@ RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /**/ *vector, RZ_NULLABLE RzListFree qfree) { + rz_return_val_if_fail(vector, NULL); + RzThreadQueue *queue = rz_th_queue_new(rz_pvector_len(vector), qfree); + if (!queue) { + return NULL; + } + + void **it; + rz_pvector_foreach (vector, it) { + void *value = *it; + if (!value) { + continue; + } + if (!rz_list_append(queue->list, value)) { + rz_th_queue_free(queue); + return NULL; + } + } + + return queue; +} + /** * \brief Frees a RzThreadQueue structure * From 0edb8ce5555502fc8f4d937bc21f2690272063b7 Mon Sep 17 00:00:00 2001 From: wargio Date: Fri, 5 Jan 2024 20:43:07 +0800 Subject: [PATCH 2/2] Rename methods to create queue. --- librz/analysis/similarity.c | 2 +- librz/include/rz_th.h | 4 ++-- librz/util/thread_queue.c | 14 ++++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/librz/analysis/similarity.c b/librz/analysis/similarity.c index bb722955672..36914f5aec1 100644 --- a/librz/analysis/similarity.c +++ b/librz/analysis/similarity.c @@ -45,7 +45,7 @@ typedef struct match_ui_info_t { static bool shared_context_init(SharedContext *context, RzAnalysis *analysis_a, RzAnalysis *analysis_b, RzList /**/ *list_a, RzList /**/ *list_b, AllocateBuffer alloc_cb) { RzThreadLock *lock_a = rz_th_lock_new(true); RzThreadLock *lock_b = analysis_a == analysis_b ? lock_a : rz_th_lock_new(true); - RzThreadQueue *queue = rz_th_queue_new2(rz_list_clone(list_a)); + RzThreadQueue *queue = rz_th_queue_from_list(list_a, NULL); RzThreadQueue *matches = rz_th_queue_new(RZ_THREAD_QUEUE_UNLIMITED, NULL); RzThreadQueue *unmatch = rz_th_queue_new(RZ_THREAD_QUEUE_UNLIMITED, NULL); RzAtomicBool *loop = rz_atomic_bool_new(true); diff --git a/librz/include/rz_th.h b/librz/include/rz_th.h index ed1e1dcccc4..913667092c6 100644 --- a/librz/include/rz_th.h +++ b/librz/include/rz_th.h @@ -72,8 +72,8 @@ RZ_API bool rz_th_pool_wait(RZ_NONNULL RzThreadPool *pool); RZ_API size_t rz_th_pool_size(RZ_NONNULL RzThreadPool *pool); RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new(size_t max_size, RZ_NULLABLE RzListFree qfree); -RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /**/ *list); -RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new3(RZ_NONNULL RZ_BORROW RzPVector /**/ *vector, RZ_NULLABLE RzListFree qfree); +RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_list(RZ_NONNULL RZ_BORROW RzList /**/ *list, RZ_NULLABLE RzListFree qfree); +RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_pvector(RZ_NONNULL RZ_BORROW RzPVector /**/ *vector, RZ_NULLABLE RzListFree qfree); RZ_API void rz_th_queue_free(RZ_NULLABLE RzThreadQueue *queue); RZ_API bool rz_th_queue_push(RZ_NONNULL RzThreadQueue *queue, RZ_NONNULL void *user, bool tail); RZ_API RZ_OWN void *rz_th_queue_pop(RZ_NONNULL RzThreadQueue *queue, bool tail); diff --git a/librz/util/thread_queue.c b/librz/util/thread_queue.c index b9abda0c484..2248cf5f274 100644 --- a/librz/util/thread_queue.c +++ b/librz/util/thread_queue.c @@ -50,19 +50,25 @@ RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new(size_t max_size, RZ_NULLABLE RzList /** * \brief Allocates and initializes a new fifo queue using a user-defined list * - * \param list Pointer to the list that will be owned by the queue. + * \param list Pointer to the list that will be used to initialize the queue. * * \return On success returns a valid pointer, otherwise NULL */ -RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /**/ *list) { +RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_list(RZ_NONNULL RZ_BORROW RzList /**/ *list, RZ_NULLABLE RzListFree qfree) { rz_return_val_if_fail(list, NULL); RzThreadQueue *queue = RZ_NEW0(RzThreadQueue); if (!queue) { return NULL; } + queue->list = rz_list_clone(list); + if (!queue->list) { + free(queue); + return NULL; + } + + queue->list->free = qfree; queue->max_size = rz_list_length(list); - queue->list = list; queue->lock = rz_th_lock_new(false); queue->cond = rz_th_cond_new(); if (!queue->list || !queue->lock || !queue->cond) { @@ -81,7 +87,7 @@ RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /**/ *vector, RZ_NULLABLE RzListFree qfree) { +RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_pvector(RZ_NONNULL RZ_BORROW RzPVector /**/ *vector, RZ_NULLABLE RzListFree qfree) { rz_return_val_if_fail(vector, NULL); RzThreadQueue *queue = rz_th_queue_new(rz_pvector_len(vector), qfree); if (!queue) {