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,18 +1,17 @@
#include "myhashmap.h" #include "myhashmap.h"
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static size_t mcl_get_mutex(mcl_hashmap *hashmap, size_t hash) { return hash % hashmap->num_locks; } static size_t mcl_get_mutex(mcl_hashmap_s *hashmap, size_t hash) { return hash % hashmap->num_locks; }
static size_t mcl_get_bucket_index(mcl_hashmap *hashmap, void *key) { static size_t mcl_get_bucket_index(mcl_hashmap_s *hashmap, void *key) {
unsigned int hash = hashmap->hash_fn(key); unsigned int hash = hashmap->hash_fn(key);
return hash % MYCLIB_HASHMAP_SIZE; return hash % MYCLIB_HASHMAP_SIZE;
} }
static void mcl_free_bucket_content(mcl_hashmap *hashmap, mcl_bucket *bucket) { static void mcl_free_bucket_content(mcl_hashmap_s *hashmap, mcl_bucket_s *bucket) {
if (bucket == NULL) { if (bucket == NULL) {
return; return;
} }
@@ -28,9 +27,9 @@ static void mcl_free_bucket_content(mcl_hashmap *hashmap, mcl_bucket *bucket) {
} }
} }
static mcl_bucket *mcl_find_bucket(mcl_hashmap *hashmap, void *key, mcl_bucket **prev) { static mcl_bucket_s *mcl_find_bucket(mcl_hashmap_s *hashmap, void *key, mcl_bucket_s **prev) {
size_t index = mcl_get_bucket_index(hashmap, key); size_t index = mcl_get_bucket_index(hashmap, key);
mcl_bucket *bucket = &hashmap->map[index]; mcl_bucket_s *bucket = &hashmap->map[index];
*prev = NULL; *prev = NULL;
@@ -51,9 +50,8 @@ static mcl_bucket *mcl_find_bucket(mcl_hashmap *hashmap, void *key, mcl_bucket *
return NULL; return NULL;
} }
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn, size_t key_size, mcl_hashmap_s *mcl_hm_init(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size) {
size_t value_size) { mcl_hashmap_s *hashmap = malloc(sizeof(mcl_hashmap_s));
mcl_hashmap *hashmap = malloc(sizeof(mcl_hashmap));
if (hashmap == NULL) { if (hashmap == NULL) {
return NULL; return NULL;
} }
@@ -66,7 +64,7 @@ mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_
hashmap->value_size = value_size; hashmap->value_size = value_size;
hashmap->num_locks = 64; hashmap->num_locks = 64;
hashmap->locks = malloc(sizeof(pthread_mutex_t) * hashmap->num_locks); hashmap->locks = malloc(sizeof(mtx_t) * hashmap->num_locks);
if (hashmap->locks == NULL) { if (hashmap->locks == NULL) {
free(hashmap); free(hashmap);
@@ -75,11 +73,11 @@ mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_
int ret; int ret;
for (size_t i = 0; i < hashmap->num_locks; ++i) { for (size_t i = 0; i < hashmap->num_locks; ++i) {
ret = pthread_mutex_init(&(hashmap->locks[i]), NULL); ret = mtx_init(&(hashmap->locks[i]), NULL);
if (ret != 0) { if (ret != thrd_success) {
/* Mutex failed */ /* Mutex failed */
for (size_t j = 0; j < i; ++j) { for (size_t j = 0; j < i; ++j) {
pthread_mutex_destroy(&(hashmap->locks[j])); mtx_destroy(&(hashmap->locks[j]));
} }
free(hashmap->locks); free(hashmap->locks);
@@ -92,14 +90,14 @@ mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_
return hashmap; return hashmap;
} }
void mcl_hm_free(mcl_hashmap *hashmap) { void mcl_hm_free(mcl_hashmap_s *hashmap) {
if (hashmap == NULL) { if (hashmap == NULL) {
return; return;
} }
/* Iterate through all buckets in the hash map */ /* Iterate through all buckets in the hash map */
for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) { for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) {
mcl_bucket *bucket = &hashmap->map[i]; mcl_bucket_s *bucket = &hashmap->map[i];
/* Free the first bucket if it contains data */ /* Free the first bucket if it contains data */
if (bucket->key != NULL) { if (bucket->key != NULL) {
@@ -109,7 +107,7 @@ void mcl_hm_free(mcl_hashmap *hashmap) {
/* Free all chained buckets */ /* Free all chained buckets */
bucket = bucket->next; bucket = bucket->next;
while (bucket != NULL) { while (bucket != NULL) {
mcl_bucket *next = bucket->next; mcl_bucket_s *next = bucket->next;
mcl_free_bucket_content(hashmap, bucket); mcl_free_bucket_content(hashmap, bucket);
free(bucket); free(bucket);
bucket = next; bucket = next;
@@ -118,7 +116,7 @@ void mcl_hm_free(mcl_hashmap *hashmap) {
/* Free the mutex */ /* Free the mutex */
for (size_t i = 0; i < hashmap->num_locks; ++i) { for (size_t i = 0; i < hashmap->num_locks; ++i) {
pthread_mutex_destroy(&(hashmap->locks[i])); mtx_destroy(&(hashmap->locks[i]));
} }
free(hashmap->locks); free(hashmap->locks);
@@ -126,7 +124,7 @@ void mcl_hm_free(mcl_hashmap *hashmap) {
free(hashmap); free(hashmap);
} }
void mcl_hm_free_bucket(mcl_bucket *bucket) { void mcl_hm_free_bucket(mcl_bucket_s *bucket) {
if (bucket == NULL) { if (bucket == NULL) {
return; return;
} }
@@ -136,17 +134,17 @@ void mcl_hm_free_bucket(mcl_bucket *bucket) {
free(bucket); free(bucket);
} }
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) { bool mcl_hm_set(mcl_hashmap_s *hashmap, void *key, void *value) {
if (hashmap == NULL || key == NULL || value == NULL) { if (hashmap == NULL || key == NULL || value == NULL) {
return false; return false;
} }
size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key)); size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key));
pthread_mutex_t *mutex = &(hashmap->locks[mutex_id]); mtx_t *mutex = &(hashmap->locks[mutex_id]);
pthread_mutex_lock(mutex); mtx_lock(mutex);
mcl_bucket *prev; mcl_bucket_s *prev;
mcl_bucket *existing = mcl_find_bucket(hashmap, key, &prev); mcl_bucket_s *existing = mcl_find_bucket(hashmap, key, &prev);
if (existing != NULL) { if (existing != NULL) {
/* Key exists, update value */ /* Key exists, update value */
@@ -156,26 +154,26 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
existing->value = malloc(hashmap->value_size); existing->value = malloc(hashmap->value_size);
if (existing->value == NULL) { if (existing->value == NULL) {
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return false; return false;
} }
memcpy(existing->value, value, hashmap->value_size); memcpy(existing->value, value, hashmap->value_size);
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return true; return true;
} }
/* Key doesn't exist, need to insert new bucket */ /* Key doesn't exist, need to insert new bucket */
size_t index = mcl_get_bucket_index(hashmap, key); size_t index = mcl_get_bucket_index(hashmap, key);
mcl_bucket *bucket = &hashmap->map[index]; mcl_bucket_s *bucket = &hashmap->map[index];
if (bucket->key == NULL) { if (bucket->key == NULL) {
/* First bucket is empty, use it */ /* First bucket is empty, use it */
bucket->key = malloc(hashmap->key_size); bucket->key = malloc(hashmap->key_size);
if (bucket->key == NULL) { if (bucket->key == NULL) {
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return false; return false;
} }
@@ -184,7 +182,7 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
if (bucket->value == NULL) { if (bucket->value == NULL) {
free(bucket->key); free(bucket->key);
bucket->key = NULL; bucket->key = NULL;
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return false; return false;
} }
@@ -192,15 +190,15 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
memcpy(bucket->key, key, hashmap->key_size); memcpy(bucket->key, key, hashmap->key_size);
memcpy(bucket->value, value, hashmap->value_size); memcpy(bucket->value, value, hashmap->value_size);
bucket->next = NULL; bucket->next = NULL;
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return true; return true;
} }
/* Create new bucket and insert at head of collision chain */ /* Create new bucket and insert at head of collision chain */
mcl_bucket *new_bucket = malloc(sizeof(mcl_bucket)); mcl_bucket_s *new_bucket = malloc(sizeof(mcl_bucket_s));
if (new_bucket == NULL) { if (new_bucket == NULL) {
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return false; return false;
} }
@@ -208,7 +206,7 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
new_bucket->key = malloc(hashmap->key_size); new_bucket->key = malloc(hashmap->key_size);
if (new_bucket->key == NULL) { if (new_bucket->key == NULL) {
free(new_bucket); free(new_bucket);
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return false; return false;
} }
@@ -217,7 +215,7 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
if (new_bucket->value == NULL) { if (new_bucket->value == NULL) {
free(new_bucket->key); free(new_bucket->key);
free(new_bucket); free(new_bucket);
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return false; return false;
} }
@@ -226,17 +224,18 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
memcpy(new_bucket->value, value, hashmap->value_size); memcpy(new_bucket->value, value, hashmap->value_size);
new_bucket->next = bucket->next; new_bucket->next = bucket->next;
bucket->next = new_bucket; bucket->next = new_bucket;
pthread_mutex_unlock(mutex);
mtx_unlock(mutex);
return true; return true;
} }
static mcl_bucket *mcl_get_bucket_copy(mcl_bucket *from, size_t key_size, size_t value_size) { static mcl_bucket_s *mcl_get_bucket_copy(mcl_bucket_s *from, size_t key_size, size_t value_size) {
mcl_bucket *copy = malloc(sizeof(mcl_bucket)); mcl_bucket_s *copy = malloc(sizeof(mcl_bucket_s));
if (copy == NULL) { if (copy == NULL) {
return NULL; return NULL;
} }
memcpy(copy, from, sizeof(mcl_bucket)); memcpy(copy, from, sizeof(mcl_bucket_s));
copy->key = malloc(key_size); copy->key = malloc(key_size);
if (copy->key == NULL) { if (copy->key == NULL) {
@@ -258,45 +257,45 @@ static mcl_bucket *mcl_get_bucket_copy(mcl_bucket *from, size_t key_size, size_t
return copy; return copy;
} }
mcl_bucket *mcl_hm_get(mcl_hashmap *hashmap, void *key) { mcl_bucket_s *mcl_hm_get(mcl_hashmap_s *hashmap, void *key) {
if (hashmap == NULL || key == NULL) { if (hashmap == NULL || key == NULL) {
return NULL; return NULL;
} }
size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key)); size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key));
pthread_mutex_t *mutex = &(hashmap->locks[mutex_id]); mtx_t *mutex = &(hashmap->locks[mutex_id]);
pthread_mutex_lock(mutex); mtx_lock(mutex);
mcl_bucket *prev; mcl_bucket_s *prev;
mcl_bucket *found = mcl_find_bucket(hashmap, key, &prev); mcl_bucket_s *found = mcl_find_bucket(hashmap, key, &prev);
if (found) { if (found) {
mcl_bucket *copy = mcl_get_bucket_copy(found, hashmap->key_size, hashmap->value_size); mcl_bucket_s *copy = mcl_get_bucket_copy(found, hashmap->key_size, hashmap->value_size);
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return copy; return copy;
} }
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return NULL; return NULL;
} }
bool mcl_hm_remove(mcl_hashmap *hashmap, void *key) { bool mcl_hm_remove(mcl_hashmap_s *hashmap, void *key) {
if (hashmap == NULL || key == NULL) { if (hashmap == NULL || key == NULL) {
return false; return false;
} }
size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key)); size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key));
pthread_mutex_t *mutex = &(hashmap->locks[mutex_id]); mtx_t *mutex = &(hashmap->locks[mutex_id]);
pthread_mutex_lock(mutex); mtx_lock(mutex);
mcl_bucket *prev; mcl_bucket_s *prev;
mcl_bucket *to_remove = mcl_find_bucket(hashmap, key, &prev); mcl_bucket_s *to_remove = mcl_find_bucket(hashmap, key, &prev);
if (to_remove == NULL) { if (to_remove == NULL) {
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return false; return false;
} }
@@ -309,7 +308,7 @@ bool mcl_hm_remove(mcl_hashmap *hashmap, void *key) {
/* Removing first bucket in chain */ /* Removing first bucket in chain */
if (to_remove->next != NULL) { if (to_remove->next != NULL) {
/* Move next bucket's content to first bucket and free the next bucket */ /* Move next bucket's content to first bucket and free the next bucket */
mcl_bucket *next_bucket = to_remove->next; mcl_bucket_s *next_bucket = to_remove->next;
to_remove->key = next_bucket->key; to_remove->key = next_bucket->key;
to_remove->value = next_bucket->value; to_remove->value = next_bucket->value;
to_remove->next = next_bucket->next; to_remove->next = next_bucket->next;
@@ -326,7 +325,7 @@ bool mcl_hm_remove(mcl_hashmap *hashmap, void *key) {
free(to_remove); free(to_remove);
} }
pthread_mutex_unlock(mutex); mtx_unlock(mutex);
return true; return true;
} }

View File

@@ -1,9 +1,9 @@
#ifndef MYCLIB_HASHMAP_H #ifndef MYCLIB_HASHMAP_H
#define MYCLIB_HASHMAP_H #define MYCLIB_HASHMAP_H
#include <threads.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <threads.h>
#define MYCLIB_HASHMAP_SIZE 1024 /**< Number of buckets in the hash map */ #define MYCLIB_HASHMAP_SIZE 1024 /**< Number of buckets in the hash map */
@@ -25,7 +25,7 @@ typedef struct mcl_bucket {
* @param[in] key Pointer to the key to hash * @param[in] key Pointer to the key to hash
* @return The computed hash as an unsigned integer * @return The computed hash as an unsigned integer
*/ */
typedef unsigned int mcl_hash_fn(const void *key); typedef unsigned int hash_f(const void *key);
/** /**
* @brief Function pointer type for a key comparison function * @brief Function pointer type for a key comparison function
@@ -34,21 +34,21 @@ typedef unsigned int mcl_hash_fn(const void *key);
* @param[in] key_b Pointer to the second key * @param[in] key_b Pointer to the second key
* @return true if the keys are considered equal, false otherwise * @return true if the keys are considered equal, false otherwise
*/ */
typedef bool mcl_equal_fn(const void *key_a, const void *key_b); typedef bool equal_f(const void *key_a, const void *key_b);
/** /**
* @brief Function pointer type for freeing a key * @brief Function pointer type for freeing a key
* *
* @param[in] key Pointer to the key to free * @param[in] key Pointer to the key to free
*/ */
typedef void mcl_free_key_fn(void *key); typedef void free_key_f(void *key);
/** /**
* @brief Function pointer type for freeing a value * @brief Function pointer type for freeing a value
* *
* @param[in] value Pointer to the value to free * @param[in] value Pointer to the value to free
*/ */
typedef void mcl_free_value_fn(void *value); typedef void free_value_f(void *value);
/** /**
* @brief Main structure representing the hash map * @brief Main structure representing the hash map
@@ -56,17 +56,17 @@ typedef void mcl_free_value_fn(void *value);
* Contains function pointers for hash computation, key comparison, * Contains function pointers for hash computation, key comparison,
* and memory management, along with the bucket array. * and memory management, along with the bucket array.
*/ */
typedef struct mcl_hashmap_t { typedef struct mcl_hashmap {
mcl_hash_fn *hash_fn; /**< Hash function */ hash_f *hash_fn; /**< Hash function */
mcl_equal_fn *equal_fn; /**< Equality comparison function */ equal_f *equal_fn; /**< Equality comparison function */
mcl_free_key_fn *free_key_fn; /**< Key deallocation function (optional) */ free_key_f *free_key_fn; /**< Key deallocation function (optional) */
mcl_free_value_fn *free_value_fn; /**< Value deallocation function (optional) */ free_value_f *free_value_fn; /**< Value deallocation function (optional) */
size_t key_size; /**< Size in bytes of the key */ size_t key_size; /**< Size in bytes of the key */
size_t value_size; /**< Size in bytes of the value */ size_t value_size; /**< Size in bytes of the value */
mcl_bucket map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */ mcl_bucket map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */
pthread_mutex_t *locks; /**< Mutex array */ mtx_t *locks; /**< Mutex array */
size_t num_locks; /**< Number of mutex */ size_t num_locks; /**< Number of mutex */
} mcl_hashmap; } mcl_hashmap_s;
/** /**
* @brief Initialize a new hash map with user-defined behavior functions * @brief Initialize a new hash map with user-defined behavior functions
@@ -83,8 +83,7 @@ typedef struct mcl_hashmap_t {
* @param[in] value_size Size in bytes of each value to be stored * @param[in] value_size Size in bytes of each value to be stored
* @return A pointer to the newly initialized hash map, or NULL on failure * @return A pointer to the newly initialized hash map, or NULL on failure
*/ */
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn, size_t key_size, mcl_hashmap *mcl_hm_init(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size);
size_t value_size);
/** /**
* @brief Free all resources used by the hash map * @brief Free all resources used by the hash map
@@ -94,14 +93,14 @@ mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_
* *
* @param[in] hashmap Pointer to the hash map to free * @param[in] hashmap Pointer to the hash map to free
*/ */
void mcl_hm_free(mcl_hashmap *hashmap); void mcl_hm_free(mcl_hashmap_s *hashmap);
/** /**
* @brief Free a bucket returned by mcl_hm_get() * @brief Free a bucket returned by mcl_hm_get()
* *
* @param[in] bucket Pointer to the bucket to free * @param[in] bucket Pointer to the bucket to free
*/ */
void mcl_hm_free_bucket(mcl_bucket *bucket); void mcl_hm_free_bucket(mcl_bucket_s *bucket);
/** /**
* @brief Insert or update a key-value pair in the hash map * @brief Insert or update a key-value pair in the hash map
@@ -115,7 +114,7 @@ void mcl_hm_free_bucket(mcl_bucket *bucket);
* @param[in] value Pointer to the value to insert (will be copied, must not be NULL) * @param[in] value Pointer to the value to insert (will be copied, must not be NULL)
* @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory allocation failure) * @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory allocation failure)
*/ */
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value); bool mcl_hm_set(mcl_hashmap_s *hashmap, void *key, void *value);
/** /**
* @brief Retrieve a bucket by key * @brief Retrieve a bucket by key
@@ -127,7 +126,7 @@ bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value);
* @param[in] key Pointer to the key to search for * @param[in] key Pointer to the key to search for
* @return Pointer to the copy of the bucket, to avoid race conditions, or NULL if not found or on invalid input * @return Pointer to the copy of the bucket, to avoid race conditions, or NULL if not found or on invalid input
*/ */
mcl_bucket *mcl_hm_get(mcl_hashmap *hashmap, void *key); mcl_bucket_s *mcl_hm_get(mcl_hashmap_s *hashmap, void *key);
/** /**
* @brief Remove a key-value pair from the hash map * @brief Remove a key-value pair from the hash map
@@ -139,6 +138,6 @@ mcl_bucket *mcl_hm_get(mcl_hashmap *hashmap, void *key);
* @param[in] key Pointer to the key to remove * @param[in] key Pointer to the key to remove
* @return true if the key was found and removed, false if not found or on invalid input * @return true if the key was found and removed, false if not found or on invalid input
*/ */
bool mcl_hm_remove(mcl_hashmap *hashmap, void *key); bool mcl_hm_remove(mcl_hashmap_s *hashmap, void *key);
#endif /* MYCLIB_HASHMAP_H */ #endif /* MYCLIB_HASHMAP_H */

View File

@@ -1,11 +1,10 @@
#include "myqueue.h" #include "myqueue.h"
#include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
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) {
mcl_queue *queue = malloc(sizeof(mcl_queue)); mcl_queue_s *queue = malloc(sizeof(mcl_queue_s));
if (queue == NULL) { if (queue == NULL) {
return NULL; return NULL;
} }
@@ -17,8 +16,8 @@ mcl_queue *mcl_queue_init(size_t queue_size, size_t elem_size) {
return NULL; return NULL;
} }
int ret = pthread_mutex_init(&queue->lock, NULL); int ret = mtx_init(&queue->lock, NULL);
if (ret != 0) { if (ret != thrd_success) {
free(queue->buffer); free(queue->buffer);
free(queue); free(queue);
@@ -34,15 +33,15 @@ mcl_queue *mcl_queue_init(size_t queue_size, size_t elem_size) {
return queue; return queue;
} }
int mcl_queue_push(mcl_queue *queue, const void *elem) { int mcl_queue_push(mcl_queue_s *queue, const void *elem) {
int ret = pthread_mutex_lock(&queue->lock); int ret = mtx_lock(&queue->lock);
if (ret != 0) { if (ret != thrd_success) {
return -1; return -1;
} }
if (queue->size == queue->capacity) { if (queue->size == queue->capacity) {
/* Queue full */ /* Queue full */
pthread_mutex_unlock(&queue->lock); mtx_unlock(&queue->lock);
return -1; return -1;
} }
@@ -54,20 +53,20 @@ int mcl_queue_push(mcl_queue *queue, const void *elem) {
queue->size++; queue->size++;
queue->rear = (queue->rear + 1) % queue->capacity; queue->rear = (queue->rear + 1) % queue->capacity;
pthread_mutex_unlock(&queue->lock); mtx_unlock(&queue->lock);
return 0; return 0;
} }
int mcl_queue_pop(mcl_queue *queue, void *out_elem) { int mcl_queue_pop(mcl_queue_s *queue, void *out_elem) {
int ret = pthread_mutex_lock(&queue->lock); int ret = mtx_lock(&queue->lock);
if (ret != 0) { if (ret != thrd_success) {
return -1; return -1;
} }
if (queue->size == 0) { if (queue->size == 0) {
/* Queue empty */ /* Queue empty */
pthread_mutex_unlock(&queue->lock); mtx_unlock(&queue->lock);
return -1; return -1;
} }
@@ -78,19 +77,19 @@ int mcl_queue_pop(mcl_queue *queue, void *out_elem) {
queue->front = (queue->front + 1) % queue->capacity; queue->front = (queue->front + 1) % queue->capacity;
queue->size--; queue->size--;
pthread_mutex_unlock(&queue->lock); mtx_unlock(&queue->lock);
return 0; return 0;
} }
int mcl_queue_get_front(mcl_queue *queue, void *out) { int mcl_queue_get_front(mcl_queue_s *queue, void *out) {
int ret = pthread_mutex_lock(&queue->lock); int ret = mtx_lock(&queue->lock);
if (ret != 0) { if (ret != thrd_success) {
return -1; return -1;
} }
if (queue->size == 0) { if (queue->size == 0) {
pthread_mutex_unlock(&queue->lock); mtx_unlock(&queue->lock);
return -1; return -1;
} }
@@ -98,19 +97,19 @@ int mcl_queue_get_front(mcl_queue *queue, void *out) {
void *front = (void *)queue->buffer + (queue->front * queue->elem_size); void *front = (void *)queue->buffer + (queue->front * queue->elem_size);
memcpy(out, front, queue->elem_size); memcpy(out, front, queue->elem_size);
pthread_mutex_unlock(&queue->lock); mtx_unlock(&queue->lock);
return 0; return 0;
} }
int mcl_queue_get_rear(mcl_queue *queue, void *out) { int mcl_queue_get_rear(mcl_queue_s *queue, void *out) {
int ret = pthread_mutex_lock(&queue->lock); int ret = mtx_lock(&queue->lock);
if (ret != 0) { if (ret != thrd_success) {
return -1; return -1;
} }
if (queue->size == 0) { if (queue->size == 0) {
pthread_mutex_unlock(&queue->lock); mtx_unlock(&queue->lock);
return -1; return -1;
} }
@@ -125,17 +124,17 @@ int mcl_queue_get_rear(mcl_queue *queue, void *out) {
void *rear = (void *)queue->buffer + (rear_index * queue->elem_size); void *rear = (void *)queue->buffer + (rear_index * queue->elem_size);
memcpy(out, rear, queue->elem_size); memcpy(out, rear, queue->elem_size);
pthread_mutex_unlock(&queue->lock); mtx_unlock(&queue->lock);
return 0; return 0;
} }
void mcl_queue_free(mcl_queue *queue) { void mcl_queue_free(mcl_queue_s *queue) {
if (queue == NULL) { if (queue == NULL) {
return; return;
} }
pthread_mutex_destroy(&queue->lock); mtx_destroy(&queue->lock);
free(queue->buffer); free(queue->buffer);
free(queue); free(queue);

View File

@@ -1,21 +1,21 @@
#ifndef MYCLIB_QUEUE_H #ifndef MYCLIB_QUEUE_H
#define MYCLIB_QUEUE_H #define MYCLIB_QUEUE_H
#include <pthread.h>
#include <stddef.h> #include <stddef.h>
#include <threads.h>
/** /**
* @brief A simple circular queue (ring buffer). * @brief A simple circular queue (ring buffer).
*/ */
typedef struct mcl_queue_t { typedef struct mcl_queue {
size_t front; /**< Index of the next element to read. */ size_t front; /**< Index of the next element to read. */
size_t rear; /**< Index where the next element will be written. */ size_t rear; /**< Index where the next element will be written. */
size_t size; /**< Current number of elements in the queue. */ size_t size; /**< Current number of elements in the queue. */
size_t capacity; /**< Maximum number of elements the queue can hold. */ size_t capacity; /**< Maximum number of elements the queue can hold. */
size_t elem_size; /**< Size in bytes of each element. */ size_t elem_size; /**< Size in bytes of each element. */
void *buffer; /**< Memory buffer that holds the elements. */ void *buffer; /**< Memory buffer that holds the elements. */
pthread_mutex_t lock; /**< Mutex to protect concurrent access. */ mtx_t lock; /**< Mutex to protect concurrent access. */
} mcl_queue; } mcl_queue_s;
/** /**
* @brief Create and initialize a new queue. * @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. * @param elem_size Size in bytes of each element.
* @return Pointer to the new queue, or NULL on failure. * @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. * @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. * @param elem Pointer to the data to add.
* @return 0 on success, -1 if the queue is full or on error. * @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. * @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. * @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. * @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. * @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. * @param out Pointer to memory where the element will be copied.
* @return 0 on success, -1 if the queue is empty or on error. * @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. * @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. * @param out Pointer to memory where the element will be copied.
* @return 0 on success, -1 if the queue is empty or on error. * @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. * @brief Free all resources used by the queue.
* *
* @param queue Pointer to the queue to free. * @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 #endif // MYCLIB_QUEUE_H