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

Various MVar fixes #70

Merged
merged 14 commits into from
Mar 22, 2023
Merged

Various MVar fixes #70

merged 14 commits into from
Mar 22, 2023

Conversation

dcoutts
Copy link
Contributor

@dcoutts dcoutts commented Mar 8, 2023

  • Fix missing cases in (the sim impl of) takeMVar that was probably causing deadlocks
  • Fix an incorrect invariant violation check in (the sim impl of) isEmptyMVar
  • Fix an incorrect invariant violation check in (the sim impl of) takeMVar / putMVar in async exception case
  • Do not require the MVar type family to be injective, which makes it consistent with TVar and others.
  • Fix (the sim impl of) readMVar which had the wrong semantics.
  • Add tryReadMVar to the class

Copy link
Contributor

@jorisdral jorisdral left a comment

Choose a reason for hiding this comment

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

LGTM

io-classes/src/Control/Monad/Class/MonadMVar.hs Outdated Show resolved Hide resolved
io-classes/src/Control/Monad/Class/MonadMVar.hs Outdated Show resolved Hide resolved
io-classes/src/Control/Monad/Class/MonadMVar.hs Outdated Show resolved Hide resolved
io-classes/src/Control/Monad/Class/MonadMVar.hs Outdated Show resolved Hide resolved
Copy link
Collaborator

@coot coot left a comment

Choose a reason for hiding this comment

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

LGTM

@dcoutts dcoutts force-pushed the dcoutts/mvar-fixes branch from 4f978b5 to c7ffcc0 Compare March 15, 2023 13:19
dcoutts added 14 commits March 22, 2023 15:27
In the default implementation of MVar (used in the SimM, not IO), there
were two cases where the mvar state was not being correctly updated. In
both takeMVarDefault and tryTakeMVarDefault, in the case of a full mvar
with an empty wait queue, the mvar state was not being updated.

This is likely to be the cause of deadlocks observed in some simulations
using MVars.
The existing code assumed for some reason that it is impossible to have
any threads waiting on an empty mvar when someone is testing it to see
if it is empty. This is false, it's entirely possible, and doesn't
affect the question of whether the mvar is empty or not.
Scheduling means we cannot know the state of the MVar in the cleanup
handlers, so we cannot assume the mvar is empty/full. So don't fail in
such cases.
We don't require this for TVar or other classes. We only currently use
injective type families for the monad (like STM) not the variables.

It makes it harder (in the current style) to add newtype instances.

Fix tests (by removing unnecessary type annotations). These type
annotations could have done the intended thing, but only with scoped
type variables (which was not used). Easier to remove them.
Into a more logical grouping for review and modification.
Rename 'wakevar' to either 'takevar' or 'putvar', depending on usage.

Rename 'blockedq' to either 'takeq' or 'putq', depending on usage.

This helps to clarify the code because both takeMVar and putMVar have to
deal with both queues, so calling them the same hinders clarity.
Similarly (to a lesser extent) the wake variables.

This will be especially helpful when we introduce a third queue for
threads blocked on readMVar: the 'readq'.
Use a second readq of threads blocked on readMVar, which gets woken up
by putMVar or tryPutMVar.

All "readers" get woken up and the first "taker" if any.
It provides the wrong semantics, so it's not a helpful default
Hopefully slightly improve clarity.
It's actually not a suitable general purpose implementation, but it is
ok for the simulation.
@jorisdral jorisdral force-pushed the dcoutts/mvar-fixes branch from 343b696 to 71f6cb2 Compare March 22, 2023 14:28
@jorisdral jorisdral merged commit acdcffb into main Mar 22, 2023
@jorisdral jorisdral deleted the dcoutts/mvar-fixes branch March 22, 2023 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants