aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/control/concurrency/thread.lux
blob: a34e050d50e5b05a767ff4170f7d609da72e1bb7 (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
(.module:
  [lux #*
   ["@" target]
   ["." host]
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." exception (#+ exception:)]
    ["." io (#+ IO io)]]
   [data
    [collection
     ["." list]]]
   [math
    [number
     ["n" nat]
     ["f" frac]]]]
  [//
   ["." atom (#+ Atom)]])

(with_expansions [<jvm> (as_is (host.import: java/lang/Object)

                               (host.import: java/lang/Runtime
                                 ["#::."
                                  (#static getRuntime [] java/lang/Runtime)
                                  (availableProcessors [] int)])

                               (host.import: java/lang/Runnable)

                               (host.import: java/util/concurrent/TimeUnit
                                 ["#::."
                                  (#enum MILLISECONDS)])
                               
                               (host.import: java/util/concurrent/Executor
                                 ["#::."
                                  (execute [java/lang/Runnable] #io void)])

                               (host.import: (java/util/concurrent/ScheduledFuture a))

                               (host.import: java/util/concurrent/ScheduledThreadPoolExecutor
                                 ["#::."
                                  (new [int])
                                  (schedule [java/lang/Runnable long java/util/concurrent/TimeUnit] #io (java/util/concurrent/ScheduledFuture java/lang/Object))]))]
  (for {@.old (as_is <jvm>)
        @.jvm (as_is <jvm>)

        @.js
        (as_is (host.import: (setTimeout [host.Function host.Number] #io Any)))

        @.python
        (host.import: threading/Timer
          (new [host.Float host.Function])
          (start [] #io #? Any))}
       
       ## Default
       (type: Thread
         {#creation Nat
          #delay Nat
          #action (IO Any)})
       ))

(def: #export parallelism
  Nat
  (with_expansions [<jvm> (|> (java/lang/Runtime::getRuntime)
                              (java/lang/Runtime::availableProcessors)
                              .nat)]
    (for {@.old <jvm>
          @.jvm <jvm>}
         ## Default
         1)))

(with_expansions [<jvm> (as_is (def: runner
                                 java/util/concurrent/ScheduledThreadPoolExecutor
                                 (java/util/concurrent/ScheduledThreadPoolExecutor::new (.int ..parallelism))))]
  (for {@.old <jvm>
        @.jvm <jvm>
        @.js (as_is)
        @.python (as_is)}
       
       ## Default
       (def: runner
         (Atom (List Thread))
         (atom.atom (list)))))

(def: #export (schedule milli_seconds action)
  (-> Nat (IO Any) (IO Any))
  (for {@.old
        (let [runnable (host.object [] [java/lang/Runnable]
                         []
                         (java/lang/Runnable [] (run self) void
                                             (io.run action)))]
          (case milli_seconds
            0 (java/util/concurrent/Executor::execute runnable runner)
            _ (java/util/concurrent/ScheduledThreadPoolExecutor::schedule runnable (.int milli_seconds) java/util/concurrent/TimeUnit::MILLISECONDS
                                                                          runner)))

        @.jvm
        (let [runnable (host.object [] [java/lang/Runnable]
                         []
                         (java/lang/Runnable [] (run self) void
                                             (io.run action)))]
          (case milli_seconds
            0 (java/util/concurrent/Executor::execute runnable runner)
            _ (java/util/concurrent/ScheduledThreadPoolExecutor::schedule runnable (.int milli_seconds) java/util/concurrent/TimeUnit::MILLISECONDS
                                                                          runner)))

        @.js
        (..setTimeout [(host.closure [] (io.run action))
                       (n.frac milli_seconds)])

        @.python
        (do io.monad
          [_ (|> (host.lambda [] (io.run action))
                 [(|> milli_seconds n.frac (f./ +1,000.0))]
                 threading/Timer::new
                 (threading/Timer::start []))]
          (wrap []))}
       
       ## Default
       (do io.monad
         [_ (atom.update (|>> (#.Cons {#creation (.nat ("lux io current-time"))
                                       #delay milli_seconds
                                       #action action}))
                         ..runner)]
         (wrap []))))

(for {@.old (as_is)
      @.jvm (as_is)
      @.js (as_is)
      @.python (as_is)}
     
     ## Default
     (as_is (exception: #export cannot_continue_running_threads)
            
            (def: #export (run! _)
              (-> Any (IO Any))
              (do {! io.monad}
                [threads (atom.read ..runner)]
                (case threads
                  ## And... we're done!
                  #.Nil
                  (wrap [])

                  _
                  (do !
                    [#let [now (.nat ("lux io current-time"))
                           [ready pending] (list.partition (function (_ thread)
                                                             (|> (get@ #creation thread)
                                                                 (n.+ (get@ #delay thread))
                                                                 (n.<= now)))
                                                           threads)]
                     swapped? (atom.compare_and_swap threads pending ..runner)]
                    (if swapped?
                      (do !
                        [_ (monad.map ! (get@ #action) ready)]
                        (run! []))
                      (error! (exception.construct ..cannot_continue_running_threads []))))
                  )))
            ))