1{-# LANGUAGE ScopedTypeVariables #-}
5This is an implementation of Cloud Haskell, as described in
6/Towards Haskell in the Cloud/ by Jeff Epstein, Andrew Black, and Simon
7Peyton Jones (<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
40 -- * Advanced messaging
51 -- * Process management
60 , ProcessTerminationException(..)
61 , ProcessRegistrationException(..)
67 -- * Monitoring and linking
79 , MonitorRef -- opaque
80 , ProcessLinkException(..)
81 , NodeLinkException(..)
82 , PortLinkException(..)
83 , ProcessMonitorNotification(..)
84 , NodeMonitorNotification(..)
85 , PortMonitorNotification(..)
102 , registerRemoteAsync
103 , reregisterRemoteAsync
104 , unregisterRemoteAsync
109 -- * Exception handling
126 -- * Local versions of 'spawn'
134#if ! MIN_VERSION_base(4,6,0)
135import Prelude hiding (catch)
138import Data.Typeable (Typeable)
139import Control.Monad.IO.Class (liftIO)
140import Control.Applicative ((<$>))
141import Control.Monad.Reader (ask)
142import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
143import Control.Distributed.Static
151import Control.Distributed.Process.Internal.Types
156 , ProcessMonitorNotification(..)
157 , NodeMonitorNotification(..)
158 , PortMonitorNotification(..)
159 , ProcessLinkException(..)
160 , NodeLinkException(..)
161 , PortLinkException(..)
162 , ProcessRegistrationException(..)
171 , LocalProcess(processNode)
174import Control.Distributed.Process.Serializable (Serializable, SerializableDict)
175import Control.Distributed.Process.Internal.Closure.BuiltIn
188import Control.Distributed.Process.Internal.Primitives
198 -- Advanced messaging
205 , AbstractMessage(..)
209 -- Process management
211 , ProcessTerminationException(..)
221 -- Monitoring and linking
241 , registerRemoteAsync
242 , reregisterRemoteAsync
243 , unregisterRemoteAsync
249 -- Exception handling
267import Control.Distributed.Process.Node (forkProcess)
271-- 1. 'send' never fails. If you want to know that the remote process received
272-- your message, you will need to send an explicit acknowledgement. If you
273-- want to know when the remote process failed, you will need to monitor
274-- that remote process.
276-- 2. 'send' may block (when the system TCP buffers are full, while we are
277-- trying to establish a connection to the remote endpoint, etc.) but its
278-- return does not imply that the remote process received the message (much
281-- 3. Message delivery is reliable and ordered. That means that if process A
282-- sends messages m1, m2, m3 to process B, B will either arrive all three
283-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
284-- 'missing' (m1, m3) or reordered (m1, m3, m2)
286-- In order to guarantee (3), we stipulate that
288-- 3a. We do not garbage collect connections because Network.Transport provides
289-- ordering guarantees only *per connection*.
291-- 3b. Once a connection breaks, we have no way of knowing which messages
292-- arrived and which did not; hence, once a connection fails, we assume the
293-- remote process to be forever unreachable. Otherwise we might sent m1 and
294-- m2, get notified of the broken connection, reconnect, send m3, but only
297-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
298-- we consider it forever unreachable. Hence, new processes should get new
299-- IDs or they too would be considered unreachable.
301-- Main reference for Cloud Haskell is
303-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
305-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
307-- The precise semantics for message passing is based on
309-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
310-- and Clara Benac Earle (not freely available online, unfortunately)
312-- Some pointers to related documentation about Erlang, for comparison and
315-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
316-- Hans Svensson and Lars-Ake Fredlund
317-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
318-- [4] The Erlang manual, sections "Message Sending" and "Send"
319-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
320-- http://www.erlang.org/doc/reference_manual/expressions.html#send
321-- [5] Questions "Is the order of message reception guaranteed?" and
322-- "If I send a message, is it guaranteed to reach the receiver?" of
324-- http://www.erlang.org/faq/academic.html
325-- [6] "Delivery of Messages", post on erlang-questions
326-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
328--------------------------------------------------------------------------------
329-- Primitives are defined in a separate module; here we only define derived --
331--------------------------------------------------------------------------------
335-- For more information about 'Closure', see
336-- "Control.Distributed.Process.Closure".
339spawn :: NodeId -> Closure (Process ()) -> Process ProcessId
342 mRef <- monitorNode nid
343 sRef <- spawnAsync nid (cpDelay us proc)
345 matchIf (\(DidSpawn ref _) -> ref == sRef) $ \(DidSpawn _ pid) -> do
349 , matchIf (\(NodeMonitorNotification ref _ _) -> ref == mRef) $ \_ ->
350 return (nullProcessId nid)
353-- | Spawn a process and link to it
355-- Note that this is just the sequential composition of 'spawn' and 'link'.
356-- (The "Unified" semantics that underlies Cloud Haskell does not even support
357-- a synchronous link operation)
358spawnLink :: NodeId -> Closure (Process ()) -> Process ProcessId
359spawnLink nid proc = do
360 pid <- spawn nid proc
364-- | Like 'spawnLink', but monitor the spawned process
365spawnMonitor :: NodeId -> Closure (Process ()) -> Process (ProcessId, MonitorRef)
366spawnMonitor nid proc = do
367 pid <- spawn nid proc
371-- | Run a process remotely and wait for it to reply
373-- We monitor the remote process: if it dies before it can send a reply, we die
376-- For more information about 'Static', 'SerializableDict', and 'Closure', see
377-- "Control.Distributed.Process.Closure".
380call :: Serializable a
381 => Static (SerializableDict a)
383 -> Closure (Process a)
385call dict nid proc = do
387 (pid, mRef) <- spawnMonitor nid (proc `bindCP` cpSend dict us)
388 -- We are guaranteed to receive the reply before the monitor notification
389 -- (if a reply is sent at all)
390 -- NOTE: This might not be true if we switch to unreliable delivery.
391 mResult <- receiveWait
392 [ match (return . Right)
393 , matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
394 (\(ProcessMonitorNotification _ _ reason) -> return (Left reason))
398 -- Wait for the monitor message so that we the mailbox doesn't grow
400 [ matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
401 (\(ProcessMonitorNotification {}) -> return ())
403 -- Clean up connection to pid
407 fail $ "call: remote process died: " ++ show err
409-- | Spawn a child process, have the child link to the parent and the parent
411spawnSupervised :: NodeId
412 -> Closure (Process ())
413 -> Process (ProcessId, MonitorRef)
414spawnSupervised nid proc = do
416 them <- spawn nid (cpLink us `seqCP` proc)
420-- | Spawn a new process, supplying it with a new 'ReceivePort' and return
421-- the corresponding 'SendPort'.
422spawnChannel :: forall a. Typeable a => Static (SerializableDict a)
424 -> Closure (ReceivePort a -> Process ())
425 -> Process (SendPort a)
426spawnChannel dict nid proc = do
428 _ <- spawn nid (go sp)
431 go :: SendPort (SendPort a) -> Closure (Process ())
432 go chan = cpNewChan dict
434 (cpSendChan (sdictSendPort dict) chan `splitCP` proc)
436 (idCP `closureCompose` staticClosure sndStatic)
438--------------------------------------------------------------------------------
439-- Local versions of spawn --
440--------------------------------------------------------------------------------
442-- | Spawn a process on the local node
443spawnLocal :: Process () -> Process ProcessId
445 node <- processNode <$> ask
446 liftIO $ forkProcess node proc
448-- | Create a new typed channel, spawn a process on the local node, passing it
449-- the receive port, and return the send port
450spawnChannelLocal :: Serializable a
451 => (ReceivePort a -> Process ())
452 -> Process (SendPort a)
453spawnChannelLocal proc = do
454 node <- processNode <$> ask
457 _ <- forkProcess node $ do
458 -- It is important that we allocate the new channel in the new process,
459 -- because otherwise it will be associated with the wrong process ID
460 (sport, rport) <- newChan
461 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