Skip to content

Commit

Permalink
Fix possible race in multi-threaded precompilation
Browse files Browse the repository at this point in the history
by changing $!lock-count to an atomicint and using atomic operations to
interact with it. This code used to be inside a `.protect` call (and so
didn't need to be atomic), but that was removed in
ac87ea2 for other reasons.
  • Loading branch information
MasterDuke17 committed Oct 4, 2020
1 parent 9fdc003 commit 1294621
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/core.c/CompUnit/PrecompilationStore/File.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ class CompUnit::PrecompilationStore::File
has IO::Path:D $.prefix is built(:bind) is required;

has IO::Handle $!lock;
#?if moar
has atomicint $!lock-count;
#?endif
#?if !moar
has int $!lock-count;
#?endif
has $!loaded;
has $!dir-cache;
has $!compiler-cache;
Expand Down Expand Up @@ -168,15 +173,29 @@ class CompUnit::PrecompilationStore::File
$!update-lock.lock;
$!lock := "$path.lock".IO.open(:create, :rw)
unless $!lock;
#?if moar
$!lock-countβš›++;
$!lock.lock if βš›$!lock-count == 1;
#?endif
#?if !moar
$!lock.lock if $!lock-count++ == 0;
#?endif
}

method unlock() {
LEAVE $!update-lock.unlock;
#?if moar
die "unlock when we're not locked!" if βš›$!lock-count == 0;

$!lock-countβš›-- if βš›$!lock-count > 0;
if $!lock && βš›$!lock-count == 0 {
#?endif
#?if !moar
die "unlock when we're not locked!" if $!lock-count == 0;

$!lock-count-- if $!lock-count > 0;
if $!lock && $!lock-count == 0 {
#?endif
$!lock.unlock;
$!lock.close;
$!lock := IO::Handle;
Expand Down

0 comments on commit 1294621

Please sign in to comment.