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 , ProcessExitException()
86 , ProcessLinkException(..)
87 , NodeLinkException(..)
88 , PortLinkException(..)
89 , ProcessMonitorNotification(..)
90 , NodeMonitorNotification(..)
91 , PortMonitorNotification(..)
108 , registerRemoteAsync
109 , reregisterRemoteAsync
110 , unregisterRemoteAsync
115 -- * Exception handling
133 -- * Local versions of 'spawn'
141#if ! MIN_VERSION_base(4,6,0)
142import Prelude hiding (catch)
145import Data.Typeable (Typeable)
146import Control.Monad.IO.Class (liftIO)
147import Control.Applicative ((<$>))
148import Control.Monad.Reader (ask)
149import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
150import Control.Distributed.Static
158import Control.Distributed.Process.Internal.Types
163 , ProcessMonitorNotification(..)
164 , NodeMonitorNotification(..)
165 , PortMonitorNotification(..)
166 , ProcessExitException()
168 , ProcessLinkException(..)
169 , NodeLinkException(..)
170 , PortLinkException(..)
171 , ProcessRegistrationException(..)
180 , LocalProcess(processNode)
184import Control.Distributed.Process.Serializable (Serializable, SerializableDict)
185import Control.Distributed.Process.Internal.Closure.BuiltIn
197import Control.Distributed.Process.Internal.Primitives
207 -- Advanced messaging
223 -- Process management
225 , ProcessTerminationException(..)
235 -- Monitoring and linking
255 , registerRemoteAsync
256 , reregisterRemoteAsync
257 , unregisterRemoteAsync
263 -- Exception handling
281import Control.Distributed.Process.Node (forkProcess)
285-- 1. 'send' never fails. If you want to know that the remote process received
286-- your message, you will need to send an explicit acknowledgement. If you
287-- want to know when the remote process failed, you will need to monitor
288-- that remote process.
290-- 2. 'send' may block (when the system TCP buffers are full, while we are
291-- trying to establish a connection to the remote endpoint, etc.) but its
292-- return does not imply that the remote process received the message (much
295-- 3. Message delivery is reliable and ordered. That means that if process A
296-- sends messages m1, m2, m3 to process B, B will either arrive all three
297-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
298-- 'missing' (m1, m3) or reordered (m1, m3, m2)
300-- In order to guarantee (3), we stipulate that
302-- 3a. We do not garbage collect connections because Network.Transport provides
303-- ordering guarantees only *per connection*.
305-- 3b. Once a connection breaks, we have no way of knowing which messages
306-- arrived and which did not; hence, once a connection fails, we assume the
307-- remote process to be forever unreachable. Otherwise we might sent m1 and
308-- m2, get notified of the broken connection, reconnect, send m3, but only
311-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
312-- we consider it forever unreachable. Hence, new processes should get new
313-- IDs or they too would be considered unreachable.
315-- Main reference for Cloud Haskell is
317-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
319-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
321-- The precise semantics for message passing is based on
323-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
324-- and Clara Benac Earle (not freely available online, unfortunately)
326-- Some pointers to related documentation about Erlang, for comparison and
329-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
330-- Hans Svensson and Lars-Ake Fredlund
331-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
332-- [4] The Erlang manual, sections "Message Sending" and "Send"
333-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
334-- http://www.erlang.org/doc/reference_manual/expressions.html#send
335-- [5] Questions "Is the order of message reception guaranteed?" and
336-- "If I send a message, is it guaranteed to reach the receiver?" of
338-- http://www.erlang.org/faq/academic.html
339-- [6] "Delivery of Messages", post on erlang-questions
340-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
342--------------------------------------------------------------------------------
343-- Primitives are defined in a separate module; here we only define derived --
345--------------------------------------------------------------------------------
349-- For more information about 'Closure', see
350-- "Control.Distributed.Process.Closure".
353spawn :: NodeId -> Closure (Process ()) -> Process ProcessId
356 mRef <- monitorNode nid
357 sRef <- spawnAsync nid (cpDelay us proc)
359 matchIf (\(DidSpawn ref _) -> ref == sRef) $ \(DidSpawn _ pid) -> do
363 , matchIf (\(NodeMonitorNotification ref _ _) -> ref == mRef) $ \_ ->
364 return (nullProcessId nid)
367-- | Spawn a process and link to it
369-- Note that this is just the sequential composition of 'spawn' and 'link'.
370-- (The "Unified" semantics that underlies Cloud Haskell does not even support
371-- a synchronous link operation)
372spawnLink :: NodeId -> Closure (Process ()) -> Process ProcessId
373spawnLink nid proc = do
374 pid <- spawn nid proc
378-- | Like 'spawnLink', but monitor the spawned process
379spawnMonitor :: NodeId -> Closure (Process ()) -> Process (ProcessId, MonitorRef)
380spawnMonitor nid proc = do
381 pid <- spawn nid proc
385-- | Run a process remotely and wait for it to reply
387-- We monitor the remote process: if it dies before it can send a reply, we die
390-- For more information about 'Static', 'SerializableDict', and 'Closure', see
391-- "Control.Distributed.Process.Closure".
394call :: Serializable a
395 => Static (SerializableDict a)
397 -> Closure (Process a)
399call dict nid proc = do
401 (pid, mRef) <- spawnMonitor nid (proc `bindCP` cpSend dict us)
402 -- We are guaranteed to receive the reply before the monitor notification
403 -- (if a reply is sent at all)
404 -- NOTE: This might not be true if we switch to unreliable delivery.
405 mResult <- receiveWait
406 [ match (return . Right)
407 , matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
408 (\(ProcessMonitorNotification _ _ reason) -> return (Left reason))
412 -- Wait for the monitor message so that we the mailbox doesn't grow
414 [ matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
415 (\(ProcessMonitorNotification {}) -> return ())
417 -- Clean up connection to pid
421 fail $ "call: remote process died: " ++ show err
423-- | Spawn a child process, have the child link to the parent and the parent
425spawnSupervised :: NodeId
426 -> Closure (Process ())
427 -> Process (ProcessId, MonitorRef)
428spawnSupervised nid proc = do
430 them <- spawn nid (cpLink us `seqCP` proc)
434-- | Spawn a new process, supplying it with a new 'ReceivePort' and return
435-- the corresponding 'SendPort'.
436spawnChannel :: forall a. Typeable a => Static (SerializableDict a)
438 -> Closure (ReceivePort a -> Process ())
439 -> Process (SendPort a)
440spawnChannel dict nid proc = do
442 _ <- spawn nid (go us)
445 go :: ProcessId -> Closure (Process ())
446 go pid = cpNewChan dict
448 (cpSend (sdictSendPort dict) pid `splitCP` proc)
450 (idCP `closureCompose` staticClosure sndStatic)
452--------------------------------------------------------------------------------
453-- Local versions of spawn --
454--------------------------------------------------------------------------------
456-- | Spawn a process on the local node
457spawnLocal :: Process () -> Process ProcessId
459 node <- processNode <$> ask
460 liftIO $ forkProcess node proc
462-- | Create a new typed channel, spawn a process on the local node, passing it
463-- the receive port, and return the send port
464spawnChannelLocal :: Serializable a
465 => (ReceivePort a -> Process ())
466 -> Process (SendPort a)
467spawnChannelLocal proc = do
468 node <- processNode <$> ask
471 _ <- forkProcess node $ do
472 -- It is important that we allocate the new channel in the new process,
473 -- because otherwise it will be associated with the wrong process ID
474 (sport, rport) <- newChan
475 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