aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/concurrency/atom.lux
blob: 3905ee7cabe97f1d302e1e5bd5f972dd7baa0909 (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
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;module:
  lux
  (lux (codata [io #- run])
       host)
  )

(jvm-import (java.util.concurrent.atomic.AtomicReference V)
  (new [V])
  (compareAndSet [V V] boolean)
  (get [] V))

(type: #export (Atom a)
  (AtomicReference a))

(def: #export (atom value)
  (All [a] (-> a (Atom a)))
  (AtomicReference.new [value]))

(def: #export (get atom)
  (All [a] (-> (Atom a) (IO a)))
  (io (AtomicReference.get [] atom)))

(def: #export (compare-and-swap old new atom)
  (All [a] (-> a a (Atom a) (IO Bool)))
  (io (AtomicReference.compareAndSet [old new] atom)))

(def: #export (update f atom)
  (All [a] (-> (-> a a) (Atom a) (IO Unit)))
  (io (let [old (AtomicReference.get [] atom)]
        (if (AtomicReference.compareAndSet [old (f old)] atom)
          []
          (io;run (update f atom))))))

(def: #export (set value atom)
  (All [a] (-> a (Atom a) (IO Unit)))
  (update (lambda [_] value) atom))