From d77b3e4fee699c3c6f6ee8adb7a04536d251354b Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 27 Jan 2023 20:48:51 +0100 Subject: [PATCH 1/2] chore: some nitpicking --- Makefile | 11 +++++++---- README.md | 4 ++-- include/vieter_cron.h | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index cf67ffa..04c6dd3 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ SRC_DIR ?= src TEST_DIR ?= test INC_DIRS ?= include +LIB := $(BUILD_DIR)/$(LIB_FILENAME) + SRCS != find '$(SRC_DIR)' -iname '*.c' SRCS_H != find $(INC_DIRS) -iname '*.h' SRCS_TEST != find '$(TEST_DIR)' -iname '*.c' @@ -40,7 +42,7 @@ all: vieter objs: $(OBJS) .PHONY: vieter -vieter: $(BUILD_DIR)/$(LIB_FILENAME) +vieter: $(LIB) $(BUILD_DIR)/$(LIB_FILENAME): $(OBJS) ar -rcs $@ $(OBJS) @@ -69,10 +71,11 @@ build-test: $(BINS_TEST) # For simplicity, we link every object file to each of the test files. This # might be changed later if this starts to become too slow. -$(BINS_TEST): %: %.c.o $(OBJS) - $(CC) $^ -o $@ +$(BINS_TEST): %: %.c.o $(LIB) + $(CC) \ + $^ -o $@ -# Allow with the include directory, each test includes $(TEST_DIR) (which +# Along with the include directory, each test includes $(TEST_DIR) (which # contains the acutest.h header file), and the src directory of the module it's # testing. This allows tests to access internal methods, which aren't publicly # exposed. diff --git a/README.md b/README.md index 0c33ab0..d1fef1a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ be implemented in C (or just parts I want to implement in C because it's fun). The goal of this library is to be as self-contained as possible; data structures should be implemented manually if possible. -See the [source code](/src) for the list of modules. +See the [source code](src) for the list of modules. ## Development @@ -43,7 +43,7 @@ only used in a .c file, the import should be placed in the .c file instead. This library uses [Acutest](https://github.com/mity/acutest) for its tests. Tests should be placed in the `test` subdirectory, further divided into -directories that correspond those in `src`. Test files should begin with +directories that correspond to those in `src`. Test files should begin with `test_`, and their format should follow the expected format for Acutest. Each `test_` is compiled separately into a binary, linked with libvieter. A diff --git a/include/vieter_cron.h b/include/vieter_cron.h index 81a2630..459a49f 100644 --- a/include/vieter_cron.h +++ b/include/vieter_cron.h @@ -37,7 +37,7 @@ typedef struct vieter_cron_simple_time { /* * Allocate and initialize a new empty cron expression. */ -vieter_cron_expression *ce_init(); +vieter_cron_expression *vieter_cron_expr_init(); /* * Deallocate a cron expression. From 5b2ce6acaa6cb9cc559eb9618be1eabda2e12356 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 27 Jan 2023 20:59:06 +0100 Subject: [PATCH 2/2] feat(heap): thread-safety features --- include/vieter_heap.h | 20 +++++++++++++++++++- src/heap/vieter_heap.c | 20 +++++++++++++++++++- src/heap/vieter_heap_internal.h | 2 ++ src/heap/vieter_heap_tree.h | 2 +- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/include/vieter_heap.h b/include/vieter_heap.h index 5d08ebc..ce6f3a4 100644 --- a/include/vieter_heap.h +++ b/include/vieter_heap.h @@ -11,7 +11,7 @@ typedef enum vieter_heap_error { } vieter_heap_error; /* - * Allocate and initalize an empty heap. + * Allocate and initialize an empty heap. */ vieter_heap *vieter_heap_init(); @@ -41,4 +41,22 @@ vieter_heap_error vieter_heap_pop(void **out, vieter_heap *heap); */ vieter_heap_error vieter_heap_peek(void **out, vieter_heap *heap); +/* + * Acquire a read lock on the heap. Return value is the result of + * pthread_rwlock_rdlock. + */ +int vieter_heap_rlock(vieter_heap *heap); + +/* + * Acquire a write lock on the heap. Return value is the result of + * pthread_rwlock_wrlock. + */ +int vieter_heap_wlock(vieter_heap *heap); + +/* + * Unlock the lock after having acquired it. Return value is the result of + * pthread_rwlock_unlock. + */ +int vieter_heap_unlock(vieter_heap *heap); + #endif diff --git a/src/heap/vieter_heap.c b/src/heap/vieter_heap.c index 9d4ef8e..b10d94c 100644 --- a/src/heap/vieter_heap.c +++ b/src/heap/vieter_heap.c @@ -2,7 +2,13 @@ #include -vieter_heap *vieter_heap_init() { return calloc(1, sizeof(vieter_heap)); } +vieter_heap *vieter_heap_init() { + vieter_heap *heap = calloc(1, sizeof(vieter_heap)); + + pthread_rwlock_init(&heap->lock, NULL); + + return heap; +} uint64_t vieter_heap_size(vieter_heap *heap) { uint64_t size = 0; @@ -76,3 +82,15 @@ vieter_heap_error vieter_heap_peek(void **out, vieter_heap *heap) { return vieter_heap_ok; } + +int vieter_heap_rlock(vieter_heap *heap) { + return pthread_rwlock_rdlock(&heap->lock); +} + +int vieter_heap_wlock(vieter_heap *heap) { + return pthread_rwlock_wrlock(&heap->lock); +} + +int vieter_heap_unlock(vieter_heap *heap) { + return pthread_rwlock_unlock(&heap->lock); +} diff --git a/src/heap/vieter_heap_internal.h b/src/heap/vieter_heap_internal.h index 8aaed53..41850e9 100644 --- a/src/heap/vieter_heap_internal.h +++ b/src/heap/vieter_heap_internal.h @@ -1,6 +1,8 @@ #include "vieter_heap.h" #include "vieter_heap_tree.h" +#include struct vieter_heap { vieter_heap_node *tree; + pthread_rwlock_t lock; }; diff --git a/src/heap/vieter_heap_tree.h b/src/heap/vieter_heap_tree.h index c3bc55d..0a299db 100644 --- a/src/heap/vieter_heap_tree.h +++ b/src/heap/vieter_heap_tree.h @@ -18,7 +18,7 @@ typedef struct vieter_heap_node { } vieter_heap_node; /* - * Allocate an initialize a heap node object. + * Allocate and initialize a heap node object. */ vieter_heap_node *vieter_heap_node_init();