Compare commits

...

2 Commits

Author SHA1 Message Date
Jef Roosens 5b2ce6acaa
feat(heap): thread-safety features 2023-01-27 21:23:32 +01:00
Jef Roosens d77b3e4fee
chore: some nitpicking 2023-01-27 20:50:23 +01:00
7 changed files with 51 additions and 10 deletions

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -2,7 +2,13 @@
#include <stdlib.h>
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);
}

View File

@ -1,6 +1,8 @@
#include "vieter_heap.h"
#include "vieter_heap_tree.h"
#include <pthread.h>
struct vieter_heap {
vieter_heap_node *tree;
pthread_rwlock_t lock;
};

View File

@ -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();