Skip to content

Commit

Permalink
array library uses mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed May 31, 2024
1 parent 570eb26 commit e11b705
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/ccode/archive-extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ static void archive_extract_fn(archive *data, char *path, bool all) {
unlink(target_file);
}
if (S_ISLNK(mode)) {
if(isdir(target_file)){
continue;
}
char *link_target = archive_entry_symlink(entry);
if (link_target != NULL) {
if (symlink(link_target, target_file) != 0) {
Expand Down
27 changes: 27 additions & 0 deletions src/ccode/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ array *array_new() {
arr->size = 0;
arr->capacity = 1024;
arr->removed = 0;
arr->lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
size_t start;
for(start=0;start<arr->capacity;start++){
arr->data[start] = NULL;
Expand All @@ -36,6 +37,7 @@ void array_add(array *arr, char *value) {
if(value == NULL){
return;
}
pthread_mutex_lock(&arr->lock);
if (arr->size >= arr->capacity-1) {
arr->capacity += 1024;
arr->data = (char **)realloc(arr->data, arr->capacity * sizeof(char *));
Expand All @@ -46,20 +48,24 @@ void array_add(array *arr, char *value) {
}
arr->size++;
arr->data[arr->size]=strdup(value);
pthread_mutex_unlock(&arr->lock);
}

void array_set(array *arr, char** new_data, size_t len){
pthread_mutex_lock(&arr->lock);
arr->data = calloc(arr->capacity, sizeof(char*));
arr->size = 0;
arr->removed = 0;
size_t start;
for(start=0;start<arr->capacity;start++){
arr->data[start] = NULL;
}
pthread_mutex_unlock(&arr->lock);
array_adds(arr, new_data, len);
}

char* array_get_string(array *arr){
pthread_mutex_lock(&arr->lock);
size_t tot_len = 0;
size_t start = 0;
while(start < arr->capacity){
Expand All @@ -76,6 +82,7 @@ char* array_get_string(array *arr){
}
start++;
}
pthread_mutex_unlock(&arr->lock);
return ret;
}

Expand All @@ -87,6 +94,7 @@ void array_adds(array *arr, char **value, size_t len) {
}

void array_remove(array* arr, char* item){
pthread_mutex_lock(&arr->lock);
size_t start = 0;
while(start < arr->capacity){
if(arr->data[start] != NULL && strcmp(arr->data[start],item)==0){
Expand All @@ -96,22 +104,27 @@ void array_remove(array* arr, char* item){
}
start++;
}
pthread_mutex_unlock(&arr->lock);
}

bool array_has(array* arr, char* name){
pthread_mutex_lock(&arr->lock);
size_t start = 0;
while(start < arr->size + arr->removed){
if(arr->data[start]){
if (strcmp(arr->data[start], name) == 0){
pthread_mutex_unlock(&arr->lock);
return true;
}
}
start++;
}
pthread_mutex_unlock(&arr->lock);
return false;
}

void array_uniq(array* arr){
pthread_mutex_lock(&arr->lock);
size_t start = 1;
size_t i = 0;
size_t removed = 0;
Expand All @@ -130,22 +143,27 @@ void array_uniq(array* arr){
}
arr->size -= removed;
arr->removed = removed;
pthread_mutex_unlock(&arr->lock);
}

void array_pop(array* arr, size_t index){
pthread_mutex_lock(&arr->lock);
arr->data[index] = NULL;
arr->size -= 1;
arr->removed +=1;
pthread_mutex_unlock(&arr->lock);
}


void array_insert(array* arr, char* value, size_t index){
pthread_mutex_lock(&arr->lock);
if (arr->size >= arr->capacity) {
array_add(arr,NULL);
}
if (arr->data[index] == NULL){
arr->data[index] = value;
arr->size++;
pthread_mutex_unlock(&arr->lock);
return;
}
char* tmp = strdup(arr->data[index]);
Expand All @@ -156,6 +174,7 @@ void array_insert(array* arr, char* value, size_t index){
if(arr->data[start] == NULL){
arr->data[start] = tmp;
arr->size+=1;
pthread_mutex_unlock(&arr->lock);
return;
}
tmp2 = strdup(arr->data[start]);
Expand All @@ -164,9 +183,11 @@ void array_insert(array* arr, char* value, size_t index){
start++;
}
arr->size += 1;
pthread_mutex_unlock(&arr->lock);
}

void array_sort(array* arr){
pthread_mutex_lock(&arr->lock);
char** new_data = (char**)calloc(arr->capacity,sizeof(char*));
size_t start = 0;
size_t skip = 0;
Expand All @@ -181,9 +202,11 @@ void array_sort(array* arr){
}
csort(new_data, arr->size);
arr->data = new_data;
pthread_mutex_unlock(&arr->lock);
}

char **array_get(array *arr, int* len) {
pthread_mutex_lock(&arr->lock);
*len = arr->size;
char** ret = calloc(arr->size+1, sizeof(char*));
size_t start = 0;
Expand All @@ -197,6 +220,7 @@ char **array_get(array *arr, int* len) {
ret[start-skip]=strdup(arr->data[start]);
start++;
}
pthread_mutex_unlock(&arr->lock);
return ret;
}

Expand All @@ -206,7 +230,9 @@ size_t array_length(array *arr) {


void array_reverse(array *arr) {
pthread_mutex_lock(&arr->lock);
if (arr->size <= 1) {
pthread_mutex_unlock(&arr->lock);
return; /* No need to reverse if size is 0 or 1 */
}

Expand All @@ -228,4 +254,5 @@ void array_reverse(array *arr) {
/* Move towards the center */
start++;
}
pthread_mutex_unlock(&arr->lock);
}
3 changes: 1 addition & 2 deletions src/ccode/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ static array *error_arr;

void error(int status){
if(!error_arr){
error_arr = array_new();
return;
}
size_t i;
Expand Down Expand Up @@ -51,7 +50,7 @@ void error_add(char* message) {

bool has_error(){
if(!error_arr){
error_arr = array_new();
return false;
}
return array_length(error_arr) > 0;
}
2 changes: 2 additions & 0 deletions src/include/array.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdbool.h>
#include <stddef.h>
#include <pthread.h>

#ifndef GETTEXT_PACKAGE
#define GETTEXT_PACKAGE "ymp"
Expand All @@ -13,6 +14,7 @@ typedef struct {
size_t size;
size_t capacity;
size_t removed;
pthread_mutex_t lock;
} array;

array* array_new();
Expand Down

0 comments on commit e11b705

Please sign in to comment.