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
48 -- * Advanced messaging
74 -- * Process management
83 , ProcessTerminationException(..)
84 , ProcessRegistrationException(..)
93 -- * Monitoring and linking
106 , MonitorRef -- opaque
107 , ProcessLinkException(..)
108 , NodeLinkException(..)
109 , PortLinkException(..)
110 , ProcessMonitorNotification(..)
111 , NodeMonitorNotification(..)
112 , PortMonitorNotification(..)
129 , registerRemoteAsync
130 , reregisterRemoteAsync
131 , unregisterRemoteAsync
136 -- * Exception handling
154 -- * Local versions of 'spawn'
163import Control.Monad.IO.Class (liftIO)
164import Control.Applicative
165import Control.Monad.Reader (ask)
166import Control.Concurrent (killThread)
167import Control.Concurrent.MVar
173import Control.Distributed.Static
179import Control.Distributed.Process.Internal.Types
184 , ProcessMonitorNotification(..)
185 , NodeMonitorNotification(..)
186 , PortMonitorNotification(..)
187 , ProcessLinkException(..)
188 , NodeLinkException(..)
189 , PortLinkException(..)
190 , ProcessRegistrationException(..)
199 , LocalProcess(processNode)
203import Control.Distributed.Process.Serializable (Serializable)
204import Control.Distributed.Process.Internal.Primitives
220 -- Advanced messaging
246 -- Process management
248 , ProcessTerminationException(..)
261 -- Monitoring and linking
282 , registerRemoteAsync
283 , reregisterRemoteAsync
284 , unregisterRemoteAsync
290 -- Exception handling
309import Control.Distributed.Process.Node (forkProcess)
310import Control.Distributed.Process.Internal.Types
312 , withValidLocalState
314import Control.Distributed.Process.Internal.Spawn
315 ( -- Spawning Processes/Channels
323import qualified Control.Monad.Catch as Catch
325#if MIN_VERSION_base(4,6,0)
328import Prelude hiding (catch)
330import qualified Control.Exception as Exception (onException)
331import Data.Accessor ((^.))
332import Data.Foldable (forM_)
337-- 1. 'send' never fails. If you want to know that the remote process received
338-- your message, you will need to send an explicit acknowledgement. If you
339-- want to know when the remote process failed, you will need to monitor
340-- that remote process.
342-- 2. 'send' may block (when the system TCP buffers are full, while we are
343-- trying to establish a connection to the remote endpoint, etc.) but its
344-- return does not imply that the remote process received the message (much
345-- less processed it). When 'send' targets a local process, it is dispatched
346-- via the node controller however, in which cases it will /not/ block. This
347-- isn't part of the semantics, so it should be ok. The ordering is maintained
348-- because the ctrlChannel still has FIFO semantics with regards interactions
349-- between two disparate forkIO threads.
351-- 3. Message delivery is reliable and ordered. That means that if process A
352-- sends messages m1, m2, m3 to process B, B will either arrive all three
353-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
354-- 'missing' (m1, m3) or reordered (m1, m3, m2)
356-- In order to guarantee (3), we stipulate that
358-- 3a. We do not garbage collect connections because Network.Transport provides
359-- ordering guarantees only *per connection*.
361-- 3b. Once a connection breaks, we have no way of knowing which messages
362-- arrived and which did not; hence, once a connection fails, we assume the
363-- remote process to be forever unreachable. Otherwise we might sent m1 and
364-- m2, get notified of the broken connection, reconnect, send m3, but only
367-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
368-- we consider it forever unreachable. Hence, new processes should get new
369-- IDs or they too would be considered unreachable.
371-- Main reference for Cloud Haskell is
373-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
375-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
377-- The precise semantics for message passing is based on
379-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
380-- and Clara Benac Earle (not freely available online, unfortunately)
382-- Some pointers to related documentation about Erlang, for comparison and
385-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
386-- Hans Svensson and Lars-Ake Fredlund
387-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
388-- [4] The Erlang manual, sections "Message Sending" and "Send"
389-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
390-- http://www.erlang.org/doc/reference_manual/expressions.html#send
391-- [5] Questions "Is the order of message reception guaranteed?" and
392-- "If I send a message, is it guaranteed to reach the receiver?" of
394-- http://www.erlang.org/faq/academic.html
395-- [6] "Delivery of Messages", post on erlang-questions
396-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
398--------------------------------------------------------------------------------
399-- Local versions of spawn --
400--------------------------------------------------------------------------------
402-- | Spawn a process on the local node
403spawnLocal :: Process () -> Process ProcessId
405 node <- processNode <$> ask
406 liftIO $ forkProcess node proc
408-- | Create a new typed channel, spawn a process on the local node, passing it
409-- the receive port, and return the send port
410spawnChannelLocal :: Serializable a
411 => (ReceivePort a -> Process ())
412 -> Process (SendPort a)
413spawnChannelLocal proc = do
414 node <- processNode <$> ask
417 _ <- forkProcess node $ do
418 -- It is important that we allocate the new channel in the new process,
419 -- because otherwise it will be associated with the wrong process ID
420 (sport, rport) <- newChan
421 liftIO $ putMVar mvar sport
425-- | Local version of 'call'. Running a process in this way isolates it from
426-- messages sent to the caller process, and also allows silently dropping late
427-- or duplicate messages sent to the isolated process after it exits.
428-- Silently dropping messages may not always be the best approach.
429callLocal :: Process a -> Process a
430callLocal proc = Catch.mask $ \release -> do
431 mv <- liftIO newEmptyMVar :: Process (MVar (Either Catch.SomeException a))
432 child <- spawnLocal $ Catch.try (release proc) >>= liftIO . putMVar mv
435 rs <- Exception.onException (takeMVar mv) $ Catch.uninterruptibleMask_ $
436 -- Exceptions need to be prevented from interrupting the clean up or
437 -- the original exception which caused entering the handler could be
438 -- forgotten. For instance, this could have a problematic effect
439 -- when the original exception was meant to kill the thread and the
440 -- second exception doesn't (like the exception thrown by
441 -- 'System.Timeout.timeout').
442 do mchildThreadId <- withValidLocalState (processNode lproc) $
443 \vst -> return $ fmap processThread $
444 vst ^. localProcessWithId (processLocalId child)
445 forM_ mchildThreadId killThread
447 either Catch.throwM return rs
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