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
49 -- * Process management
58 , ProcessTerminationException(..)
59 , ProcessRegistrationException(..)
65 -- * Monitoring and linking
77 , MonitorRef -- opaque
78 , ProcessLinkException(..)
79 , NodeLinkException(..)
80 , PortLinkException(..)
81 , ProcessMonitorNotification(..)
82 , NodeMonitorNotification(..)
83 , PortMonitorNotification(..)
100 , registerRemoteAsync
101 , reregisterRemoteAsync
102 , unregisterRemoteAsync
107 -- * Exception handling
124 -- * Local versions of 'spawn'
132#if ! MIN_VERSION_base(4,6,0)
133import Prelude hiding (catch)
136import Data.Typeable (Typeable)
137import Control.Monad.IO.Class (liftIO)
138import Control.Applicative ((<$>))
139import Control.Monad.Reader (ask)
140import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
141import Control.Distributed.Static
149import Control.Distributed.Process.Internal.Types
154 , ProcessMonitorNotification(..)
155 , NodeMonitorNotification(..)
156 , PortMonitorNotification(..)
157 , ProcessLinkException(..)
158 , NodeLinkException(..)
159 , PortLinkException(..)
160 , ProcessRegistrationException(..)
169 , LocalProcess(processNode)
172import Control.Distributed.Process.Serializable (Serializable, SerializableDict)
173import Control.Distributed.Process.Internal.Closure.BuiltIn
185import Control.Distributed.Process.Internal.Primitives
195 -- Advanced messaging
202 , AbstractMessage(..)
206 -- Process management
208 , ProcessTerminationException(..)
218 -- Monitoring and linking
238 , registerRemoteAsync
239 , reregisterRemoteAsync
240 , unregisterRemoteAsync
246 -- Exception handling
264import Control.Distributed.Process.Node (forkProcess)
268-- 1. 'send' never fails. If you want to know that the remote process received
269-- your message, you will need to send an explicit acknowledgement. If you
270-- want to know when the remote process failed, you will need to monitor
271-- that remote process.
273-- 2. 'send' may block (when the system TCP buffers are full, while we are
274-- trying to establish a connection to the remote endpoint, etc.) but its
275-- return does not imply that the remote process received the message (much
278-- 3. Message delivery is reliable and ordered. That means that if process A
279-- sends messages m1, m2, m3 to process B, B will either arrive all three
280-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
281-- 'missing' (m1, m3) or reordered (m1, m3, m2)
283-- In order to guarantee (3), we stipulate that
285-- 3a. We do not garbage collect connections because Network.Transport provides
286-- ordering guarantees only *per connection*.
288-- 3b. Once a connection breaks, we have no way of knowing which messages
289-- arrived and which did not; hence, once a connection fails, we assume the
290-- remote process to be forever unreachable. Otherwise we might sent m1 and
291-- m2, get notified of the broken connection, reconnect, send m3, but only
294-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
295-- we consider it forever unreachable. Hence, new processes should get new
296-- IDs or they too would be considered unreachable.
298-- Main reference for Cloud Haskell is
300-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
302-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
304-- The precise semantics for message passing is based on
306-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
307-- and Clara Benac Earle (not freely available online, unfortunately)
309-- Some pointers to related documentation about Erlang, for comparison and
312-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
313-- Hans Svensson and Lars-Ake Fredlund
314-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
315-- [4] The Erlang manual, sections "Message Sending" and "Send"
316-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
317-- http://www.erlang.org/doc/reference_manual/expressions.html#send
318-- [5] Questions "Is the order of message reception guaranteed?" and
319-- "If I send a message, is it guaranteed to reach the receiver?" of
321-- http://www.erlang.org/faq/academic.html
322-- [6] "Delivery of Messages", post on erlang-questions
323-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
325--------------------------------------------------------------------------------
326-- Primitives are defined in a separate module; here we only define derived --
328--------------------------------------------------------------------------------
332-- For more information about 'Closure', see
333-- "Control.Distributed.Process.Closure".
336spawn :: NodeId -> Closure (Process ()) -> Process ProcessId
339 mRef <- monitorNode nid
340 sRef <- spawnAsync nid (cpDelay us proc)
342 matchIf (\(DidSpawn ref _) -> ref == sRef) $ \(DidSpawn _ pid) -> do
346 , matchIf (\(NodeMonitorNotification ref _ _) -> ref == mRef) $ \_ ->
347 return (nullProcessId nid)
350-- | Spawn a process and link to it
352-- Note that this is just the sequential composition of 'spawn' and 'link'.
353-- (The "Unified" semantics that underlies Cloud Haskell does not even support
354-- a synchronous link operation)
355spawnLink :: NodeId -> Closure (Process ()) -> Process ProcessId
356spawnLink nid proc = do
357 pid <- spawn nid proc
361-- | Like 'spawnLink', but monitor the spawned process
362spawnMonitor :: NodeId -> Closure (Process ()) -> Process (ProcessId, MonitorRef)
363spawnMonitor nid proc = do
364 pid <- spawn nid proc
368-- | Run a process remotely and wait for it to reply
370-- We monitor the remote process: if it dies before it can send a reply, we die
373-- For more information about 'Static', 'SerializableDict', and 'Closure', see
374-- "Control.Distributed.Process.Closure".
377call :: Serializable a
378 => Static (SerializableDict a)
380 -> Closure (Process a)
382call dict nid proc = do
384 (pid, mRef) <- spawnMonitor nid (proc `bindCP` cpSend dict us)
385 -- We are guaranteed to receive the reply before the monitor notification
386 -- (if a reply is sent at all)
387 -- NOTE: This might not be true if we switch to unreliable delivery.
388 mResult <- receiveWait
389 [ match (return . Right)
390 , matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
391 (\(ProcessMonitorNotification _ _ reason) -> return (Left reason))
395 -- Wait for the monitor message so that we the mailbox doesn't grow
397 [ matchIf (\(ProcessMonitorNotification ref _ _) -> ref == mRef)
398 (\(ProcessMonitorNotification {}) -> return ())
400 -- Clean up connection to pid
404 fail $ "call: remote process died: " ++ show err
406-- | Spawn a child process, have the child link to the parent and the parent
408spawnSupervised :: NodeId
409 -> Closure (Process ())
410 -> Process (ProcessId, MonitorRef)
411spawnSupervised nid proc = do
413 them <- spawn nid (cpLink us `seqCP` proc)
417-- | Spawn a new process, supplying it with a new 'ReceivePort' and return
418-- the corresponding 'SendPort'.
419spawnChannel :: forall a. Typeable a => Static (SerializableDict a)
421 -> Closure (ReceivePort a -> Process ())
422 -> Process (SendPort a)
423spawnChannel dict nid proc = do
425 _ <- spawn nid (go us)
428 go :: ProcessId -> Closure (Process ())
429 go pid = cpNewChan dict
431 (cpSend (sdictSendPort dict) pid `splitCP` proc)
433 (idCP `closureCompose` staticClosure sndStatic)
435--------------------------------------------------------------------------------
436-- Local versions of spawn --
437--------------------------------------------------------------------------------
439-- | Spawn a process on the local node
440spawnLocal :: Process () -> Process ProcessId
442 node <- processNode <$> ask
443 liftIO $ forkProcess node proc
445-- | Create a new typed channel, spawn a process on the local node, passing it
446-- the receive port, and return the send port
447spawnChannelLocal :: Serializable a
448 => (ReceivePort a -> Process ())
449 -> Process (SendPort a)
450spawnChannelLocal proc = do
451 node <- processNode <$> ask
454 _ <- forkProcess node $ do
455 -- It is important that we allocate the new channel in the new process,
456 -- because otherwise it will be associated with the wrong process ID
457 (sport, rport) <- newChan
458 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