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
105 , MonitorRef -- opaque
106 , ProcessLinkException(..)
107 , NodeLinkException(..)
108 , PortLinkException(..)
109 , ProcessMonitorNotification(..)
110 , NodeMonitorNotification(..)
111 , PortMonitorNotification(..)
128 , registerRemoteAsync
129 , reregisterRemoteAsync
130 , unregisterRemoteAsync
135 -- * Exception handling
153 -- * Local versions of 'spawn'
162#if ! MIN_VERSION_base(4,6,0)
163import Prelude hiding (catch)
166import Control.Monad.IO.Class (liftIO)
167import Control.Applicative ((<$>))
168import Control.Monad.Reader (ask)
169import Control.Concurrent.MVar
175import Control.Distributed.Static
181import Control.Distributed.Process.Internal.Types
186 , ProcessMonitorNotification(..)
187 , NodeMonitorNotification(..)
188 , PortMonitorNotification(..)
189 , ProcessLinkException(..)
190 , NodeLinkException(..)
191 , PortLinkException(..)
192 , ProcessRegistrationException(..)
201 , LocalProcess(processNode)
204import Control.Distributed.Process.Serializable (Serializable)
205import Control.Distributed.Process.Internal.Primitives
221 -- Advanced messaging
247 -- Process management
249 , ProcessTerminationException(..)
262 -- Monitoring and linking
282 , registerRemoteAsync
283 , reregisterRemoteAsync
284 , unregisterRemoteAsync
290 -- Exception handling
309import Control.Distributed.Process.Node (forkProcess)
310import Control.Distributed.Process.Internal.Spawn
311 ( -- Spawning Processes/Channels
319import Control.Exception (SomeException, throw)
323-- 1. 'send' never fails. If you want to know that the remote process received
324-- your message, you will need to send an explicit acknowledgement. If you
325-- want to know when the remote process failed, you will need to monitor
326-- that remote process.
328-- 2. 'send' may block (when the system TCP buffers are full, while we are
329-- trying to establish a connection to the remote endpoint, etc.) but its
330-- return does not imply that the remote process received the message (much
331-- less processed it). When 'send' targets a local process, it is dispatched
332-- via the node controller however, in which cases it will /not/ block. This
333-- isn't part of the semantics, so it should be ok. The ordering is maintained
334-- because the ctrlChannel still has FIFO semantics with regards interactions
335-- between two disparate forkIO threads.
337-- 3. Message delivery is reliable and ordered. That means that if process A
338-- sends messages m1, m2, m3 to process B, B will either arrive all three
339-- messages in order (m1, m2, m3) or a prefix thereof; messages will not be
340-- 'missing' (m1, m3) or reordered (m1, m3, m2)
342-- In order to guarantee (3), we stipulate that
344-- 3a. We do not garbage collect connections because Network.Transport provides
345-- ordering guarantees only *per connection*.
347-- 3b. Once a connection breaks, we have no way of knowing which messages
348-- arrived and which did not; hence, once a connection fails, we assume the
349-- remote process to be forever unreachable. Otherwise we might sent m1 and
350-- m2, get notified of the broken connection, reconnect, send m3, but only
353-- 3c. As a consequence of (3b) we should not reuse PIDs. If a process dies,
354-- we consider it forever unreachable. Hence, new processes should get new
355-- IDs or they too would be considered unreachable.
357-- Main reference for Cloud Haskell is
359-- [1] "Towards Haskell in the Cloud", Jeff Epstein, Andrew Black and Simon
361-- http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/remote.pdf
363-- The precise semantics for message passing is based on
365-- [2] "A Unified Semantics for Future Erlang", Hans Svensson, Lars-Ake Fredlund
366-- and Clara Benac Earle (not freely available online, unfortunately)
368-- Some pointers to related documentation about Erlang, for comparison and
371-- [3] "Programming Distributed Erlang Applications: Pitfalls and Recipes",
372-- Hans Svensson and Lars-Ake Fredlund
373-- http://man.lupaworld.com/content/develop/p37-svensson.pdf
374-- [4] The Erlang manual, sections "Message Sending" and "Send"
375-- http://www.erlang.org/doc/reference_manual/processes.html#id82409
376-- http://www.erlang.org/doc/reference_manual/expressions.html#send
377-- [5] Questions "Is the order of message reception guaranteed?" and
378-- "If I send a message, is it guaranteed to reach the receiver?" of
380-- http://www.erlang.org/faq/academic.html
381-- [6] "Delivery of Messages", post on erlang-questions
382-- http://erlang.org/pipermail/erlang-questions/2012-February/064767.html
384--------------------------------------------------------------------------------
385-- Local versions of spawn --
386--------------------------------------------------------------------------------
388-- | Spawn a process on the local node
389spawnLocal :: Process () -> Process ProcessId
391 node <- processNode <$> ask
392 liftIO $ forkProcess node proc
394-- | Create a new typed channel, spawn a process on the local node, passing it
395-- the receive port, and return the send port
396spawnChannelLocal :: Serializable a
397 => (ReceivePort a -> Process ())
398 -> Process (SendPort a)
399spawnChannelLocal proc = do
400 node <- processNode <$> ask
403 _ <- forkProcess node $ do
404 -- It is important that we allocate the new channel in the new process,
405 -- because otherwise it will be associated with the wrong process ID
406 (sport, rport) <- newChan
407 liftIO $ putMVar mvar sport
411-- | Local version of 'call'. Running a process in this way isolates it from
412-- messages sent to the caller process, and also allows silently dropping late
413-- or duplicate messages sent to the isolated process after it exits.
414-- Silently dropping messages may not always be the best approach.
415callLocal :: Process a -> Process a
416callLocal proc = mask $ \release -> do
417 mv <- liftIO newEmptyMVar :: Process (MVar (Either SomeException a))
418 child <- spawnLocal $ mask_ $ try (release proc) >>= liftIO . putMVar mv
420 rs <- liftIO (takeMVar mv)
422 (do kill child "exception in parent process"
425 either throw return rs
427 -- TODO: when processes spawned with spawnLocal inherit the masking state of
428 -- the parent, this can be simplified to simply reading the MVar.
430 -- At the time of this writing, the spawned process is unmasked and can be
431 -- killed before the MVar is filled.
432 waitFor ref = receiveWait
433 [ matchIf (\(ProcessMonitorNotification ref' _ _) -> ref == ref')
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