3This is an implementation of Cloud Haskell, as described in
4/Towards Haskell in the Cloud/ by Jeff Epstein, Andrew Black, and Simon
5Peyton Jones (<http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/>),
6although some of the details are different. The precise message passing
7semantics are based on /A unified semantics for future Erlang/ by Hans
8Svensson, Lars-Åke Fredlund and Clara Benac Earle.
10For a detailed description of the package and other reference materials,
11please see the distributed-process wiki page on github:
12<https://github.com/haskell-distributed/distributed-process/wiki>.
15module Control.Distributed.Process
23 , liftIO -- Reexported for convenience
38 -- * Advanced messaging
55 -- * Process management
64 , ProcessTerminationException(..)
65 , ProcessRegistrationException(..)
71 -- * Monitoring and linking
83 , MonitorRef -- opaque
84 , ProcessLinkException(..)
85 , NodeLinkException(..)
86 , PortLinkException(..)
87 , ProcessMonitorNotification(..)
88 , NodeMonitorNotification(..)
89 , PortMonitorNotification(..)
106 , registerRemoteAsync
107 , reregisterRemoteAsync
108 , unregisterRemoteAsync
113 -- * Exception handling
130 -- * Local versions of 'spawn'
138#if ! MIN_VERSION_base(4,6,0)
139import Prelude hiding (catch)
142import Data.Typeable (Typeable)
143import Control.Monad.IO.Class (liftIO)
144import Control.Applicative ((<$>))
145import Control.Monad.Reader (ask)
146import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
147import Control.Distributed.Static
155import Control.Distributed.Process.Internal.Types
160 , ProcessMonitorNotification(..)
161 , NodeMonitorNotification(..)
162 , PortMonitorNotification(..)
163 , ProcessLinkException(..)
164 , NodeLinkException(..)
165 , PortLinkException(..)
166 , ProcessRegistrationException(..)
175 , LocalProcess(processNode)
179import Control.Distributed.Process.Serializable (Serializable, SerializableDict)
180import Control.Distributed.Process.Internal.Closure.BuiltIn
192import Control.Distributed.Process.Internal.Primitives
202 -- Advanced messaging
218 -- Process management
220 , ProcessTerminationException(..)
230 -- Monitoring and linking
250 , registerRemoteAsync
251 , reregisterRemoteAsync
252 , unregisterRemoteAsync
258 -- Exception handling
276import Control.Distributed.Process.Node (forkProcess)
280-- 1. 'send' never fails. If you want to know that the remote process received
281-- your message, you will need to send an explicit acknowledgement. If you
282-- want to know when the remote process failed, you will need to monitor
283-- that remote process.
285-- 2. 'send' may block (when the system TCP buffers are full, while we are
286-- trying to establish a connection to the remote endpoint, etc.) but its
287-- return does not imply that the remote process received the message (much
290-- 3. Message delivery is reliable and ordered. That means that if process A
291-- sends messages m1, m2, m3 to process B, B will either arrive all three
292-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
293-- 'missing' (m1, m3) or reordered (m1, m3, m2)
295-- In order to guarantee (3), we stipulate that
297-- 3a. We do not garbage collect connections because Network.Transport provides
298-- ordering guarantees only *per connection*.
300-- 3b. Once a connection breaks, we have no way of knowing which messages
301-- arrived and which did not; hence, once a connection fails, we assume the
302-- remote process to be forever unreachable. Otherwise we might sent m1 and
303-- m2, get notified of the broken connection, reconnect, send m3, but only
306-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
307-- we consider it forever unreachable. Hence, new processes should get new
308-- IDs or they too would be considered unreachable.
310-- Main reference for Cloud Haskell is
312-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
314-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
316-- The precise semantics for message passing is based on
318-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
319-- and Clara Benac Earle (not freely available online, unfortunately)
321-- Some pointers to related documentation about Erlang, for comparison and
324-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
325-- Hans Svensson and Lars-Ake Fredlund
326-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
327-- [4] The Erlang manual, sections "Message Sending" and "Send"
328-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
329-- http://www.erlang.org/doc/reference_manual/expressions.html#send
330-- [5] Questions "Is the order of message reception guaranteed?" and
331-- "If I send a message, is it guaranteed to reach the receiver?" of
333-- http://www.erlang.org/faq/academic.html
334-- [6] "Delivery of Messages", post on erlang-questions
335-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
337--------------------------------------------------------------------------------
338-- Primitives are defined in a separate module; here we only define derived --
340--------------------------------------------------------------------------------
344-- For more information about 'Closure', see
345-- "Control.Distributed.Process.Closure".
348spawn :: NodeId -> Closure (Process ()) -> Process ProcessId
351 mRef <- monitorNode nid
352 sRef <- spawnAsync nid (cpDelay us proc)
354 matchIf (\(DidSpawn ref _) -> ref == sRef) $ \(DidSpawn _ pid) -> do
358 , matchIf (\(NodeMonitorNotification ref _ _) -> ref == mRef) $ \_ ->
359 return (nullProcessId nid)
362-- | Spawn a process and link to it
364-- Note that this is just the sequential composition of 'spawn' and 'link'.
365-- (The "Unified" semantics that underlies Cloud Haskell does not even support
366-- a synchronous link operation)
367spawnLink :: NodeId -> Closure (Process ()) -> Process ProcessId
368spawnLink nid proc = do
369 pid <- spawn nid proc
373-- | Like 'spawnLink', but monitor the spawned process
374spawnMonitor :: NodeId -> Closure (Process ()) -> Process (ProcessId, MonitorRef)
375spawnMonitor nid proc = do
376 pid <- spawn nid proc
380-- | Run a process remotely and wait for it to reply
382-- We monitor the remote process: if it dies before it can send a reply, we die
385-- For more information about 'Static', 'SerializableDict', and 'Closure', see
386-- "Control.Distributed.Process.Closure".
389call :: Serializable a
390 => Static (SerializableDict a)
392 -> Closure (Process a)
394call dict nid proc = do
396 (pid, mRef) <- spawnMonitor nid (proc `bindCP` cpSend dict us)
397 -- We are guaranteed to receive the reply before the monitor notification
398 -- (if a reply is sent at all)
399 -- NOTE: This might not be true if we switch to unreliable delivery.
400 mResult <- receiveWait
401 [ match (return . Right)
402 , matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
403 (\(ProcessMonitorNotification _ _ reason) -> return (Left reason))
407 -- Wait for the monitor message so that we the mailbox doesn't grow
409 [ matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
410 (\(ProcessMonitorNotification {}) -> return ())
412 -- Clean up connection to pid
416 fail $ "call: remote process died: " ++ show err
418-- | Spawn a child process, have the child link to the parent and the parent
420spawnSupervised :: NodeId
421 -> Closure (Process ())
422 -> Process (ProcessId, MonitorRef)
423spawnSupervised nid proc = do
425 them <- spawn nid (cpLink us `seqCP` proc)
429-- | Spawn a new process, supplying it with a new 'ReceivePort' and return
430-- the corresponding 'SendPort'.
431spawnChannel :: forall a. Typeable a => Static (SerializableDict a)
433 -> Closure (ReceivePort a -> Process ())
434 -> Process (SendPort a)
435spawnChannel dict nid proc = do
437 _ <- spawn nid (go us)
440 go :: ProcessId -> Closure (Process ())
441 go pid = cpNewChan dict
443 (cpSend (sdictSendPort dict) pid `splitCP` proc)
445 (idCP `closureCompose` staticClosure sndStatic)
447--------------------------------------------------------------------------------
448-- Local versions of spawn --
449--------------------------------------------------------------------------------
451-- | Spawn a process on the local node
452spawnLocal :: Process () -> Process ProcessId
454 node <- processNode <$> ask
455 liftIO $ forkProcess node proc
457-- | Create a new typed channel, spawn a process on the local node, passing it
458-- the receive port, and return the send port
459spawnChannelLocal :: Serializable a
460 => (ReceivePort a -> Process ())
461 -> Process (SendPort a)
462spawnChannelLocal proc = do
463 node <- processNode <$> ask
466 _ <- forkProcess node $ do
467 -- It is important that we allocate the new channel in the new process,
468 -- because otherwise it will be associated with the wrong process ID
469 (sport, rport) <- newChan
470 liftIO $ putMVar mvar sport
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