Skip to content

Commit c0973ab

Browse files
committed
fix: resolved issue with GC-managed object creation in a fully multithreaded environment
1 parent ec79d20 commit c0973ab

14 files changed

+37
-23
lines changed

argon/lang/parser2/node/node.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,7 @@ const argon::vm::datatype::TypeInfo *argon::lang::parser2::ExtName = &alias##Ast
269269
T *NewNode(const TypeInfo *t_info, bool gc, NodeType node_type) {
270270
T *node;
271271

272-
if (!gc)
273-
node = MakeObject<T>(t_info);
274-
else
275-
node = MakeGCObject<T>(t_info, false);
272+
node = !gc ? MakeObject<T>(t_info) : MakeGCObject<T>(t_info);
276273

277274
if (node == nullptr)
278275
return nullptr;
@@ -283,6 +280,9 @@ const argon::vm::datatype::TypeInfo *argon::lang::parser2::ExtName = &alias##Ast
283280

284281
*((NodeType *) n_obj) = node_type;
285282

283+
if (gc)
284+
argon::vm::memory::Track((ArObject *) node);
285+
286286
return node;
287287
}
288288

argon/vm/datatype/arobject.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ namespace argon::vm::datatype {
111111
}
112112

113113
template<typename T>
114-
T *MakeGCObject(const TypeInfo *type, bool track) {
115-
return (T *) memory::GCNew(type, track);
114+
T *MakeGCObject(const TypeInfo *type) {
115+
return (T *) memory::GCNew(type, false);
116116
}
117117

118118
template<typename T>
119-
T *MakeGCObject(TypeInfo *type, bool track) {
120-
auto *ret = (T *) memory::GCNew(type, track);
119+
T *MakeGCObject(TypeInfo *type) {
120+
auto *ret = (T *) memory::GCNew(type, false);
121121
if (ret != nullptr)
122122
IncRef(type);
123123

argon/vm/datatype/chan.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ bool argon::vm::datatype::ChanWrite(Chan *chan, ArObject *value) {
270270
}
271271

272272
Chan *argon::vm::datatype::ChanNew(ArObject *defval, unsigned int backlog) {
273-
auto *chan = MakeGCObject<Chan>(type_chan_, false);
273+
auto *chan = MakeGCObject<Chan>(type_chan_);
274274

275275
if (chan != nullptr) {
276276
if ((chan->queue = (ArObject **) argon::vm::memory::Alloc(backlog * sizeof(void *))) == nullptr) {

argon/vm/datatype/dict.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ ArObject *dict_compare(Dict *self, ArObject *other, CompareMode mode) {
277277
}
278278

279279
ArObject *dict_iter(Dict *self, bool reverse) {
280-
auto *li = MakeGCObject<DictIterator>(type_dict_iterator_, true);
280+
auto *li = MakeGCObject<DictIterator>(type_dict_iterator_);
281281

282282
if (li != nullptr) {
283283
std::shared_lock _(self->rwlock);
@@ -291,6 +291,8 @@ ArObject *dict_iter(Dict *self, bool reverse) {
291291
li->cursor->ref++;
292292

293293
li->reverse = reverse;
294+
295+
argon::vm::memory::Track((ArObject*)li);
294296
}
295297

296298
return (ArObject *) li;
@@ -631,7 +633,7 @@ Dict *argon::vm::datatype::DictMerge(Dict *dict1, Dict *dict2, bool clone) {
631633
}
632634

633635
Dict *argon::vm::datatype::DictNew() {
634-
auto *dict = MakeGCObject<Dict>(type_dict_, false);
636+
auto *dict = MakeGCObject<Dict>(type_dict_);
635637

636638
if (dict != nullptr) {
637639
if (!dict->hmap.Initialize()) {
@@ -719,7 +721,7 @@ Dict *argon::vm::datatype::DictNew(ArObject *object) {
719721
}
720722

721723
Dict *argon::vm::datatype::DictNew(unsigned int size) {
722-
auto *dict = MakeGCObject<Dict>(type_dict_, false);
724+
auto *dict = MakeGCObject<Dict>(type_dict_);
723725

724726
if (dict != nullptr) {
725727
if (!dict->hmap.Initialize(size)) {

argon/vm/datatype/function.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ bool FunctionCheckParams(const PCheck *pcheck, ArObject **args, ArSize count) {
186186
}
187187

188188
Function *FunctionClone(const Function *func) {
189-
auto *fn = MakeGCObject<Function>(type_function_, false);
189+
auto *fn = MakeGCObject<Function>(type_function_);
190190

191191
if (fn != nullptr) {
192192
if (func->IsNative())
@@ -213,7 +213,7 @@ Function *FunctionClone(const Function *func) {
213213
}
214214

215215
Function *FunctionNew(String *name, String *doc, unsigned short arity, FunctionFlags flags) {
216-
auto *fn = MakeGCObject<Function>(type_function_, false);
216+
auto *fn = MakeGCObject<Function>(type_function_);
217217

218218
if (fn != nullptr) {
219219
fn->name = IncRef(name);

argon/vm/datatype/future.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ bool argon::vm::datatype::FutureAWait(Future *future) {
8080
}
8181

8282
Future *argon::vm::datatype::FutureNew() {
83-
auto *future = MakeGCObject<Future>(type_future_, false);
83+
auto *future = MakeGCObject<Future>(type_future_);
8484

8585
if (future != nullptr) {
8686
future->value = nullptr;

argon/vm/datatype/iterator.h

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ namespace argon::vm::datatype {
6060
}
6161

6262
inline void IteratorTrace(IteratorGeneric *self, Void_UnaryOp trace){
63+
std::unique_lock _(self->lock);
64+
6365
trace(self->iterable);
6466
}
6567

argon/vm/datatype/list.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,16 @@ ArObject *list_compare(List *self, ArObject *other, CompareMode mode) {
499499
}
500500

501501
ArObject *list_iter(List *self, bool reverse) {
502-
auto *li = MakeGCObject<ListIterator>(type_list_iterator_, true);
502+
auto *li = MakeGCObject<ListIterator>(type_list_iterator_);
503503

504504
if (li != nullptr) {
505505
new(&li->lock)std::mutex;
506506

507507
li->iterable = IncRef(self);
508508
li->index = 0;
509509
li->reverse = reverse;
510+
511+
argon::vm::memory::Track((ArObject *) li);
510512
}
511513

512514
return (ArObject *) li;
@@ -817,7 +819,7 @@ List *ListFromIterable(List **dest, ArObject *iterable) {
817819
}
818820

819821
List *argon::vm::datatype::ListNew(ArSize capacity) {
820-
auto *list = MakeGCObject<List>(type_list_, false);
822+
auto *list = MakeGCObject<List>(type_list_);
821823

822824
if (list != nullptr) {
823825
list->objects = nullptr;

argon/vm/datatype/module.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ Module *argon::vm::datatype::ModuleNew(const ModuleInit *init) {
347347
}
348348

349349
Module *argon::vm::datatype::ModuleNew(String *name, String *doc) {
350-
auto *mod = MakeGCObject<Module>(type_module_, true);
350+
auto *mod = MakeGCObject<Module>(type_module_);
351351

352352
if (mod != nullptr) {
353353
mod->fini = nullptr;
@@ -368,6 +368,8 @@ Module *argon::vm::datatype::ModuleNew(String *name, String *doc) {
368368
Release(mod);
369369
return nullptr;
370370
}
371+
372+
memory::Track((ArObject *) mod);
371373
}
372374

373375
return mod;

argon/vm/datatype/namespace.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ List *argon::vm::datatype::NamespaceKeysToList(Namespace *ns, AttributeFlag matc
317317
}
318318

319319
Namespace *argon::vm::datatype::NamespaceNew() {
320-
auto *ns = MakeGCObject<Namespace>(&NamespaceType, true);
320+
auto *ns = MakeGCObject<Namespace>(&NamespaceType);
321321

322322
if (ns != nullptr) {
323323
if (!ns->ns.Initialize()) {
@@ -327,6 +327,8 @@ Namespace *argon::vm::datatype::NamespaceNew() {
327327
}
328328

329329
new(&ns->rwlock)sync::RecursiveSharedMutex();
330+
331+
memory::Track((ArObject*)ns);
330332
}
331333

332334
return ns;

argon/vm/datatype/option.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ TypeInfo OptionType = {
135135
const TypeInfo *argon::vm::datatype::type_option_ = &OptionType;
136136

137137
Option *argon::vm::datatype::OptionNew(ArObject *value) {
138-
auto *opt = MakeGCObject<Option>(&OptionType, false);
138+
auto *opt = MakeGCObject<Option>(&OptionType);
139139

140140
if (opt != nullptr)
141141
opt->some = IncRef(value);

argon/vm/datatype/result.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ TypeInfo ResultType = {
140140
const TypeInfo *argon::vm::datatype::type_result_ = &ResultType;
141141

142142
Result *argon::vm::datatype::ResultNew(ArObject *value, bool success) {
143-
auto *result = MakeGCObject<Result>(&ResultType, false);
143+
auto *result = MakeGCObject<Result>(&ResultType);
144144

145145
if (result != nullptr) {
146146
result->value = IncRef(value == nullptr ? (ArObject *) Nil : value);

argon/vm/datatype/struct.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ Struct *argon::vm::datatype::StructNew(TypeInfo *type, ArObject **argv, unsigned
164164
}
165165
}
166166

167-
if ((ret = MakeGCObject<Struct>(type, true)) == nullptr) {
167+
if ((ret = MakeGCObject<Struct>(type)) == nullptr) {
168168
Release(ns);
169169
return nullptr;
170170
}
171171

172172
ret->ns = ns;
173173

174+
memory::Track((ArObject *) ret);
175+
174176
return ret;
175177
}

argon/vm/importer/import.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ Import *argon::vm::importer::ImportNew(Context *context) {
491491
goto ERROR; \
492492
} while(0)
493493

494-
auto *imp = MakeGCObject<Import>(type_import_, true);
494+
auto *imp = MakeGCObject<Import>(type_import_);
495495

496496
if (imp == nullptr)
497497
return nullptr;
@@ -528,6 +528,8 @@ Import *argon::vm::importer::ImportNew(Context *context) {
528528

529529
new(&imp->lock)std::mutex();
530530

531+
memory::Track((ArObject*)imp);
532+
531533
return imp;
532534

533535
ERROR:

0 commit comments

Comments
 (0)