Skip to content

Commit

Permalink
fix: resolved issue with GC-managed object creation in a fully multit…
Browse files Browse the repository at this point in the history
…hreaded environment
  • Loading branch information
jacopodl committed Jun 11, 2024
1 parent ec79d20 commit c0973ab
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 23 deletions.
8 changes: 4 additions & 4 deletions argon/lang/parser2/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ const argon::vm::datatype::TypeInfo *argon::lang::parser2::ExtName = &alias##Ast
T *NewNode(const TypeInfo *t_info, bool gc, NodeType node_type) {
T *node;

if (!gc)
node = MakeObject<T>(t_info);
else
node = MakeGCObject<T>(t_info, false);
node = !gc ? MakeObject<T>(t_info) : MakeGCObject<T>(t_info);

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

*((NodeType *) n_obj) = node_type;

if (gc)
argon::vm::memory::Track((ArObject *) node);

return node;
}

Expand Down
8 changes: 4 additions & 4 deletions argon/vm/datatype/arobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ namespace argon::vm::datatype {
}

template<typename T>
T *MakeGCObject(const TypeInfo *type, bool track) {
return (T *) memory::GCNew(type, track);
T *MakeGCObject(const TypeInfo *type) {
return (T *) memory::GCNew(type, false);
}

template<typename T>
T *MakeGCObject(TypeInfo *type, bool track) {
auto *ret = (T *) memory::GCNew(type, track);
T *MakeGCObject(TypeInfo *type) {
auto *ret = (T *) memory::GCNew(type, false);
if (ret != nullptr)
IncRef(type);

Expand Down
2 changes: 1 addition & 1 deletion argon/vm/datatype/chan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ bool argon::vm::datatype::ChanWrite(Chan *chan, ArObject *value) {
}

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

if (chan != nullptr) {
if ((chan->queue = (ArObject **) argon::vm::memory::Alloc(backlog * sizeof(void *))) == nullptr) {
Expand Down
8 changes: 5 additions & 3 deletions argon/vm/datatype/dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ ArObject *dict_compare(Dict *self, ArObject *other, CompareMode mode) {
}

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

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

li->reverse = reverse;

argon::vm::memory::Track((ArObject*)li);
}

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

Dict *argon::vm::datatype::DictNew() {
auto *dict = MakeGCObject<Dict>(type_dict_, false);
auto *dict = MakeGCObject<Dict>(type_dict_);

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

Dict *argon::vm::datatype::DictNew(unsigned int size) {
auto *dict = MakeGCObject<Dict>(type_dict_, false);
auto *dict = MakeGCObject<Dict>(type_dict_);

if (dict != nullptr) {
if (!dict->hmap.Initialize(size)) {
Expand Down
4 changes: 2 additions & 2 deletions argon/vm/datatype/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ bool FunctionCheckParams(const PCheck *pcheck, ArObject **args, ArSize count) {
}

Function *FunctionClone(const Function *func) {
auto *fn = MakeGCObject<Function>(type_function_, false);
auto *fn = MakeGCObject<Function>(type_function_);

if (fn != nullptr) {
if (func->IsNative())
Expand All @@ -213,7 +213,7 @@ Function *FunctionClone(const Function *func) {
}

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

if (fn != nullptr) {
fn->name = IncRef(name);
Expand Down
2 changes: 1 addition & 1 deletion argon/vm/datatype/future.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool argon::vm::datatype::FutureAWait(Future *future) {
}

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

if (future != nullptr) {
future->value = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions argon/vm/datatype/iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace argon::vm::datatype {
}

inline void IteratorTrace(IteratorGeneric *self, Void_UnaryOp trace){
std::unique_lock _(self->lock);

trace(self->iterable);
}

Expand Down
6 changes: 4 additions & 2 deletions argon/vm/datatype/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,16 @@ ArObject *list_compare(List *self, ArObject *other, CompareMode mode) {
}

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

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

li->iterable = IncRef(self);
li->index = 0;
li->reverse = reverse;

argon::vm::memory::Track((ArObject *) li);
}

return (ArObject *) li;
Expand Down Expand Up @@ -817,7 +819,7 @@ List *ListFromIterable(List **dest, ArObject *iterable) {
}

List *argon::vm::datatype::ListNew(ArSize capacity) {
auto *list = MakeGCObject<List>(type_list_, false);
auto *list = MakeGCObject<List>(type_list_);

if (list != nullptr) {
list->objects = nullptr;
Expand Down
4 changes: 3 additions & 1 deletion argon/vm/datatype/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ Module *argon::vm::datatype::ModuleNew(const ModuleInit *init) {
}

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

if (mod != nullptr) {
mod->fini = nullptr;
Expand All @@ -368,6 +368,8 @@ Module *argon::vm::datatype::ModuleNew(String *name, String *doc) {
Release(mod);
return nullptr;
}

memory::Track((ArObject *) mod);
}

return mod;
Expand Down
4 changes: 3 additions & 1 deletion argon/vm/datatype/namespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ List *argon::vm::datatype::NamespaceKeysToList(Namespace *ns, AttributeFlag matc
}

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

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

new(&ns->rwlock)sync::RecursiveSharedMutex();

memory::Track((ArObject*)ns);
}

return ns;
Expand Down
2 changes: 1 addition & 1 deletion argon/vm/datatype/option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ TypeInfo OptionType = {
const TypeInfo *argon::vm::datatype::type_option_ = &OptionType;

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

if (opt != nullptr)
opt->some = IncRef(value);
Expand Down
2 changes: 1 addition & 1 deletion argon/vm/datatype/result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ TypeInfo ResultType = {
const TypeInfo *argon::vm::datatype::type_result_ = &ResultType;

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

if (result != nullptr) {
result->value = IncRef(value == nullptr ? (ArObject *) Nil : value);
Expand Down
4 changes: 3 additions & 1 deletion argon/vm/datatype/struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ Struct *argon::vm::datatype::StructNew(TypeInfo *type, ArObject **argv, unsigned
}
}

if ((ret = MakeGCObject<Struct>(type, true)) == nullptr) {
if ((ret = MakeGCObject<Struct>(type)) == nullptr) {
Release(ns);
return nullptr;
}

ret->ns = ns;

memory::Track((ArObject *) ret);

return ret;
}
4 changes: 3 additions & 1 deletion argon/vm/importer/import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ Import *argon::vm::importer::ImportNew(Context *context) {
goto ERROR; \
} while(0)

auto *imp = MakeGCObject<Import>(type_import_, true);
auto *imp = MakeGCObject<Import>(type_import_);

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

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

memory::Track((ArObject*)imp);

return imp;

ERROR:
Expand Down

0 comments on commit c0973ab

Please sign in to comment.