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

Made AnalysisCallback thread-safe again. Re sbt/sbt#6198 #957

Merged

Conversation

dotta
Copy link
Contributor

@dotta dotta commented Dec 18, 2020

For the parallel Hydra Scala compiler to work with sbt and zinc, it's paramount
that the AnalysisCallback is thread-safe. The bulk of the work was done in
#410, but the changes introduced to support pipelined compilation were
not thread-safe, hence could potentially break compilation via Hydra.

There are three categories of concurrency hazards that are fixed:

  • Memory visibility issues (@volatile helps with this)
  • Compiler/JVM optimizations leading to instructions re-ordering (`@volatile
    prevents this from happening in the sections of interest)
  • Race conditions due to concurrent read/write (AtomicBoolean CAS heals this)

Mind that instead of @volatile, it was perfectly fine to use
AtomicReference/AtomicBoolean, but I opted for @volatile in the places
where CAS were not required. The rationale is to apply KISS.

I've tested this by building a local sbt with zinc produced out of this PR, and tested Hydra compilation in sbt 1.4 works. I don't believe there is a need for testing performance. The reason is that the performance impact of this PR is so irrelevant that is essentially impossible to observe.

For the parallel Hydra Scala compiler to work with sbt and zinc, it's paramount
that the AnalysisCallback is thread-safe. The bulk of the work was done in
sbt#410, but the changes introduced to support pipelined compilation were
not thread-safe, hence could potentially break compilation via Hydra.

There are three categories of concurrency hazards that are fixed:

* Memory visibility issues (`@volatile` helps with this)
* Compiler/JVM optimizations leading to instructions re-ordering (`@volatile
  prevents this from happening in the sections of interest)
* Race conditions due to concurrent read/write (`AtomicBoolean` CAS heals this)

Mind that instead of `@volatile`, it was perfectly fine to use
`AtomicReference`/`AtomicBoolean`, but I opted for `@volatile` in the places
where CAS were not required. The rationale is to apply KISS.
@dotta
Copy link
Contributor Author

dotta commented Dec 18, 2020

\cc @dragos

Copy link
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@eed3si9n eed3si9n merged commit 45c7f4b into sbt:develop Dec 20, 2020
@dotta dotta deleted the issue/sbt-6198-thread-safe-analysis-callback branch December 20, 2020 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants