Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
GHC
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gesh
GHC
Commits
3e567b65
Commit
3e567b65
authored
25 years ago
by
Simon Marlow
Browse files
Options
Downloads
Patches
Plain Diff
[project @ 2000-04-12 17:08:15 by simonmar]
add takeMaybeMVar
parent
7e150969
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ghc/lib/std/PrelConc.lhs
+12
-7
12 additions, 7 deletions
ghc/lib/std/PrelConc.lhs
with
12 additions
and
7 deletions
ghc/lib/std/PrelConc.lhs
+
12
−
7
View file @
3e567b65
...
...
@@ -33,11 +33,13 @@ module PrelConc
, putMVar -- :: MVar a -> a -> IO ()
, readMVar -- :: MVar a -> IO a
, swapMVar -- :: MVar a -> a -> IO a
, takeMaybeMVar -- :: MVar a -> IO (Maybe a)
, isEmptyMVar -- :: MVar a -> IO Bool
) where
import PrelBase
import PrelMaybe
import PrelErr ( parError, seqError )
import PrelST ( liftST )
import PrelIOBase ( IO(..), MVar(..), unsafePerformIO )
...
...
@@ -115,48 +117,51 @@ writes.
--Defined in IOBase to avoid cycle: data MVar a = MVar (SynchVar# RealWorld a)
newEmptyMVar :: IO (MVar a)
newEmptyMVar = IO $ \ s# ->
case newMVar# s# of
(# s2#, svar# #) -> (# s2#, MVar svar# #)
takeMVar :: MVar a -> IO a
takeMVar (MVar mvar#) = IO $ \ s# -> takeMVar# mvar# s#
putMVar :: MVar a -> a -> IO ()
putMVar (MVar mvar#) x = IO $ \ s# ->
case putMVar# mvar# x s# of
s2# -> (# s2#, () #)
newMVar :: a -> IO (MVar a)
newMVar value =
newEmptyMVar >>= \ mvar ->
putMVar mvar value >>
return mvar
readMVar :: MVar a -> IO a
readMVar mvar =
takeMVar mvar >>= \ value ->
putMVar mvar value >>
return value
swapMVar :: MVar a -> a -> IO a
swapMVar mvar new =
takeMVar mvar >>= \ old ->
putMVar mvar new >>
return old
-- takeMaybeMVar is a non-blocking takeMVar
takeMaybeMVar :: MVar a -> IO (Maybe a)
takeMaybeMVar (MVar m) = IO $ \ s ->
case takeMaybeMVar# m s of
(# s, 0#, _ #) -> (# s, Nothing #) -- MVar is empty
(# s, _, a #) -> (# s, Just a #) -- MVar is full
{-
Low-level op. for checking whether an MVar is filled-in or not.
Notice that the boolean value returned is just a snapshot of
the state of the MVar. By the time you get to react on its result,
the MVar may have been filled (or emptied) - so be extremely
careful when using this operation.
careful when using this operation.
Use takeMaybeMVar instead if possible.
If you can re-work your abstractions to avoid having to
depend on isEmptyMVar, then you're encouraged to do so,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment