4This is an implementation of Cloud Haskell, as described in
5/Towards Haskell in the Cloud/ by Jeff Epstein, Andrew Black, and Simon
7<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
41 -- * Unsafe messaging variants
46 -- * Advanced messaging
71 -- * Process management
80 , ProcessTerminationException(..)
81 , ProcessRegistrationException(..)
90 -- * Monitoring and linking
102 , MonitorRef -- opaque
103 , ProcessLinkException(..)
104 , NodeLinkException(..)
105 , PortLinkException(..)
106 , ProcessMonitorNotification(..)
107 , NodeMonitorNotification(..)
108 , PortMonitorNotification(..)
125 , registerRemoteAsync
126 , reregisterRemoteAsync
127 , unregisterRemoteAsync
132 -- * Exception handling
150 -- * Local versions of 'spawn'
159#if ! MIN_VERSION_base(4,6,0)
160import Prelude hiding (catch)
163import Control.Monad.IO.Class (liftIO)
164import Control.Applicative ((<$>))
165import Control.Monad.Reader (ask)
166import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar)
167import Control.Distributed.Static
173import Control.Distributed.Process.Internal.Types
178 , ProcessMonitorNotification(..)
179 , NodeMonitorNotification(..)
180 , PortMonitorNotification(..)
181 , ProcessLinkException(..)
182 , NodeLinkException(..)
183 , PortLinkException(..)
184 , ProcessRegistrationException(..)
193 , LocalProcess(processNode)
196import Control.Distributed.Process.Serializable (Serializable)
197import Control.Distributed.Process.Internal.Primitives
211 -- Advanced messaging
236 -- Process management
238 , ProcessTerminationException(..)
251 -- Monitoring and linking
271 , registerRemoteAsync
272 , reregisterRemoteAsync
273 , unregisterRemoteAsync
279 -- Exception handling
298import Control.Distributed.Process.Node (forkProcess)
299import Control.Distributed.Process.Internal.Spawn
300 ( -- Spawning Processes/Channels
308import Control.Exception (SomeException, throwIO)
312-- 1. 'send' never fails. If you want to know that the remote process received
313-- your message, you will need to send an explicit acknowledgement. If you
314-- want to know when the remote process failed, you will need to monitor
315-- that remote process.
317-- 2. 'send' may block (when the system TCP buffers are full, while we are
318-- trying to establish a connection to the remote endpoint, etc.) but its
319-- return does not imply that the remote process received the message (much
320-- less processed it). When 'send' targets a local process, it is dispatched
321-- via the node controller however, in which cases it will /not/ block. This
322-- isn't part of the semantics, so it should be ok. The ordering is maintained
323-- because the ctrlChannel still has FIFO semantics with regards interactions
324-- between two disparate forkIO threads.
326-- 3. Message delivery is reliable and ordered. That means that if process A
327-- sends messages m1, m2, m3 to process B, B will either arrive all three
328-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
329-- 'missing' (m1, m3) or reordered (m1, m3, m2)
331-- In order to guarantee (3), we stipulate that
333-- 3a. We do not garbage collect connections because Network.Transport provides
334-- ordering guarantees only *per connection*.
336-- 3b. Once a connection breaks, we have no way of knowing which messages
337-- arrived and which did not; hence, once a connection fails, we assume the
338-- remote process to be forever unreachable. Otherwise we might sent m1 and
339-- m2, get notified of the broken connection, reconnect, send m3, but only
342-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
343-- we consider it forever unreachable. Hence, new processes should get new
344-- IDs or they too would be considered unreachable.
346-- Main reference for Cloud Haskell is
348-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
350-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
352-- The precise semantics for message passing is based on
354-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
355-- and Clara Benac Earle (not freely available online, unfortunately)
357-- Some pointers to related documentation about Erlang, for comparison and
360-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
361-- Hans Svensson and Lars-Ake Fredlund
362-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
363-- [4] The Erlang manual, sections "Message Sending" and "Send"
364-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
365-- http://www.erlang.org/doc/reference_manual/expressions.html#send
366-- [5] Questions "Is the order of message reception guaranteed?" and
367-- "If I send a message, is it guaranteed to reach the receiver?" of
369-- http://www.erlang.org/faq/academic.html
370-- [6] "Delivery of Messages", post on erlang-questions
371-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
373--------------------------------------------------------------------------------
374-- Local versions of spawn --
375--------------------------------------------------------------------------------
377-- | Spawn a process on the local node
378spawnLocal :: Process () -> Process ProcessId
380 node <- processNode <$> ask
381 liftIO $ forkProcess node proc
383-- | Create a new typed channel, spawn a process on the local node, passing it
384-- the receive port, and return the send port
385spawnChannelLocal :: Serializable a
386 => (ReceivePort a -> Process ())
387 -> Process (SendPort a)
388spawnChannelLocal proc = do
389 node <- processNode <$> ask
392 _ <- forkProcess node $ do
393 -- It is important that we allocate the new channel in the new process,
394 -- because otherwise it will be associated with the wrong process ID
395 (sport, rport) <- newChan
396 liftIO $ putMVar mvar sport
400-- | Local version of 'call'. Running a process in this way isolates it from
401-- messages sent to the caller process, and also allows silently dropping late
402-- or duplicate messages sent to the isolated process after it exits.
403-- Silently dropping messages may not always be the best approach.
405 Process a -- ^ Process to run
406 -> Process a -- ^ Value returned
408 mv <- liftIO newEmptyMVar
412 try proc >>= liftIO . putMVar mv
413 liftIO $ takeMVar mv >>=
414 either (throwIO :: SomeException -> IO a) return
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