aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/control/concurrency/thread.lux
blob: b30444a54e0857b2e2b8e4d326c908c87d343790 (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
(.require
 [library
  [lux (.except)
   ["[0]" ffi]
   ["[0]" debug]
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["[0]" try]
    ["[0]" io (.only IO io)]]
   [data
    ["[0]" text]
    [collection
     ["[0]" list]]]
   [math
    [number
     ["n" nat]
     ["f" frac]]]
   [meta
    ["@" target]
    ["[0]" configuration]]
   [world
    [time
     ["[0]" instant (.only Instant) (.use "[1]#[0]" order)]
     ["[0]" duration]]]]]
 [//
  ["[0]" atom (.only Atom)]
  ["[0]" event]])

(with_expansions [<jvm> (these (ffi.import java/lang/Object
                                 "[1]::[0]")
                               
                               (ffi.import java/lang/Long
                                 "[1]::[0]")

                               (ffi.import java/lang/Runtime
                                 "[1]::[0]"
                                 ("static" getRuntime [] java/lang/Runtime)
                                 (availableProcessors [] int))

                               (ffi.import java/lang/Runnable
                                 "[1]::[0]")

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

                               (ffi.import (java/util/concurrent/ScheduledFuture a)
                                 "[1]::[0]")

                               (ffi.import java/util/concurrent/ScheduledThreadPoolExecutor
                                 "[1]::[0]"
                                 (new [int])
                                 (schedule [java/lang/Runnable long java/util/concurrent/TimeUnit] "io" (java/util/concurrent/ScheduledFuture java/lang/Object))))]
  (for @.old (these <jvm>)
       @.jvm (these <jvm>)

       @.js
       (these (ffi.import (setTimeout [ffi.Function ffi.Number] "io" Any)))

       @.python
       (ffi.import threading/Timer
         "[1]::[0]"
         (new [ffi.Float ffi.Function])
         (start [] "io" "?" Any))
       
       ... Default
       (these)
       ))

(def .public parallelism
  Nat
  (with_expansions [<default> 1
                    <jvm> (<| (configuration.for ["lua_compiler?" ""]
                                                 ... TODO: Remove this when Rembulan is no longer being used.
                                                 <default>)
                              (|> (java/lang/Runtime::getRuntime)
                                  (java/lang/Runtime::availableProcessors)
                                  ffi.of_int
                                  .nat))]
    (for @.old <jvm>
         @.jvm <jvm>
         ... Default
         <default>)))

(with_expansions [<jvm> (these (def runner
                                 java/util/concurrent/ScheduledThreadPoolExecutor
                                 (|> ..parallelism
                                     .int
                                     ffi.as_int
                                     java/util/concurrent/ScheduledThreadPoolExecutor::new)))]
  (for @.old <jvm>
       @.jvm <jvm>
       @.js (these)
       @.python (these)
       
       ... Default
       (these (def schedule!,run!
                (let [[module _] (symbol .._)]
                  (event.loop module))))))

(def (execute! action)
  (-> (IO Any) Any)
  (when (try (io.run! action))
    {try.#Failure error}
    (exec
      (debug.log! (.text_composite# "ERROR DURING THREAD EXECUTION:" text.\n
                                    error))
      [])
    
    {try.#Success _}
    []))

(type .public Delay
  Nat)

(def .public milli_second
  Delay
  1)

(with_template [<name> <scale> <base>]
  [(def .public <name>
     Delay
     (n.* <scale> <base>))]

  [second 1,000 milli_second]
  [minute    60 second]
  [hour      60 minute]
  [day       24 hour]
  [week       7 day]
  )

(def .public (schedule! milli_seconds action)
  (-> Delay (IO Any) (IO Any))
  (with_expansions [<jvm> (let [runnable (ffi.object [] [java/lang/Runnable]
                                           []
                                           (java/lang/Runnable [] (run self []) void
                                                               (..execute! action)))]
                            (when 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)))]
    (for @.old <jvm>
         @.jvm <jvm>

         @.js
         (..setTimeout (ffi.function (_ []) Any (..execute! action))
                       (n.frac milli_seconds))

         @.python
         (do io.monad
           [_ (|> (ffi.function (_ []) Any (..execute! action))
                  (threading/Timer::new (|> milli_seconds n.frac (f./ +1,000.0)))
                  threading/Timer::start)]
           (in []))
         
         ... Default
         (let [[schedule! run!] ..schedule!,run!]
           (schedule! milli_seconds action)))))

(for @.old (these)
     @.jvm (these)
     @.js (these)
     @.python (these)
     
     ... Default
     (def .public run!
       (IO Any)
       (let [[schedule! run!] ..schedule!,run!]
         (do io.monad
           [outcome run!]
           (when outcome
             {try.#Success _}
             (in [])
             
             {try.#Failure error}
             (in (debug.log! error))))))
     )