blob: dd38e3041540381204cb9b56c8aa90b4a2cc5d55 (
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
|
(.module:
[lux #*
["@" target]
["." host]
[abstract
["." monad (#+ do)]]
[control
["ex" exception (#+ exception:)]
["." io (#+ IO io)]]
[data
[number
["n" nat]]
[collection
["." list]]]]
[//
["." atom (#+ Atom)]])
(for {@.old
(as-is (host.import: #long java/lang/Object)
(host.import: #long java/lang/Runtime
(#static getRuntime [] java/lang/Runtime)
(availableProcessors [] int))
(host.import: #long java/lang/Runnable)
(host.import: #long java/util/concurrent/TimeUnit
(#enum MILLISECONDS))
(host.import: #long java/util/concurrent/Executor
(execute [java/lang/Runnable] #io void))
(host.import: #long (java/util/concurrent/ScheduledFuture a))
(host.import: #long java/util/concurrent/ScheduledThreadPoolExecutor
(new [int])
(schedule [java/lang/Runnable long java/util/concurrent/TimeUnit] #io (java/util/concurrent/ScheduledFuture java/lang/Object))))
@.jvm
(as-is (host.import: #long java/lang/Object)
(host.import: #long java/lang/Runtime
(#static getRuntime [] java/lang/Runtime)
(availableProcessors [] int))
(host.import: #long java/lang/Runnable)
(host.import: #long java/util/concurrent/TimeUnit
(#enum MILLISECONDS))
(host.import: #long java/util/concurrent/Executor
(execute [java/lang/Runnable] #io void))
(host.import: #long (java/util/concurrent/ScheduledFuture a))
(host.import: #long java/util/concurrent/ScheduledThreadPoolExecutor
(new [int])
(schedule [java/lang/Runnable long java/util/concurrent/TimeUnit] #io (java/util/concurrent/ScheduledFuture java/lang/Object))))}
## Default
(type: Process
{#creation Nat
#delay Nat
#action (IO Any)})
)
(def: #export parallelism
Nat
(for {@.old
(|> (java/lang/Runtime::getRuntime)
(java/lang/Runtime::availableProcessors)
.nat)
@.jvm
(|> (java/lang/Runtime::getRuntime)
(java/lang/Runtime::availableProcessors)
.nat)}
## Default
1))
(def: runner
(for {@.old
(java/util/concurrent/ScheduledThreadPoolExecutor::new (.int ..parallelism))
@.jvm
(java/util/concurrent/ScheduledThreadPoolExecutor::new (.int ..parallelism))}
## Default
(: (Atom (List Process))
(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)))}
## 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)}
## Default
(as-is (exception: #export cannot-continue-running-processes)
(def: #export (run! _)
(-> Any (IO Any))
(do {@ io.monad}
[processes (atom.read ..runner)]
(case processes
## And... we're done!
#.Nil
(wrap [])
_
(do @
[#let [now (.nat ("lux io current-time"))
[ready pending] (list.partition (function (_ process)
(|> (get@ #creation process)
(n.+ (get@ #delay process))
(n.<= now)))
processes)]
swapped? (atom.compare-and-swap processes pending ..runner)]
(if swapped?
(do @
[_ (monad.map @ (get@ #action) ready)]
(run! []))
(error! (ex.construct ..cannot-continue-running-processes []))))
)))
))
|