make string thread-safe
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "mystring.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -15,11 +16,21 @@ mcl_string *mcl_string_new(const char *text, long initial_capacity) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Init pthread mutex */
|
||||
int ret = pthread_mutex_init(&str->lock, NULL);
|
||||
if (ret != 0) {
|
||||
free(str);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Calculate size and capacity */
|
||||
str->size = strlen(text);
|
||||
size_t capacity = initial_capacity;
|
||||
|
||||
if (capacity != -1 && capacity - 1 < str->size) {
|
||||
free(str);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -50,9 +61,17 @@ int mcl_string_append(mcl_string *string, const char *text) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Lock resource */
|
||||
int ret = pthread_mutex_lock(&string->lock);
|
||||
if (ret != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Handle empty case */
|
||||
size_t text_len = strlen(text);
|
||||
if (text_len == 0) {
|
||||
pthread_mutex_unlock(&string->lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -64,6 +83,8 @@ int mcl_string_append(mcl_string *string, const char *text) {
|
||||
/* Reallocate the buffer */
|
||||
void *new_data = realloc(string->data, sizeof(char) * new_capacity);
|
||||
if (!new_data) {
|
||||
pthread_mutex_unlock(&string->lock);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -79,6 +100,12 @@ int mcl_string_append(mcl_string *string, const char *text) {
|
||||
string->size = new_size;
|
||||
string->data[string->size] = '\0';
|
||||
|
||||
/* Unlock resource */
|
||||
ret = pthread_mutex_unlock(&string->lock);
|
||||
if (ret != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -87,33 +114,68 @@ void mcl_string_free(mcl_string *string) {
|
||||
return;
|
||||
}
|
||||
|
||||
int ret = pthread_mutex_lock(&string->lock);
|
||||
if (ret != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (string->data) {
|
||||
free(string->data);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&string->lock);
|
||||
pthread_mutex_destroy(&string->lock);
|
||||
|
||||
free(string);
|
||||
}
|
||||
|
||||
size_t mcl_string_length(const mcl_string *string) {
|
||||
size_t mcl_string_length(mcl_string *string) {
|
||||
if (string == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return string->size;
|
||||
int ret = pthread_mutex_lock(&string->lock);
|
||||
if (ret != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t len = string->size;
|
||||
|
||||
pthread_mutex_unlock(&string->lock);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t mcl_string_capacity(const mcl_string *string) {
|
||||
size_t mcl_string_capacity(mcl_string *string) {
|
||||
if (string == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return string->capacity;
|
||||
int ret = pthread_mutex_lock(&string->lock);
|
||||
if (ret != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t cap = string->capacity;
|
||||
|
||||
pthread_mutex_unlock(&string->lock);
|
||||
|
||||
return cap;
|
||||
}
|
||||
|
||||
const char *mcl_string_cstr(const mcl_string *string) {
|
||||
const char *mcl_string_cstr(mcl_string *string) {
|
||||
if (string == NULL || string->data == NULL) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return string->data;
|
||||
int ret = pthread_mutex_lock(&string->lock);
|
||||
if (ret != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *data = string->data;
|
||||
|
||||
pthread_mutex_unlock(&string->lock);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user