Entity * to std::atomic<Entity *> to remove the need for a PtrMap+Mutex

This commit is contained in:
gingerBill
2025-09-19 11:01:41 +01:00
parent 5f76d6ce15
commit 6ce889f4eb
8 changed files with 24 additions and 28 deletions

View File

@@ -1549,7 +1549,7 @@ gb_internal void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) {
"\tother at %s",
LIT(name), token_pos_to_string(pos));
} else if (name == "main") {
if (d->entity->pkg->kind != Package_Runtime) {
if (d->entity.load()->pkg->kind != Package_Runtime) {
error(d->proc_lit, "The link name 'main' is reserved for internal use");
}
} else {
@@ -1967,8 +1967,8 @@ gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de
ctx->curr_proc_sig = type;
ctx->curr_proc_calling_convention = type->Proc.calling_convention;
if (decl->parent && decl->entity && decl->parent->entity) {
decl->entity->parent_proc_decl = decl->parent;
if (decl->parent && decl->entity.load() && decl->parent->entity) {
decl->entity.load()->parent_proc_decl = decl->parent;
}
if (ctx->pkg->name != "runtime") {
@@ -2072,7 +2072,7 @@ gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de
GB_ASSERT(decl->proc_checked_state != ProcCheckedState_Checked);
if (decl->defer_use_checked) {
GB_ASSERT(is_type_polymorphic(type, true));
error(token, "Defer Use Checked: %.*s", LIT(decl->entity->token.string));
error(token, "Defer Use Checked: %.*s", LIT(decl->entity.load()->token.string));
GB_ASSERT(decl->defer_use_checked == false);
}

View File

@@ -608,7 +608,7 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
entity->flags |= EntityFlag_Disabled;
}
d->entity = entity;
d->entity.store(entity);
AstFile *file = nullptr;
{
@@ -8335,9 +8335,10 @@ gb_internal ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *c
if (c->curr_proc_decl == nullptr) {
error(call, "Calling a '#force_inline' procedure that enables target features is not allowed at file scope");
} else {
GB_ASSERT(c->curr_proc_decl->entity);
GB_ASSERT(c->curr_proc_decl->entity->type->kind == Type_Proc);
String scope_features = c->curr_proc_decl->entity->type->Proc.enable_target_feature;
Entity *e = c->curr_proc_decl->entity.load();
GB_ASSERT(e);
GB_ASSERT(e->type->kind == Type_Proc);
String scope_features = e->type->Proc.enable_target_feature;
if (!check_target_feature_is_superset_of(scope_features, pt->Proc.enable_target_feature, &invalid)) {
ERROR_BLOCK();
error(call, "Inlined procedure enables target feature '%.*s', this requires the calling procedure to at least enable the same feature", LIT(invalid));

View File

@@ -2055,8 +2055,8 @@ gb_internal void add_entity_and_decl_info(CheckerContext *c, Ast *identifier, En
add_entity_definition(info, identifier, e);
GB_ASSERT(e->decl_info == nullptr);
e->decl_info = d;
d->entity = e;
e->pkg = c->pkg;
d->entity.store(e);
isize queue_count = -1;
bool is_lazy = false;

View File

@@ -209,7 +209,7 @@ struct DeclInfo {
Scope * scope;
Entity *entity;
std::atomic<Entity *> entity;
Ast * decl_node;
Ast * type_expr;

View File

@@ -238,9 +238,6 @@ struct lbGenerator : LinkerData {
PtrMap<LLVMContextRef, lbModule *> modules_through_ctx;
lbModule default_module;
RecursiveMutex anonymous_proc_lits_mutex;
PtrMap<Ast *, lbProcedure *> anonymous_proc_lits;
isize used_module_count;
lbProcedure *startup_runtime;

View File

@@ -157,7 +157,6 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) {
map_init(&gen->modules, gen->info->packages.count*2);
map_init(&gen->modules_through_ctx, gen->info->packages.count*2);
map_init(&gen->anonymous_proc_lits, 1024);
if (USE_SEPARATE_MODULES) {
bool module_per_file = build_context.module_per_file && build_context.optimization_level <= 0;
@@ -3084,18 +3083,14 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e)
gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent) {
lbGenerator *gen = m->gen;
// lbGenerator *gen = m->gen;
ast_node(pl, ProcLit, expr);
mutex_lock(&gen->anonymous_proc_lits_mutex);
defer (mutex_unlock(&gen->anonymous_proc_lits_mutex));
TokenPos pos = ast_token(expr).pos;
lbProcedure **found = map_get(&gen->anonymous_proc_lits, expr);
if (found) {
return lb_find_procedure_value_from_entity(m, (*found)->entity);
if (pl->decl->entity.load() != nullptr) {
return lb_find_procedure_value_from_entity(m, pl->decl->entity.load());
}
ast_node(pl, ProcLit, expr);
TokenPos pos = ast_token(expr).pos;
// NOTE(bill): Generate a new name
// parent$count
@@ -3114,15 +3109,18 @@ gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &pr
token.string = name;
Entity *e = alloc_entity_procedure(nullptr, token, type, pl->tags);
e->file = expr->file();
e->pkg = e->file->pkg;
e->scope = e->file->scope;
// NOTE(bill): this is to prevent a race condition since these procedure literals can be created anywhere at any time
pl->decl->code_gen_module = m;
e->decl_info = pl->decl;
pl->decl->entity = e;
e->parent_proc_decl = pl->decl->parent;
e->Procedure.is_anonymous = true;
e->flags |= EntityFlag_ProcBodyChecked;
pl->decl->entity.store(e);
lbProcedure *p = lb_create_procedure(m, e);
GB_ASSERT(e->code_gen_module == m);
@@ -3130,7 +3128,6 @@ gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &pr
value.value = p->value;
value.type = p->type;
map_set(&gen->anonymous_proc_lits, expr, p);
array_add(&m->procedures_to_generate, p);
if (parent != nullptr) {
array_add(&parent->children, p);

View File

@@ -2211,7 +2211,7 @@ gb_internal lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValu
GB_ASSERT(e != nullptr);
if (e->parent_proc_decl != nullptr && e->parent_proc_decl->entity != nullptr) {
procedure = e->parent_proc_decl->entity->token.string;
procedure = e->parent_proc_decl->entity.load()->token.string;
} else {
procedure = str_lit("");
}

View File

@@ -394,8 +394,9 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ
String proc_name = {};
if (t->Named.type_name->parent_proc_decl) {
DeclInfo *decl = t->Named.type_name->parent_proc_decl;
if (decl->entity && decl->entity->kind == Entity_Procedure) {
proc_name = decl->entity->token.string;
Entity *e = decl->entity.load();
if (e && e->kind == Entity_Procedure) {
proc_name = e->token.string;
}
}
TokenPos pos = t->Named.type_name->token.pos;