21
21
#include <reflect/reflect_function.h>
22
22
#include <reflect/reflect_value_type.h>
23
23
24
+ #include <threading/threading_atomic_ref_count.h>
25
+
24
26
#include <reflect/reflect_memory_tracker.h>
25
27
26
28
#include <log/log.h>
@@ -34,7 +36,7 @@ struct function_type
34
36
signature s ;
35
37
function_impl impl ;
36
38
function_interface interface ;
37
- size_t ref_count ;
39
+ struct threading_atomic_ref_count_type ref ;
38
40
enum async_id async ;
39
41
void * data ;
40
42
};
@@ -77,7 +79,6 @@ function function_create(const char *name, size_t args_count, function_impl impl
77
79
}
78
80
79
81
func -> impl = impl ;
80
- func -> ref_count = 0 ;
81
82
func -> async = SYNCHRONOUS ;
82
83
func -> data = NULL ;
83
84
@@ -87,12 +88,11 @@ function function_create(const char *name, size_t args_count, function_impl impl
87
88
{
88
89
log_write ("metacall" , LOG_LEVEL_ERROR , "Invalid function signature allocation" );
89
90
90
- free (func -> name );
91
- free (func );
92
-
93
- return NULL ;
91
+ goto function_create_error ;
94
92
}
95
93
94
+ threading_atomic_ref_count_store (& func -> ref , 0 );
95
+
96
96
func -> interface = singleton ? singleton () : NULL ;
97
97
98
98
if (func -> interface != NULL && func -> interface -> create != NULL )
@@ -101,16 +101,19 @@ function function_create(const char *name, size_t args_count, function_impl impl
101
101
{
102
102
log_write ("metacall" , LOG_LEVEL_ERROR , "Invalid function (%s) create callback <%p>" , func -> name , func -> interface -> create );
103
103
104
- free (func -> name );
105
- free (func );
106
-
107
- return NULL ;
104
+ goto function_create_error ;
108
105
}
109
106
}
110
107
111
108
reflect_memory_tracker_allocation (function_stats );
112
109
113
110
return func ;
111
+
112
+ function_create_error :
113
+ free (func -> name );
114
+ free (func );
115
+
116
+ return NULL ;
114
117
}
115
118
116
119
int function_increment_reference (function func )
@@ -120,12 +123,11 @@ int function_increment_reference(function func)
120
123
return 1 ;
121
124
}
122
125
123
- if (func -> ref_count == SIZE_MAX )
126
+ if (threading_atomic_ref_count_increment ( & func -> ref ) == 1 )
124
127
{
125
128
return 1 ;
126
129
}
127
130
128
- ++ func -> ref_count ;
129
131
reflect_memory_tracker_increment (function_stats );
130
132
131
133
return 0 ;
@@ -138,12 +140,11 @@ int function_decrement_reference(function func)
138
140
return 1 ;
139
141
}
140
142
141
- if (func -> ref_count == 0 )
143
+ if (threading_atomic_ref_count_decrement ( & func -> ref ) == 1 )
142
144
{
143
145
return 1 ;
144
146
}
145
147
146
- -- func -> ref_count ;
147
148
reflect_memory_tracker_decrement (function_stats );
148
149
149
150
return 0 ;
@@ -639,7 +640,7 @@ void function_destroy(function func)
639
640
log_write ("metacall" , LOG_LEVEL_ERROR , "Invalid reference counter in function: %s" , func -> name ? func -> name : "<anonymous>" );
640
641
}
641
642
642
- if (func -> ref_count == 0 )
643
+ if (threading_atomic_ref_count_load ( & func -> ref ) == 0 )
643
644
{
644
645
if (func -> name == NULL )
645
646
{
0 commit comments