Skip to content

Commit

Permalink
Add atomic reference counting for reflect objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Jan 5, 2023
1 parent 5494aa3 commit 4f68815
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 38 deletions.
14 changes: 7 additions & 7 deletions source/reflect/source/reflect_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include <reflect/reflect_accessor.h>

#include <threading/threading_atomic_ref_count.h>

#include <reflect/reflect_memory_tracker.h>

#include <log/log.h>
Expand All @@ -40,7 +42,7 @@ struct class_type
enum accessor_type_id accessor;
class_impl impl;
class_interface interface;
size_t ref_count;
struct threading_atomic_ref_count_type ref;
vector constructors;
map methods;
map static_methods;
Expand Down Expand Up @@ -106,7 +108,7 @@ klass class_create(const char *name, enum accessor_type_id accessor, class_impl

cls->impl = impl;
cls->accessor = accessor;
cls->ref_count = 0;
threading_atomic_ref_count_store(&cls->ref, 0);
cls->interface = singleton ? singleton() : NULL;
cls->constructors = vector_create_type(constructor);
cls->methods = map_create(&hash_callback_str, &comparable_callback_str);
Expand Down Expand Up @@ -144,12 +146,11 @@ int class_increment_reference(klass cls)
return 1;
}

if (cls->ref_count == SIZE_MAX)
if (threading_atomic_ref_count_increment(&cls->ref) == 1)
{
return 1;
}

++cls->ref_count;
reflect_memory_tracker_increment(class_stats);

return 0;
Expand All @@ -162,12 +163,11 @@ int class_decrement_reference(klass cls)
return 1;
}

if (cls->ref_count == 0)
if (threading_atomic_ref_count_decrement(&cls->ref) == 1)
{
return 1;
}

--cls->ref_count;
reflect_memory_tracker_decrement(class_stats);

return 0;
Expand Down Expand Up @@ -843,7 +843,7 @@ void class_destroy(klass cls)
log_write("metacall", LOG_LEVEL_ERROR, "Invalid reference counter in class: %s", cls->name ? cls->name : "<anonymous>");
}

if (cls->ref_count == 0)
if (threading_atomic_ref_count_load(&cls->ref) == 0)
{
if (cls->name == NULL)
{
Expand Down
17 changes: 9 additions & 8 deletions source/reflect/source/reflect_exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <reflect/reflect_exception.h>

#include <threading/threading_atomic_ref_count.h>
#include <threading/threading_thread_id.h>

#include <reflect/reflect_memory_tracker.h>
Expand All @@ -36,7 +37,7 @@ struct exception_type
int64_t code; /* Numeric code of error */
char *stacktrace; /* Stack trace of the error */
uint64_t id; /* Thread id where the error was raised */
size_t ref_count;
struct threading_atomic_ref_count_type ref;
/* TODO: value attributes; // This should implement a map for representing the extra attributes of an exception */
};

Expand All @@ -56,7 +57,8 @@ exception exception_create(char *message, char *label, int64_t code, char *stack
ex->code = code;
ex->stacktrace = stacktrace;
ex->id = thread_id_get_current();
ex->ref_count = 0;

threading_atomic_ref_count_store(&ex->ref, 0);

reflect_memory_tracker_allocation(exception_stats);

Expand Down Expand Up @@ -128,7 +130,8 @@ exception exception_create_const(const char *message, const char *label, int64_t

ex->code = code;
ex->id = thread_id_get_current();
ex->ref_count = 0;

threading_atomic_ref_count_store(&ex->ref, 0);

reflect_memory_tracker_allocation(exception_stats);

Expand All @@ -151,12 +154,11 @@ int exception_increment_reference(exception ex)
return 1;
}

if (ex->ref_count == SIZE_MAX)
if (threading_atomic_ref_count_increment(&ex->ref) == 1)
{
return 1;
}

++ex->ref_count;
reflect_memory_tracker_increment(exception_stats);

return 0;
Expand All @@ -169,12 +171,11 @@ int exception_decrement_reference(exception ex)
return 1;
}

if (ex->ref_count == 0)
if (threading_atomic_ref_count_decrement(&ex->ref) == 1)
{
return 1;
}

--ex->ref_count;
reflect_memory_tracker_decrement(exception_stats);

return 0;
Expand Down Expand Up @@ -234,7 +235,7 @@ void exception_destroy(exception ex)
log_write("metacall", LOG_LEVEL_ERROR, "Invalid reference counter in exception: %s", ex->label ? ex->label : "<anonymous>");
}

