blob: d9fb2d28a008b99ff76e68d2df8c20d2377d6183 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
{- | An EDSL to make working with concurrent in-process code a bit easier
to read.
This module is expected to be imported qualified as `Go`. Inspired by
Golang and Clojure's core.async.
$example
-}
{-# LANGUAGE NoImplicitPrelude #-}
module Com.Simatime.Go
(
-- * Running and forking
Go
, run
, fork
-- * Channels
, Channel
, chan
, broadcast
, tap
, read
, write
)
where
import Control.Concurrent ( forkIO
, ThreadId
)
import qualified Control.Concurrent.STM.TChan as TChan
import GHC.Conc ( STM
, atomically
)
import Protolude ( IO )
type Go = STM
type Channel = TChan.TChan
-- | Runs a Go command in IO.
run :: Go a -> IO a
run = atomically
-- | Starts a background process.
fork :: IO () -> IO ThreadId
fork = forkIO
-- | Make a new channel.
chan :: Go (Channel a)
chan = TChan.newTChan
-- | Make a read-only channel.
broadcast :: Go (Channel a)
broadcast = TChan.newBroadcastTChan
-- | Duplicates a channel, but then anything written to the source will
-- be available to both. This is like a combination of Clojure's
-- `core.async/mult` and `core.async/tap` but.
tap :: Channel a -> Go (Channel a)
tap = TChan.dupTChan
-- | Take from a channel. Blocks until a value is received.
read :: Channel a -> Go a
read = TChan.readTChan
-- | Write to a channel.
write :: Channel a -> a -> Go ()
write = TChan.writeTChan
{- $example
A simple example from ghci:
>>> import qualified Com.Simatime.Go as Go
>>> c <- Go.run Go.chan :: IO (Go.Channel Text)
>>> Go.run $ Go.write c "test"
>>> Go.run $ Go.read c
"test"
-}
|