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

Fix race conditions in thunk evaluation #867

Merged
merged 4 commits into from
Sep 2, 2020
Merged

Fix race conditions in thunk evaluation #867

merged 4 commits into from
Sep 2, 2020

Conversation

robdockins
Copy link
Contributor

With the addition of parmap, it became possible to observe race conditions in the thunk evaluation lifecycle. This PR reworks the evaluation monad so that threads will properly block and wait if they attempt to force a thunk that is already under evaluation by a different thread and uses transactional variables to ensure proper synchronization.

src/Cryptol/Eval/Monad.hs Outdated Show resolved Hide resolved
src/Cryptol/Eval/Value.hs Outdated Show resolved Hide resolved
@brianhuffman
Copy link
Contributor

Link to related issue: #856

@robdockins
Copy link
Contributor Author

@jared-w, the failures on Ubuntu seem related to the CVC4 installation. Did something on those configurations change recently?

Copy link
Contributor

@brianhuffman brianhuffman left a comment

Choose a reason for hiding this comment

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

Looks good to me; just needs a bit more documentation, especially about ThunkState.

Thunk state is now controlled using transational memory variables
instead of IORefs, and the thunk likecycle is made more explicit
in the relevant datatypes.  Threads will now properly block and wait
when forcing a thunk that is already under evaluation by a different
thread.  Hopefully, using optimistic concurrency (transactional memory)
will help reduce concurrency contention. The reworked lifecycle
datastructures hopefully will release closures related to evaluation
faster, which should reduce memory pressure somewhat.

Fixes #856
Instead of making the `Eval` monad a reader that carries around
the EvalOpts, pass it directly into the primitives table, which
pipes it directly to the one place those options are needed.
Hopefully, this will reduce closure allocations in the evaluation
monad, and it also just generally simplifies things.

Note, we need to be careful to instantiate the `primTable` values
only as often as needed.  The What4 and Concrete primitive tables
now take arguments, which means they are not CAFs.  As a result,
they need to be let-bound someplace fairly global so that we don't
recompute the map every time we look up a primitive.  Hence, I have
removed the helper `evalPrim` functions, and forced call sites
instead to implement the map lookups.
This should help prevent redundant thunking, as we can recognize
when a value is already thunked.  In addition, use the
already-existing thunk mechanism to implement `parmap` sparks.
@robdockins robdockins merged commit 4729596 into master Sep 2, 2020
brianhuffman pushed a commit to GaloisInc/saw-core that referenced this pull request Sep 11, 2020
This updates cryptol-saw-core to work with commit
GaloisInc/cryptol@0000ffbef6, adapting to PRs
GaloisInc/cryptol#866 and GaloisInc/cryptol#867.
@robdockins robdockins mentioned this pull request Sep 17, 2020
@RyanGlScott RyanGlScott deleted the issue856 branch March 22, 2024 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants