[go: nahoru, domu]

Skip to content

Commit

Permalink
add second form of poll with timeout, use as MVar.wait millis
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingo60 committed Feb 22, 2019
1 parent 847e560 commit f6f66bb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion frege/control/Concurrent.fr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ abstract newtype MVar a = MV (MutableIO (BlockingQueue a)) where
offer (MV q) a = q.offer a

--- get the value from a 'MVar', return 'Nothing' when empty
poll (MV q) = q.poll
poll (MV q) = q.poll

--- like 'poll', but waits a specified number of milliseconds for a value to become available
wait (MV q) n = q.poll n TimeUnit.milliSeconds

-- Haskell compatibility
newEmptyMVar = MVar.newEmpty
Expand Down
12 changes: 11 additions & 1 deletion frege/java/util/Concurrent.fr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Java.Util
data RejectedExecutionException = pure native java.util.concurrent.RejectedExecutionException
derive Exceptional RejectedExecutionException

data TimeUnit = pure native java.util.concurrent.TimeUnit where
pure native milliSeconds java.util.concurrent.TimeUnit.MILLISECONDS :: TimeUnit

data BlockingQueue e = native java.util.concurrent.BlockingQueue where
--- add element to blocking queue, throw exception if not possible
Expand All @@ -19,8 +21,16 @@ data BlockingQueue e = native java.util.concurrent.BlockingQueue where

--- get and remove element from blocking queue, throw exception if it is empty
native remove :: MutableIO (BlockingQueue e) -> IO e throws NoSuchElementException
--- get and remove element from blocking queue, return null if it is empty
{--
1. Get and remove element from blocking queue, return null if it is empty.
2. Get and removes element of this queue, waiting up to the specified wait time if necessary for an element to become available.
Return null if nothing becomes available.
--}

native poll :: MutableIO (BlockingQueue e) -> IO (Maybe e)
| MutableIO (BlockingQueue e) -> Int -> TimeUnit -> IO (Maybe e) throws InterruptedException
--- get and remove element from blocking queue, block until something is available
native take :: MutableIO (BlockingQueue e) -> IO e throws InterruptedException

Expand Down

0 comments on commit f6f66bb

Please sign in to comment.