Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rz_th_queue_new3 to initialize a queue from a pvector #4069

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading