forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacklets.pxi
More file actions
197 lines (160 loc) · 5.53 KB
/
stacklets.pxi
File metadata and controls
197 lines (160 loc) · 5.53 KB
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
(ns pixie.stacklets
(:require [pixie.uv :as uv]
[pixie.ffi :as ffi]))
;; If we don't do this, compiling this file doesn't work since the def will clear out
;; the existing value.
(when (undefined? (var stacklet-loop-h))
(def stacklet-loop-h (atom nil))
(def running-threads (atom 0))
(def main-loop-running? (atom false))
(def main-loop-lock (-create-lock))
(-acquire-lock main-loop-lock true))
;; Yield
(defrecord ThrowException [ex])
(defn run-and-process
([k]
(run-and-process k nil))
([k val]
(let [[h f] (k val)]
(f h))))
(defn exception-on-uv-error [result]
(when (neg? result)
(->ThrowException (str "UV Error: " (uv/uv_err_name result)))))
(defn call-cc [f]
(let [frames (-get-current-var-frames nil)
[h val] (@stacklet-loop-h f)]
(reset! stacklet-loop-h h)
(-set-current-var-frames nil frames)
(if (instance? ThrowException val)
(throw [::Exception (:ex val)])
val)))
(defn -run-later [f]
(let [a (uv/uv_async_t)
cb (atom nil)]
(reset! cb (ffi/ffi-prep-callback uv/uv_async_cb
(fn [handle]
(try
(uv/uv_close a close_cb)
(-dispose! @cb)
(f)
(catch ex (println ex))))))
(uv/uv_async_init (uv/uv_default_loop) a @cb)
(uv/uv_async_send a)
(when (not @main-loop-running?)
(reset! main-loop-running? true)
(-release-lock main-loop-lock))
nil))
(defn yield-control []
(call-cc (fn [k]
(-run-later (partial run-and-process k)))))
(def close_cb (ffi/ffi-prep-callback uv/uv_close_cb
pixie.ffi/dispose!))
;;; Sleep
(defn sleep
([ms]
(sleep ms false))
([ms background?]
(let [f (fn [k]
(let [cb (atom nil)
timer (uv/uv_timer_t)]
(reset! cb (ffi/ffi-prep-callback uv/uv_timer_cb
(fn [handle]
(try
(run-and-process k)
(uv/uv_timer_stop timer)
(-dispose! @cb)
(catch ex
(println ex))))))
(uv/uv_timer_init (uv/uv_default_loop) timer)
(when background?
(uv/uv_unref timer))
(uv/uv_timer_start timer @cb ms 0)))]
(call-cc f))))
;; Spawn
(defn -spawn [start-fn]
(call-cc (fn [k]
(-run-later (fn []
(run-and-process (new-stacklet start-fn))))
(-run-later (partial run-and-process k)))))
(defmacro spawn [& body]
`(let [frames (-get-current-var-frames nil)]
(-spawn (fn [h# _]
(-set-current-var-frames nil frames)
(try
(swap! running-threads inc)
(reset! stacklet-loop-h h#)
(let [result# (do ~@body)]
(swap! running-threads dec)
(call-cc (fn [_] nil)))
(catch e
(println e)))))))
(defmacro spawn-background [& body]
`(let [frames (-get-current-var-frames nil)]
(-spawn (fn [h# _]
(-set-current-var-frames nil frames)
(try
(reset! stacklet-loop-h h#)
(let [result# (do ~@body)]
(call-cc (fn [_] nil)))
(catch e
(println e)))))))
(defn spawn-from-non-stacklet [f]
(let [s (new-stacklet (fn [h _]
(try
(reset! stacklet-loop-h h)
(swap! running-threads inc)
(f)
(swap! running-threads dec)
(call-cc (fn [_] nil))
(catch e
(println e)))))]
(-run-later
(fn []
(run-and-process s)))))
(defn finalizer-loop []
(spawn-background
(loop []
(-run-finalizers)
(sleep 1000 true)
(recur))))
(defn -with-stacklets [fn]
(swap! running-threads inc)
(reset! main-loop-running? true)
(let [[h f] ((new-stacklet fn) nil)]
(f h)
(loop []
(uv/uv_run (uv/uv_default_loop) uv/UV_RUN_DEFAULT)
(when (> @running-threads 0)
(reset! main-loop-running? false)
(-acquire-lock main-loop-lock true)
(recur)))))
(defmacro with-stacklets [& body]
`(-with-stacklets
(fn [h# _]
(try
(reset! stacklet-loop-h h#)
(finalizer-loop)
(let [result# (do ~@body)]
(swap! running-threads dec)
(call-cc (fn [_] nil)))
(catch e
(println e))))))
(defn run-with-stacklets [f]
(with-stacklets
(f)))
(defprotocol IThreadPool
(-execute [this work-fn]))
;; Super basic Thread Pool, yes, this should be improved
(deftype ThreadPool []
IThreadPool
(-execute [this work-fn]
(-thread (fn [] (work-fn)))))
(def basic-thread-pool (->ThreadPool))
(defn -run-in-other-thread [work-fn]
(-execute basic-thread-pool work-fn))
(defn apply-blocking [f & args]
(call-cc (fn [k]
(-run-in-other-thread
(fn []
(let [result (apply f args)]
(-run-later (fn [] (run-and-process k result)))))))))