if (ex->ref_count == 0)
if (threading_atomic_ref_count_load(&ex->ref) == 0)
{
if (ex->message != NULL)
{
Expand Down
31 changes: 16 additions & 15 deletions source/reflect/source/reflect_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <reflect/reflect_function.h>
#include <reflect/reflect_value_type.h>

#include <threading/threading_atomic_ref_count.h>

#include <reflect/reflect_memory_tracker.h>

#include <log/log.h>
Expand All @@ -34,7 +36,7 @@ struct function_type
signature s;
function_impl impl;
function_interface interface;
size_t ref_count;
struct threading_atomic_ref_count_type ref;
enum async_id async;
void *data;
};
Expand Down Expand Up @@ -77,7 +79,6 @@ function function_create(const char *name, size_t args_count, function_impl impl
}

func->impl = impl;
func->ref_count = 0;
func->async = SYNCHRONOUS;
func->data = NULL;

Expand All @@ -87,12 +88,11 @@ function function_create(const char *name, size_t args_count, function_impl impl
{
log_write("metacall", LOG_LEVEL_ERROR, "Invalid function signature allocation");

free(func->name);
free(func);

return NULL;
goto function_create_error;
}

threading_atomic_ref_count_store(&func->ref, 0);

func->interface = singleton ? singleton() : NULL;

if (func->interface != NULL && func->interface->create != NULL)
Expand All @@ -101,16 +101,19 @@ function function_create(const char *name, size_t args_count, function_impl impl
{
log_write("metacall", LOG_LEVEL_ERROR, "Invalid function (%s) create callback <%p>", func->name, func->interface->create);

free(func->name);
free(func);

return NULL;
goto function_create_error;
}
}

reflect_memory_tracker_allocation(function_stats);

return func;

function_create_error:
free(func->name);
free(func);

return NULL;
}

int function_increment_reference(function func)
Expand All @@ -120,12 +123,11 @@ int function_increment_reference(function func)
return 1;
}

if (func->ref_count == SIZE_MAX)
if (threading_atomic_ref_count_increment(&func->ref) == 1)
{
return 1;
}

++func->ref_count;
reflect_memory_tracker_increment(function_stats);

return 0;
Expand All @@ -138,12 +140,11 @@ int function_decrement_reference(function func)
return 1;
}

if (func->ref_count == 0)
if (threading_atomic_ref_count_decrement(&func->ref) == 1)
{
return 1;
}

--func->ref_count;
reflect_memory_tracker_decrement(function_stats);

return 0;
Expand Down Expand Up @@ -639,7 +640,7 @@ void function_destroy(function func)
log_write("metacall", LOG_LEVEL_ERROR, "Invalid reference counter in function: %s", func->name ? func->name : "<anonymous>");
}

if (func->ref_count == 0)
if (threading_atomic_ref_count_load(&func->ref) == 0)
{
if (func->name == NULL)
{
Expand Down
15 changes: 8 additions & 7 deletions source/reflect/source/reflect_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include <reflect/reflect_memory_tracker.h>

#include <threading/threading_atomic_ref_count.h>

#include <log/log.h>

#include <stdlib.h>
Expand All @@ -38,7 +40,7 @@ struct object_type
enum accessor_type_id accessor;
object_impl impl;
object_interface interface;
size_t ref_count;
struct threading_atomic_ref_count_type ref;
klass cls;
};

Expand Down Expand Up @@ -79,7 +81,8 @@ object object_create(const char *name, enum accessor_type_id accessor, object_im

obj->impl = impl;
obj->accessor = accessor;
obj->ref_count = 0;
threading_atomic_ref_count_store(&obj->ref, 0);

obj->interface = singleton ? singleton() : NULL;

obj->cls = cls;
Expand Down Expand Up @@ -109,12 +112,11 @@ int object_increment_reference(object obj)
return 1;
}

if (obj->ref_count == SIZE_MAX)
if (threading_atomic_ref_count_increment(&obj->ref) == 1)
{
return 1;
}

++obj->ref_count;
reflect_memory_tracker_increment(object_stats);

return 0;
Expand All @@ -127,12 +129,11 @@ int object_decrement_reference(object obj)
return 1;
}

if (obj->ref_count == 0)
if (threading_atomic_ref_count_decrement(&obj->ref) == 1)
{
return 1;
}

--obj->ref_count;
reflect_memory_tracker_decrement(object_stats);

return 0;
Expand Down Expand Up @@ -393,7 +394,7 @@ void object_destroy(object obj)
log_write("metacall", LOG_LEVEL_ERROR, "Invalid reference counter in object: %s", obj->name ? obj->name : "<anonymous>");
}

if (obj->ref_count == 0)
if (threading_atomic_ref_count_load(&obj->ref) == 0)
{
if (obj->name == NULL)
{
Expand Down
1 change: 1 addition & 0 deletions source/threading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(headers
${include_path}/threading.h
${include_path}/threading_atomic.h
${include_path}/threading_thread_id.h
${include_path}/threading_atomic_ref_count.h
)

set(sources
Expand Down
Loading

0 comments on commit 4f68815

Please sign in to comment.