Skip to content

Commit

Permalink
Add rz_th_queue_from_pvector to initialize a queue from a pvector (#4069
Browse files Browse the repository at this point in the history
)

* Add rz_th_queue_from_pvector to initialize a queue from a pvector
* Rename methods to create queue.
  • Loading branch information
wargio authored Jan 5, 2024
1 parent 77302c8 commit a060e3b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion librz/analysis/similarity.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef struct match_ui_info_t {
static bool shared_context_init(SharedContext *context, RzAnalysis *analysis_a, RzAnalysis *analysis_b, RzList /*<void *>*/ *list_a, RzList /*<void *>*/ *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);
Expand Down
3 changes: 2 additions & 1 deletion librz/include/rz_th.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +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 /*<void *>*/ *list);
RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_list(RZ_NONNULL RZ_BORROW RzList /*<void *>*/ *list, RZ_NULLABLE RzListFree qfree);
RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_pvector(RZ_NONNULL RZ_BORROW RzPVector /*<void *>*/ *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);
Expand Down
42 changes: 39 additions & 3 deletions librz/util/thread_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 /*<void *>*/ *list) {
RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_list(RZ_NONNULL RZ_BORROW RzList /*<void *>*/ *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) {
Expand All @@ -73,6 +79,36 @@ RZ_API RZ_OWN RzThreadQueue *rz_th_queue_new2(RZ_NONNULL RZ_OWN RzList /*<void *
return queue;
}

/**
* \brief Allocates and initializes a new fifo queue using a user-defined vector
*
* \param vector Pointer to the vector that will be used to initialize the queue.
* \param qfree Pointer to a custom free function to free the queue if not empty.
*
* \return On success returns a valid pointer, otherwise NULL
*/
RZ_API RZ_OWN RzThreadQueue *rz_th_queue_from_pvector(RZ_NONNULL RZ_BORROW RzPVector /*<void *>*/ *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
*
Expand Down

0 comments on commit a060e3b

Please sign in to comment.