aboutsummaryrefslogtreecommitdiff
path: root/src/std
diff options
context:
space:
mode:
Diffstat (limited to 'src/std')
-rw-r--r--src/std/scoped_allocator.c39
-rw-r--r--src/std/thread.c6
2 files changed, 38 insertions, 7 deletions
diff --git a/src/std/scoped_allocator.c b/src/std/scoped_allocator.c
index c66639a..3104fd3 100644
--- a/src/std/scoped_allocator.c
+++ b/src/std/scoped_allocator.c
@@ -1,5 +1,6 @@
#include "../../include/std/scoped_allocator.h"
#include "../../include/std/alloc.h"
+#include "../../include/std/thread.h"
#include "../../include/std/log.h"
#include <assert.h>
@@ -26,7 +27,8 @@ void scoped_allocator_node_deinit(ScopedAllocatorNode *self) {
int scoped_allocator_init(ScopedAllocator *self) {
return_if_error(scoped_allocator_node_init(&self->head));
self->current = &self->head;
- return buffer_init(&self->buffers, NULL);
+ return_if_error(buffer_init(&self->buffers, NULL));
+ return buffer_init(&self->mutexes, NULL);
}
static void buffer_deinit(Buffer *self) {
@@ -36,11 +38,9 @@ static void buffer_deinit(Buffer *self) {
self->capacity = 0;
}
-void scoped_allocator_deinit(ScopedAllocator *self) {
+static void scoped_allocator_deinit_buffers(ScopedAllocator *self) {
Buffer **buffer;
Buffer **buffers_end;
-
- self->current = NULL;
buffer = buffer_start(&self->buffers);
buffers_end = buffer_end(&self->buffers);
while(buffer != buffers_end) {
@@ -48,6 +48,24 @@ void scoped_allocator_deinit(ScopedAllocator *self) {
++buffer;
}
buffer_deinit(&self->buffers);
+}
+
+static void scoped_allocator_deinit_mutexes(ScopedAllocator *self) {
+ amal_mutex **mutex;
+ amal_mutex **mutexes_end;
+ mutex = buffer_start(&self->mutexes);
+ mutexes_end = buffer_end(&self->mutexes);
+ while(mutex != mutexes_end) {
+ amal_mutex_deinit(*mutex);
+ ++mutex;
+ }
+ buffer_deinit(&self->mutexes);
+}
+
+void scoped_allocator_deinit(ScopedAllocator *self) {
+ self->current = NULL;
+ scoped_allocator_deinit_buffers(self);
+ scoped_allocator_deinit_mutexes(self);
scoped_allocator_node_deinit(&self->head);
}
@@ -79,6 +97,8 @@ static usize align_ptr_ceil_offset(void *ptr, uintptr_t alignment) {
return (uintptr_t)align_ptr_ceil(ptr, alignment) - (uintptr_t)ptr;
}
+#define SCOPED_ALLOC_ALIGNMENT 8
+
int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) {
ScopedAllocatorNode *current;
usize alloc_size;
@@ -90,14 +110,14 @@ int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) {
return -1;
}
- alloc_size = size + align_ptr_ceil_offset(self->current->data + self->current->size, 16);
+ alloc_size = size + align_ptr_ceil_offset(self->current->data + self->current->size, SCOPED_ALLOC_ALIGNMENT);
return_if_error(scoped_allocator_ensure_capacity_for(self, alloc_size));
/* Reallocated (new node created) */
if(self->current != current) {
*mem = self->current->data;
self->current->size += size;
} else {
- *mem = align_ptr_ceil(self->current->data + self->current->size, 16);
+ *mem = align_ptr_ceil(self->current->data + self->current->size, SCOPED_ALLOC_ALIGNMENT);
self->current->size += alloc_size;
}
return 0;
@@ -106,3 +126,10 @@ int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) {
int scoped_allocator_add_buffer(ScopedAllocator *self, Buffer *buffer) {
return buffer_append(&self->buffers, &buffer, sizeof(Buffer*));
}
+
+int scoped_allocator_create_mutex(ScopedAllocator *self, amal_mutex **mutex) {
+ *mutex = NULL;
+ return_if_error(scoped_allocator_alloc(self, sizeof(amal_mutex), (void**)mutex));
+ amal_mutex_init(*mutex);
+ return buffer_append(&self->mutexes, mutex, sizeof(amal_mutex*));
+}
diff --git a/src/std/thread.c b/src/std/thread.c
index ba67463..350631a 100644
--- a/src/std/thread.c
+++ b/src/std/thread.c
@@ -109,7 +109,9 @@ int amal_thread_join(amal_thread *self, void **result) {
void amal_mutex_init(amal_mutex *self) {
pthread_mutex_init(&self->mutex, NULL);
+ #ifdef AMAL_MUTEX_DEBUG
self->lock_identifier = NULL;
+ #endif
self->locked = bool_false;
self->owner_thread = 0;
}
@@ -129,10 +131,11 @@ static long amal_thread_get_id() {
int amal_mutex_lock(amal_mutex *self, const char *lock_identifier) {
int result;
result = pthread_mutex_lock(&self->mutex);
- self->lock_identifier = lock_identifier;
self->owner_thread = pthread_self();
self->locked = bool_true;
+ (void)lock_identifier;
#ifdef AMAL_MUTEX_DEBUG
+ self->lock_identifier = lock_identifier;
if(result == 0 && self->lock_identifier) {
amal_log_debug("amal_mutex_lock: mutex locked by thread %lu (%s), identification: %s",
amal_thread_get_id(),
@@ -149,6 +152,7 @@ int amal_mutex_unlock(amal_mutex *self) {
const char *identifier;
identifier = self->lock_identifier;
#endif
+ /* No-op if mutex isn't locked or if another thread owns the mutex lock */
if(!self->locked || pthread_equal(self->owner_thread, pthread_self()) == 0)
return 0;
self->locked = bool_false;