refactor: drop pthreads for threads

This commit is contained in:
2025-09-04 22:37:06 +02:00
parent 3bfd7faa24
commit 8936243527
2 changed files with 27 additions and 28 deletions

View File

@@ -1,13 +1,12 @@
#include "mystring.h" #include "mystring.h"
#include <pthread.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/* Initialize Thread-Specific Data Keys */ /* Initialize Thread-Specific Data Keys */
static pthread_key_t buffer_key; static tss_t buffer_key;
static pthread_once_t buffer_once = PTHREAD_ONCE_INIT; static once_flag buffer_once = ONCE_FLAG_INIT;
typedef struct { typedef struct {
char *buf; char *buf;
@@ -23,7 +22,7 @@ static void buffer_destructor(void *buf) {
free(tb); free(tb);
} }
static void buffer_key_init(void) { pthread_key_create(&buffer_key, buffer_destructor); } static void buffer_key_init(void) { tss_create(&buffer_key, buffer_destructor); }
/* Dynamic String library */ /* Dynamic String library */
size_t mcl_next_power_two(size_t len) { size_t mcl_next_power_two(size_t len) {
@@ -79,7 +78,7 @@ mcl_string *mcl_string_new(const char *text, size_t initial_capacity) {
str->data[str->size] = '\0'; str->data[str->size] = '\0';
/* Init pthread mutex */ /* Init pthread mutex */
if (pthread_mutex_init(&str->lock, NULL) != 0) { if (mtx_init(&str->lock, NULL) != thrd_success) {
free(str->data); free(str->data);
free(str); free(str);
@@ -95,14 +94,14 @@ int mcl_string_append(mcl_string *string, const char *text) {
} }
/* Lock resource */ /* Lock resource */
if (pthread_mutex_lock(&string->lock) != 0) { if (mtx_lock(&string->lock) != thrd_success) {
return -1; return -1;
} }
/* Handle empty case */ /* Handle empty case */
size_t text_len = strlen(text); size_t text_len = strlen(text);
if (text_len == 0) { if (text_len == 0) {
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return 0; return 0;
} }
@@ -115,7 +114,7 @@ int mcl_string_append(mcl_string *string, const char *text) {
/* Reallocate the buffer */ /* Reallocate the buffer */
void *new_data = realloc(string->data, new_capacity); void *new_data = realloc(string->data, new_capacity);
if (!new_data) { if (!new_data) {
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return -1; return -1;
} }
@@ -129,7 +128,7 @@ int mcl_string_append(mcl_string *string, const char *text) {
string->size = new_size; string->size = new_size;
string->data[string->size] = '\0'; string->data[string->size] = '\0';
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return 0; return 0;
} }
@@ -143,7 +142,7 @@ void mcl_string_free(mcl_string *string) {
free(string->data); free(string->data);
} }
pthread_mutex_destroy(&string->lock); mtx_destroy(&string->lock);
free(string); free(string);
} }
@@ -153,13 +152,13 @@ size_t mcl_string_length(mcl_string *string) {
return 0; return 0;
} }
if (pthread_mutex_lock(&string->lock) != 0) { if (mtx_lock(&string->lock) != thrd_success) {
return 0; return 0;
} }
size_t len = string->size; size_t len = string->size;
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return len; return len;
} }
@@ -169,13 +168,13 @@ size_t mcl_string_capacity(mcl_string *string) {
return 0; return 0;
} }
if (pthread_mutex_lock(&string->lock) != 0) { if (mtx_lock(&string->lock) != thrd_success) {
return 0; return 0;
} }
size_t cap = string->capacity; size_t cap = string->capacity;
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return cap; return cap;
} }
@@ -185,19 +184,19 @@ char *mcl_string_cstr(mcl_string *string) {
return NULL; return NULL;
} }
pthread_once(&buffer_once, buffer_key_init); call_once(&buffer_once, buffer_key_init);
if (pthread_mutex_lock(&string->lock) != 0) { if (mtx_lock(&string->lock) != thrd_success) {
return NULL; return NULL;
} }
size_t need = string->size + 1; size_t need = string->size + 1;
tl_buffer_t *tb = (tl_buffer_t *)pthread_getspecific(buffer_key); tl_buffer_t *tb = (tl_buffer_t *)tss_get(buffer_key);
if (tb == NULL) { if (tb == NULL) {
tb = malloc(sizeof(*tb)); tb = malloc(sizeof(*tb));
if (tb == NULL) { if (tb == NULL) {
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return NULL; return NULL;
} }
@@ -206,15 +205,15 @@ char *mcl_string_cstr(mcl_string *string) {
tb->buf = malloc(tb->cap); tb->buf = malloc(tb->cap);
if (tb->buf == NULL) { if (tb->buf == NULL) {
free(tb); free(tb);
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return NULL; return NULL;
} }
if (pthread_setspecific(buffer_key, tb) != 0) { if (tss_set(buffer_key, tb) != thrd_success) {
free(tb->buf); free(tb->buf);
free(tb); free(tb);
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return NULL; return NULL;
} }
@@ -222,7 +221,7 @@ char *mcl_string_cstr(mcl_string *string) {
size_t newcap = mcl_next_power_two(need); size_t newcap = mcl_next_power_two(need);
char *tmp = realloc(tb->buf, newcap); char *tmp = realloc(tb->buf, newcap);
if (tmp == NULL) { if (tmp == NULL) {
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return NULL; return NULL;
} }
@@ -233,7 +232,7 @@ char *mcl_string_cstr(mcl_string *string) {
memcpy(tb->buf, string->data, need); memcpy(tb->buf, string->data, need);
pthread_mutex_unlock(&string->lock); mtx_unlock(&string->lock);
return tb->buf; return tb->buf;
} }

View File

@@ -1,8 +1,8 @@
#ifndef MYCLIB_STRING_H #ifndef MYCLIB_STRING_H
#define MYCLIB_STRING_H #define MYCLIB_STRING_H
#include <pthread.h>
#include <stddef.h> #include <stddef.h>
#include <threads.h>
/** /**
* @brief Thread-safe dynamic string structure. * @brief Thread-safe dynamic string structure.
@@ -11,7 +11,7 @@ typedef struct mcl_string {
char *data; /**< Pointer to string data (null-terminated) */ char *data; /**< Pointer to string data (null-terminated) */
size_t size; /**< Current length of the string (excluding null terminator) */ size_t size; /**< Current length of the string (excluding null terminator) */
size_t capacity; /**< Allocated capacity including null terminator */ size_t capacity; /**< Allocated capacity including null terminator */
pthread_mutex_t lock; /**< Mutex for thread safety */ mtx_t lock; /**< Mutex for thread safety */
} mcl_string; } mcl_string;
/** /**