aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/concurrency/async.lux
blob: 75bec2d4b1fb5d5cc7a7ec796e4f49cb1235fe9d (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
(.require
 [library
  [lux (.except and or)
   [abstract
    [functor (.only Functor)]
    [apply (.only Apply)]
    ["[0]" monad (.only Monad do)]]
   [control
    ["[0]" pipe]
    ["[0]" function]
    ["[0]" io (.only IO io)]
    ["[0]" maybe (.use "[1]#[0]" functor)]]
   [data
    ["[0]" product]]
   [meta
    [macro
     ["^" pattern]]
    [type (.only sharing)
     [primitive (.except)]
     ["[0]" variance (.only Mutable)]]]]]
 [//
  ["[0]" thread]
  ["[0]" atom (.only Atom atom)]])

(def Value
  (template (_ a)
    [(Maybe a)]))

(def Handler
  (template (_ a)
    [(-> a (IO Any))]))

(primitive (Async'' a)
  (Atom [(Value a) (List (Handler a))])

  (type .public (Async' r w)
    (Async'' (Mutable r w)))

  (type .public (Async a)
    (Async'' (Mutable a a)))

  (type .public (Resolver w)
    (-> w (IO Bit)))

  ... Sets an async's value if it has not been done yet.
  (def (resolver async)
    (All (_ r w) (-> (Async' r w) (Resolver w)))
    (function (resolve value)
      (let [async (representation async)]
        (do [! io.monad]
          [(^.let old [_value _observers]) (atom.read! async)]
          (when _value
            {.#Some _}
            (in false)

            {.#None}
            (do !
              [succeeded? (atom.compare_and_swap! old [{.#Some (variance.write value)} (list)] async)]
              (if succeeded?
                (do !
                  [_ (monad.each ! (function.on (variance.write value))
                                 _observers)]
                  (in true))
                (resolve value))))))))

  (def .public (resolved value)
    (All (_ a) (-> a (Async a)))
    (abstraction (atom [{.#Some (variance.write value)} (list)])))

  (def .public (async _)
    (All (_ r w) (-> Any [(Async' r w) (Resolver w)]))
    (let [async (abstraction (atom [{.#None} (list)]))]
      [async (..resolver async)]))

  (def .public value
    (All (_ r w) (-> (Async' r w) (IO (Value r))))
    (|>> representation
         atom.read!
         (at io.functor each (|>> product.left
                                  (maybe#each (|>> variance.read))))))

  (def .public (upon! f async)
    (All (_ r w) (-> (Handler r) (Async' r w) (IO Any)))
    (do [! io.monad]
      [.let [async (representation async)]
       (^.let old [_value _observers]) (atom.read! async)]
      (when _value
        {.#Some value}
        (f (variance.read value))

        {.#None}
        (do !
          [swapped? (atom.compare_and_swap! old [_value {.#Item (|>> variance.read f) _observers}] async)]
          (if swapped?
            (in [])
            (upon! f (abstraction async)))))))
  )

(def .public resolved?
  (All (_ r w) (-> (Async' r w) (IO Bit)))
  (|>> ..value
       (at io.functor each
           (|>> (pipe.when
                  {.#None}
                  false

                  {.#Some _}
                  true)))))

(def .public functor
  (Functor Async)
  (implementation
   (def (each f fa)
     (let [[fb resolve] (sharing [a b]
                          (is (-> a b)
                              f)
                          (is [(Async b) (Resolver b)]
                              (..async [])))]
       (exec
         (io.run! (..upon! (|>> f resolve) fa))
         fb)))))

(def .public apply
  (Apply Async)
  (implementation
   (def functor ..functor)

   (def (on fa ff)
     (let [[fb resolve] (sharing [a b]
                          (is (Async (-> a b))
                              ff)
                          (is [(Async b) (Resolver b)]
                              (..async [])))]
       (exec
         (io.run! (..upon! (function (_ f)
                             (..upon! (|>> f resolve) fa))
                           ff))
         fb)))))

(def .public monad
  (Monad Async)
  (implementation
   (def functor ..functor)

   (def in ..resolved)

   (def (conjoint mma)
     (let [[ma resolve] (sharing [a]
                          (is (Async (Async a))
                              mma)
                          (is [(Async a) (Resolver a)]
                              (..async [])))]
       (exec
         (io.run! (..upon! (..upon! resolve) mma))
         ma)))))

(def .public (and left right)
  (All (_ lr lw rr rw) (-> (Async' lr lw) (Async' rr rw) (Async [lr rr])))
  (let [[read! write!] (sharing [lr lw rr rw]
                         (is [(Async' lr lw) (Async' rr rw)]
                             [left right])
                         (is [(Async [lr rr]) (Resolver [lr rr])]
                             (..async [])))
        _ (io.run! (..upon! (function (_ left)
                              (..upon! (function (_ right)
                                         (write! [left right]))
                                       right))
                            left))]
    read!))

(def .public (or left right)
  (All (_ lr lw rr rw) (-> (Async' lr lw) (Async' rr rw) (Async (Or lr rr))))
  (let [[left|right resolve] (sharing [lr lw rr rw]
                               (is [(Async' lr lw) (Async' rr rw)]
                                   [left right])
                               (is [(Async (Or lr rr)) (Resolver (Or lr rr))]
                                   (..async [])))]
    (with_expansions
      [<sides> (with_template [<async> <tag>]
                 [(io.run! (upon! (|>> {<tag>} resolve) <async>))]

                 [left  .#Left]
                 [right .#Right]
                 )]
      (exec
        <sides>
        left|right))))

(def .public (either left right)
  (All (_ a lw rw) (-> (Async' a lw) (Async' a rw) (Async a)))
  (let [[left||right resolve] (sharing [a lw rw]
                                (is [(Async' a lw) (Async' a rw)]
                                    [left right])
                                (is [(Async a) (Resolver a)]
                                    (..async [])))]
    (`` (exec
          (,, (with_template [<async>]
                [(io.run! (upon! resolve <async>))]

                [left]
                [right]))
          left||right))))

(def .public (schedule! milli_seconds computation)
  (All (_ a) (-> Nat (IO a) (Async a)))
  (let [[!out resolve] (sharing [a]
                         (is (IO a)
                             computation)
                         (is [(Async a) (Resolver a)]
                             (..async [])))]
    (exec
      (|> (do io.monad
            [value computation]
            (resolve value))
          (thread.schedule! milli_seconds)
          io.run!)
      !out)))

(def .public future
  (All (_ a) (-> (IO a) (Async a)))
  (..schedule! 0))

(def .public (after milli_seconds value)
  (All (_ a) (-> Nat a (Async a)))
  (..schedule! milli_seconds (io value)))

(def .public (delay milli_seconds)
  (-> Nat (Async Any))
  (..after milli_seconds []))

(def .public (within milli_seconds async)
  (All (_ r w) (-> Nat (Async' r w) (Async (Maybe r))))
  (..or (..delay milli_seconds)
        async))