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
58 -- * Process management
67 , ProcessTerminationException(..)
68 , ProcessRegistrationException(..)
74 -- * Monitoring and linking
86 , MonitorRef -- opaque
87 , ProcessLinkException(..)
88 , NodeLinkException(..)
89 , PortLinkException(..)
90 , ProcessMonitorNotification(..)
91 , NodeMonitorNotification(..)
92 , PortMonitorNotification(..)
109 , registerRemoteAsync
110 , reregisterRemoteAsync
111 , unregisterRemoteAsync
116 -- * 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 , ProcessLinkException(..)
167 , NodeLinkException(..)
168 , PortLinkException(..)
169 , ProcessRegistrationException(..)
178 , LocalProcess(processNode)
182import Control.Distributed.Process.Serializable (Serializable, SerializableDict)
183import Control.Distributed.Process.Internal.Closure.BuiltIn
195import Control.Distributed.Process.Internal.Primitives
205 -- Advanced messaging
224 -- Process management
226 , ProcessTerminationException(..)
236 -- Monitoring and linking
256 , registerRemoteAsync
257 , reregisterRemoteAsync
258 , unregisterRemoteAsync
264 -- Exception handling
282import Control.Distributed.Process.Node (forkProcess)
283import Control.Distributed.Process.Internal.Spawn
284 ( -- Spawning Processes/Channels
295-- 1. 'send' never fails. If you want to know that the remote process received
296-- your message, you will need to send an explicit acknowledgement. If you
297-- want to know when the remote process failed, you will need to monitor
298-- that remote process.
300-- 2. 'send' may block (when the system TCP buffers are full, while we are
301-- trying to establish a connection to the remote endpoint, etc.) but its
302-- return does not imply that the remote process received the message (much
303-- less processed it). When 'send' targets a local process, it is dispatched
304-- via the node controller however, in which cases it will /not/ block. This
305-- isn't part of the semantics, so it should be ok. The ordering is maintained
306-- because the ctrlChannel still has FIFO semantics with regards interactions
307-- between two disparate forkIO threads.
309-- 3. Message delivery is reliable and ordered. That means that if process A
310-- sends messages m1, m2, m3 to process B, B will either arrive all three
311-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
312-- 'missing' (m1, m3) or reordered (m1, m3, m2)
314-- In order to guarantee (3), we stipulate that
316-- 3a. We do not garbage collect connections because Network.Transport provides
317-- ordering guarantees only *per connection*.
319-- 3b. Once a connection breaks, we have no way of knowing which messages
320-- arrived and which did not; hence, once a connection fails, we assume the
321-- remote process to be forever unreachable. Otherwise we might sent m1 and
322-- m2, get notified of the broken connection, reconnect, send m3, but only
325-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
326-- we consider it forever unreachable. Hence, new processes should get new
327-- IDs or they too would be considered unreachable.
329-- Main reference for Cloud Haskell is
331-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
333-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
335-- The precise semantics for message passing is based on
337-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
338-- and Clara Benac Earle (not freely available online, unfortunately)
340-- Some pointers to related documentation about Erlang, for comparison and
343-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
344-- Hans Svensson and Lars-Ake Fredlund
345-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
346-- [4] The Erlang manual, sections "Message Sending" and "Send"
347-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
348-- http://www.erlang.org/doc/reference_manual/expressions.html#send
349-- [5] Questions "Is the order of message reception guaranteed?" and
350-- "If I send a message, is it guaranteed to reach the receiver?" of
352-- http://www.erlang.org/faq/academic.html
353-- [6] "Delivery of Messages", post on erlang-questions
354-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
356--------------------------------------------------------------------------------
357-- Local versions of spawn --
358--------------------------------------------------------------------------------
360-- | Spawn a process on the local node
361spawnLocal :: Process () -> Process ProcessId
363 node <- processNode <$> ask
364 liftIO $ forkProcess node proc
366-- | Create a new typed channel, spawn a process on the local node, passing it
367-- the receive port, and return the send port
368spawnChannelLocal :: Serializable a
369 => (ReceivePort a -> Process ())
370 -> Process (SendPort a)
371spawnChannelLocal proc = do
372 node <- processNode <$> ask
375 _ <- forkProcess node $ do
376 -- It is important that we allocate the new channel in the new process,
377 -- because otherwise it will be associated with the wrong process ID
378 (sport, rport) <- newChan
379 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