4This is an implementation of Cloud Haskell, as described in
5/Towards Haskell in the Cloud/ by Jeff Epstein, Andrew Black, and Simon
7<http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/>),
8although some of the details are different. The precise message passing
9semantics are based on /A unified semantics for future Erlang/ by Hans
10Svensson, Lars-Åke Fredlund and Clara Benac Earle.
12For a detailed description of the package and other reference materials,
13please see the distributed-process wiki page on github:
14<https://github.com/haskell-distributed/distributed-process/wiki>.
17module Control.Distributed.Process
25 , liftIO -- Reexported for convenience
41 -- * Unsafe messaging variants
48 -- * Advanced messaging
74 -- * Process management
83 , ProcessTerminationException(..)
84 , ProcessRegistrationException(..)
93 -- * Monitoring and linking
105 , MonitorRef -- opaque
106 , ProcessLinkException(..)
107 , NodeLinkException(..)
108 , PortLinkException(..)
109 , ProcessMonitorNotification(..)
110 , NodeMonitorNotification(..)
111 , PortMonitorNotification(..)
128 , registerRemoteAsync
129 , reregisterRemoteAsync
130 , unregisterRemoteAsync
135 -- * Exception handling
153 -- * Local versions of 'spawn'
162#if ! MIN_VERSION_base(4,6,0)
163import Prelude hiding (catch)
166import Control.Monad.IO.Class (liftIO)
167import Control.Applicative ((<$>))
168import Control.Monad.Reader (ask)
169import Control.Concurrent (killThread)
170import Control.Concurrent.MVar
176import Control.Distributed.Static
182import Control.Distributed.Process.Internal.Types
187 , ProcessMonitorNotification(..)
188 , NodeMonitorNotification(..)
189 , PortMonitorNotification(..)
190 , ProcessLinkException(..)
191 , NodeLinkException(..)
192 , PortLinkException(..)
193 , ProcessRegistrationException(..)
202 , LocalProcess(processNode)
206import Control.Distributed.Process.Serializable (Serializable)
207import Control.Distributed.Process.Internal.Primitives
223 -- Advanced messaging
249 -- Process management
251 , ProcessTerminationException(..)
264 -- Monitoring and linking
284 , registerRemoteAsync
285 , reregisterRemoteAsync
286 , unregisterRemoteAsync
292 -- Exception handling
311import Control.Distributed.Process.Node (forkProcess)
312import Control.Distributed.Process.Internal.Types
314 , withValidLocalState
316import Control.Distributed.Process.Internal.Spawn
317 ( -- Spawning Processes/Channels
325import Control.Exception (SomeException, throwIO, uninterruptibleMask_)
326import qualified Control.Exception as Exception (onException)
327import Control.Monad (forM_)
328import Data.Accessor ((^.))
333-- 1. 'send' never fails. If you want to know that the remote process received
334-- your message, you will need to send an explicit acknowledgement. If you
335-- want to know when the remote process failed, you will need to monitor
336-- that remote process.
338-- 2. 'send' may block (when the system TCP buffers are full, while we are
339-- trying to establish a connection to the remote endpoint, etc.) but its
340-- return does not imply that the remote process received the message (much
341-- less processed it). When 'send' targets a local process, it is dispatched
342-- via the node controller however, in which cases it will /not/ block. This
343-- isn't part of the semantics, so it should be ok. The ordering is maintained
344-- because the ctrlChannel still has FIFO semantics with regards interactions
345-- between two disparate forkIO threads.
347-- 3. Message delivery is reliable and ordered. That means that if process A
348-- sends messages m1, m2, m3 to process B, B will either arrive all three
349-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
350-- 'missing' (m1, m3) or reordered (m1, m3, m2)
352-- In order to guarantee (3), we stipulate that
354-- 3a. We do not garbage collect connections because Network.Transport provides
355-- ordering guarantees only *per connection*.
357-- 3b. Once a connection breaks, we have no way of knowing which messages
358-- arrived and which did not; hence, once a connection fails, we assume the
359-- remote process to be forever unreachable. Otherwise we might sent m1 and
360-- m2, get notified of the broken connection, reconnect, send m3, but only
363-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
364-- we consider it forever unreachable. Hence, new processes should get new
365-- IDs or they too would be considered unreachable.
367-- Main reference for Cloud Haskell is
369-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
371-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
373-- The precise semantics for message passing is based on
375-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
376-- and Clara Benac Earle (not freely available online, unfortunately)
378-- Some pointers to related documentation about Erlang, for comparison and
381-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
382-- Hans Svensson and Lars-Ake Fredlund
383-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
384-- [4] The Erlang manual, sections "Message Sending" and "Send"
385-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
386-- http://www.erlang.org/doc/reference_manual/expressions.html#send
387-- [5] Questions "Is the order of message reception guaranteed?" and
388-- "If I send a message, is it guaranteed to reach the receiver?" of
390-- http://www.erlang.org/faq/academic.html
391-- [6] "Delivery of Messages", post on erlang-questions
392-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
394--------------------------------------------------------------------------------
395-- Local versions of spawn --
396--------------------------------------------------------------------------------
398-- | Spawn a process on the local node
399spawnLocal :: Process () -> Process ProcessId
401 node <- processNode <$> ask
402 liftIO $ forkProcess node proc
404-- | Create a new typed channel, spawn a process on the local node, passing it
405-- the receive port, and return the send port
406spawnChannelLocal :: Serializable a
407 => (ReceivePort a -> Process ())
408 -> Process (SendPort a)
409spawnChannelLocal proc = do
410 node <- processNode <$> ask
413 _ <- forkProcess node $ do
414 -- It is important that we allocate the new channel in the new process,
415 -- because otherwise it will be associated with the wrong process ID
416 (sport, rport) <- newChan
417 liftIO $ putMVar mvar sport
421-- | Local version of 'call'. Running a process in this way isolates it from
422-- messages sent to the caller process, and also allows silently dropping late
423-- or duplicate messages sent to the isolated process after it exits.
424-- Silently dropping messages may not always be the best approach.
425callLocal :: Process a -> Process a
426callLocal proc = mask $ \release -> do
427 mv <- liftIO newEmptyMVar :: Process (MVar (Either SomeException a))
428 child <- spawnLocal $ try (release proc) >>= liftIO . putMVar mv
431 rs <- Exception.onException (takeMVar mv) $ uninterruptibleMask_ $
432 -- Exceptions need to be prevented from interrupting the clean up or
433 -- the original exception which caused entering the handler could be
434 -- forgotten. For instance, this could have a problematic effect
435 -- when the original exception was meant to kill the thread and the
436 -- second exception doesn't (like the exception thrown by
437 -- 'System.Timeout.timeout').
438 do mchildThreadId <- withValidLocalState (processNode lproc) $
439 \vst -> return $ fmap processThread $
440 vst ^. localProcessWithId (processLocalId child)
441 forM_ mchildThreadId killThread
443 either throwIO return rs
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list