git.iko.soy/distributed-process
on master help sign in
Merge pull request #263 from haskell-distributed/fd/more-unsafe-prims
6deb13dFacundo Domínguez
src/Control/Distributed/Process.hs+4
@@ -40,8 +40,10 @@ module Control.Distributed.Process
40 , mergePortsRR
40 , mergePortsRR
41 -- * Unsafe messaging variants
41 -- * Unsafe messaging variants
42 , unsafeSend
42 , unsafeSend
43+ , unsafeUSend
43 , unsafeSendChan
44 , unsafeSendChan
44 , unsafeNSend
45 , unsafeNSend
46+ , unsafeNSendRemote
45 , unsafeWrapMessage
47 , unsafeWrapMessage
46 -- * Advanced messaging
48 -- * Advanced messaging
47 , Match
49 , Match
@@ -212,8 +214,10 @@ import Control.Distributed.Process.Internal.Primitives
212 , mergePortsBiased
214 , mergePortsBiased
213 , mergePortsRR
215 , mergePortsRR
214 , unsafeSend
216 , unsafeSend
217+ , unsafeUSend
215 , unsafeSendChan
218 , unsafeSendChan
216 , unsafeNSend
219 , unsafeNSend
220+ , unsafeNSendRemote
217 -- Advanced messaging
221 -- Advanced messaging
218 , Match
222 , Match
219 , receiveWait
223 , receiveWait
src/Control/Distributed/Process/Internal/Primitives.hs+16
@@ -23,8 +23,10 @@ module Control.Distributed.Process.Internal.Primitives
23 , mergePortsRR
23 , mergePortsRR
24 -- * Unsafe messaging variants
24 -- * Unsafe messaging variants
25 , unsafeSend
25 , unsafeSend
26+ , unsafeUSend
26 , unsafeSendChan
27 , unsafeSendChan
27 , unsafeNSend
28 , unsafeNSend
29+ , unsafeNSendRemote
28 -- * Advanced messaging
30 -- * Advanced messaging
29 , Match
31 , Match
30 , receiveWait
32 , receiveWait
@@ -279,6 +281,13 @@ usend them msg = do
279 else sendCtrlMsg (Just there) $ UnreliableSend (processLocalId them)
281 else sendCtrlMsg (Just there) $ UnreliableSend (processLocalId them)
280 (createMessage msg)
282 (createMessage msg)
281
283
284+-- | /Unsafe/ variant of 'usend'. This function makes /no/ attempt to serialize
285+-- the message when the destination process resides on the same local
286+-- node. Therefore, a local receiver would need to be prepared to cope with any
287+-- errors resulting from evaluation of the message.
288+unsafeUSend :: Serializable a => ProcessId -> a -> Process ()
289+unsafeUSend = Unsafe.usend
290+
282-- | Wait for a message of a specific type
291-- | Wait for a message of a specific type
283expect :: forall a. Serializable a => Process a
292expect :: forall a. Serializable a => Process a
284expect = receiveWait [match return]
293expect = receiveWait [match return]
@@ -1176,6 +1185,13 @@ nsendRemote :: Serializable a => NodeId -> String -> a -> Process ()
1176nsendRemote nid label msg =
1185nsendRemote nid label msg =
1177 sendCtrlMsg (Just nid) (NamedSend label (createMessage msg))
1186 sendCtrlMsg (Just nid) (NamedSend label (createMessage msg))
1178
1187
1188+-- | Named send to a process in a remote registry (asynchronous)
1189+-- This function makes /no/ attempt to serialize and (in the case when the
1190+-- destination process resides on the same local node) therefore ensure that
1191+-- the payload is fully evaluated before it is delivered.
1192+unsafeNSendRemote :: Serializable a => NodeId -> String -> a -> Process ()
1193+unsafeNSendRemote = Unsafe.nsendRemote
1194+
1179--------------------------------------------------------------------------------
1195--------------------------------------------------------------------------------
1180-- Closures --
1196-- Closures --
1181--------------------------------------------------------------------------------
1197--------------------------------------------------------------------------------
src/Control/Distributed/Process/UnsafePrimitives.hs+31
@@ -45,6 +45,8 @@ module Control.Distributed.Process.UnsafePrimitives
45 send
45 send
46 , sendChan
46 , sendChan
47 , nsend
47 , nsend
48+ , nsendRemote
49+ , usend
48 , wrapMessage
50 , wrapMessage
49 ) where
51 ) where
50
52
@@ -56,6 +58,7 @@ import Control.Distributed.Process.Internal.Messaging
56
58
57import Control.Distributed.Process.Internal.Types
59import Control.Distributed.Process.Internal.Types
58 ( ProcessId(..)
60 ( ProcessId(..)
61+ , NodeId(..)
59 , LocalNode(..)
62 , LocalNode(..)
60 , LocalProcess(..)
63 , LocalProcess(..)
61 , Process(..)
64 , Process(..)
@@ -65,6 +68,7 @@ import Control.Distributed.Process.Internal.Types
65 , ImplicitReconnect(..)
68 , ImplicitReconnect(..)
66 , SendPortId(..)
69 , SendPortId(..)
67 , Message
70 , Message
71+ , createMessage
68 , sendPortProcessId
72 , sendPortProcessId
69 , unsafeCreateUnencodedMessage
73 , unsafeCreateUnencodedMessage
70 )
74 )
@@ -78,6 +82,14 @@ nsend :: Serializable a => String -> a -> Process ()
78nsend label msg =
82nsend label msg =
79 sendCtrlMsg Nothing (NamedSend label (unsafeCreateUnencodedMessage msg))
83 sendCtrlMsg Nothing (NamedSend label (unsafeCreateUnencodedMessage msg))
80
84
85+-- | Named send to a process in a remote registry (asynchronous)
86+nsendRemote :: Serializable a => NodeId -> String -> a -> Process ()
87+nsendRemote nid label msg = do
88+ proc <- ask
89+ if localNodeId (processNode proc) == nid
90+ then nsend label msg
91+ else sendCtrlMsg (Just nid) (NamedSend label (createMessage msg))
92+
81-- | Send a message
93-- | Send a message
82send :: Serializable a => ProcessId -> a -> Process ()
94send :: Serializable a => ProcessId -> a -> Process ()
83send them msg = do
95send them msg = do
@@ -96,6 +108,25 @@ send them msg = do
96 unsafeSendLocal pid msg' =
108 unsafeSendLocal pid msg' =
97 sendCtrlMsg Nothing $ LocalSend pid (unsafeCreateUnencodedMessage msg')
109 sendCtrlMsg Nothing $ LocalSend pid (unsafeCreateUnencodedMessage msg')
98
110
111+-- | Send a message unreliably.
112+--
113+-- Unlike 'send', this function is insensitive to 'reconnect'. It will
114+-- try to send the message regardless of the history of connection failures
115+-- between the nodes.
116+--
117+-- Message passing with 'usend' is ordered for a given sender and receiver
118+-- if the messages arrive at all.
119+--
120+usend :: Serializable a => ProcessId -> a -> Process ()
121+usend them msg = do
122+ proc <- ask
123+ let there = processNodeId them
124+ if localNodeId (processNode proc) == there
125+ then sendCtrlMsg Nothing $
126+ LocalSend them (unsafeCreateUnencodedMessage msg)
127+ else sendCtrlMsg (Just there) $ UnreliableSend (processLocalId them)
128+ (createMessage msg)
129+
99-- | Send a message on a typed channel
130-- | Send a message on a typed channel
100sendChan :: Serializable a => SendPort a -> a -> Process ()
131sendChan :: Serializable a => SendPort a -> a -> Process ()
101sendChan (SendPort cid) msg = do
132sendChan (SendPort cid) msg = do