Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the concurrency of erts_fun_table insertions #8662

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions erts/emulator/beam/erl_fun.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ erts_put_fun_entry2(Eterm mod, int old_uniq, int old_index,
ErlFunEntryContainer *fc;
ErlFunEntry *tp;
erts_aint_t refc;
int is_read_lock;

tp = &template.entry;

Expand All @@ -118,14 +119,34 @@ erts_put_fun_entry2(Eterm mod, int old_uniq, int old_index,

sys_memcpy(tp->uniq, uniq, sizeof(tp->uniq));

erts_fun_write_lock();
fc = (ErlFunEntryContainer*)hash_put(&erts_fun_table, (void*)&template);
/*
* Start with a shared, reader-lock which avoids contention when an insert
* is not required.
*/
is_read_lock = 1;
erts_fun_read_lock();
fc = (ErlFunEntryContainer*)hash_get(&erts_fun_table, (void*)&template);
if (fc == NULL) {
/*
* Key is not present. Acquire an exclusive, writer-lock and retry with
* a lookup that will insert if the key is still not present.
*/
erts_fun_read_unlock();
is_read_lock = 0;
erts_fun_write_lock();
fc = (ErlFunEntryContainer*)hash_put(&erts_fun_table, (void*)&template);
}

refc = erts_refc_inctest(&fc->entry.refc, 0);
if (refc < 2) {
/* New or pending delete */
erts_refc_inc(&fc->entry.refc, 1);
}
erts_fun_write_unlock();

if (is_read_lock)
erts_fun_read_unlock();
else
erts_fun_write_unlock();

return &fc->entry;
}
Expand Down