Add rz_th_queue_from_pvector to initialize a queue from a pvector (#4069)

* Add rz_th_queue_from_pvector to initialize a queue from a pvector
* Rename methods to create queue.
This commit is contained in:
Giovanni 2024-01-05 21:40:25 +08:00 committed by GitHub
parent 77302c80b7
commit a060e3beb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 5 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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) {
@ -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
*