refactor: drop pthreads on hashmap and queue

This commit is contained in:
2025-09-04 23:04:00 +02:00
parent 3297890967
commit 24f6ed0f84
4 changed files with 114 additions and 117 deletions

View File

@@ -1,21 +1,21 @@
#ifndef MYCLIB_QUEUE_H
#define MYCLIB_QUEUE_H
#include <pthread.h>
#include <stddef.h>
#include <threads.h>
/**
* @brief A simple circular queue (ring buffer).
*/
typedef struct mcl_queue_t {
size_t front; /**< Index of the next element to read. */
size_t rear; /**< Index where the next element will be written. */
size_t size; /**< Current number of elements in the queue. */
size_t capacity; /**< Maximum number of elements the queue can hold. */
size_t elem_size; /**< Size in bytes of each element. */
void *buffer; /**< Memory buffer that holds the elements. */
pthread_mutex_t lock; /**< Mutex to protect concurrent access. */
} mcl_queue;
typedef struct mcl_queue {
size_t front; /**< Index of the next element to read. */
size_t rear; /**< Index where the next element will be written. */
size_t size; /**< Current number of elements in the queue. */
size_t capacity; /**< Maximum number of elements the queue can hold. */
size_t elem_size; /**< Size in bytes of each element. */
void *buffer; /**< Memory buffer that holds the elements. */
mtx_t lock; /**< Mutex to protect concurrent access. */
} mcl_queue_s;
/**
* @brief Create and initialize a new queue.
@@ -24,7 +24,7 @@ typedef struct mcl_queue_t {
* @param elem_size Size in bytes of each element.
* @return Pointer to the new queue, or NULL on failure.
*/
mcl_queue *mcl_queue_init(size_t queue_size, size_t elem_size);
mcl_queue_s *mcl_queue_init(size_t queue_size, size_t elem_size);
/**
* @brief Add an element to the queue.
@@ -33,7 +33,7 @@ mcl_queue *mcl_queue_init(size_t queue_size, size_t elem_size);
* @param elem Pointer to the data to add.
* @return 0 on success, -1 if the queue is full or on error.
*/
int mcl_queue_push(mcl_queue *queue, const void *elem);
int mcl_queue_push(mcl_queue_s *queue, const void *elem);
/**
* @brief Remove an element from the queue.
@@ -42,7 +42,7 @@ int mcl_queue_push(mcl_queue *queue, const void *elem);
* @param out_elem Pointer to memory where the removed element will be copied.
* @return 0 on success, -1 if the queue is empty or on error.
*/
int mcl_queue_pop(mcl_queue *queue, void *out_elem);
int mcl_queue_pop(mcl_queue_s *queue, void *out_elem);
/**
* @brief Copy the front element without removing it.
@@ -51,7 +51,7 @@ int mcl_queue_pop(mcl_queue *queue, void *out_elem);
* @param out Pointer to memory where the element will be copied.
* @return 0 on success, -1 if the queue is empty or on error.
*/
int mcl_queue_get_front(mcl_queue *queue, void *out);
int mcl_queue_get_front(mcl_queue_s *queue, void *out);
/**
* @brief Copy the last element without removing it.
@@ -60,13 +60,13 @@ int mcl_queue_get_front(mcl_queue *queue, void *out);
* @param out Pointer to memory where the element will be copied.
* @return 0 on success, -1 if the queue is empty or on error.
*/
int mcl_queue_get_rear(mcl_queue *queue, void *out);
int mcl_queue_get_rear(mcl_queue_s *queue, void *out);
/**
* @brief Free all resources used by the queue.
*
* @param queue Pointer to the queue to free.
*/
void mcl_queue_free(mcl_queue *queue);
void mcl_queue_free(mcl_queue_s *queue);
#endif // MYCLIB_QUEUE_H