Repository navigation
Expand file tree
/
Copy pathchannels.clj
More file actions
299 lines (285 loc) · 11 KB
/
Copy pathchannels.clj
File metadata and controls
299 lines (285 loc) · 11 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
;; Copyright (c) Rich Hickey and contributors. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns ^{:skip-wiki true}
clojure.core.async.impl.channels
(:require [clojure.core.async.impl.protocols :as impl]
[clojure.core.async.impl.dispatch :as dispatch]
[clojure.core.async.impl.mutex :as mutex])
(:import [java.util LinkedList Queue Iterator]
[java.util.concurrent.locks Lock]))
(set! *warn-on-reflection* true)
(defmacro assert-unlock [lock test msg]
`(when-not ~test
(.unlock ~lock)
(throw (new AssertionError (str "Assert failed: " ~msg "\n" (pr-str '~test))))))
(defn box [val]
(reify clojure.lang.IDeref
(deref [_] val)))
(defprotocol MMC
(cleanup [_])
(abort [_]))
(deftype ManyToManyChannel [^LinkedList takes ^LinkedList puts ^Queue buf closed ^Lock mutex add!]
MMC
(cleanup
[_]
(when-not (.isEmpty takes)
(let [iter (.iterator takes)]
(loop [taker (.next iter)]
(when-not (impl/active? taker)
(.remove iter))
(when (.hasNext iter)
(recur (.next iter))))))
(when-not (.isEmpty puts)
(let [iter (.iterator puts)]
(loop [[putter] (.next iter)]
(when-not (impl/active? putter)
(.remove iter))
(when (.hasNext iter)
(recur (.next iter)))))))
(abort
[this]
(let [iter (.iterator puts)]
(when (.hasNext iter)
(loop [^Lock putter (.next iter)]
(.lock putter)
(let [put-cb (and (impl/active? putter) (impl/commit putter))]
(.unlock putter)
(when put-cb
(dispatch/run (fn [] (put-cb true))))
(when (.hasNext iter)
(recur (.next iter)))))))
(.clear puts)
(impl/close! this))
impl/WritePort
(put!
[this val handler]
(when (nil? val)
(throw (IllegalArgumentException. "Can't put nil on channel")))
(.lock mutex)
(cleanup this)
(if @closed
(do (.unlock mutex)
(box false))
(let [^Lock handler handler]
(if (and buf (not (impl/full? buf)) (not (.isEmpty takes)))
(do
(.lock handler)
(let [put-cb (and (impl/active? handler) (impl/commit handler))]
(.unlock handler)
(if put-cb
(let [done? (reduced? (add! buf val))]
(if (pos? (count buf))
(let [iter (.iterator takes)
take-cb (when (.hasNext iter)
(loop [^Lock taker (.next iter)]
(.lock taker)
(let [ret (and (impl/active? taker) (impl/commit taker))]
(.unlock taker)
(if ret
(do
(.remove iter)
ret)
(when (.hasNext iter)
(recur (.next iter)))))))]
(if take-cb
(let [val (impl/remove! buf)]
(when done?
(abort this))
(.unlock mutex)
(dispatch/run (fn [] (take-cb val))))
(do
(when done?
(abort this))
(.unlock mutex))))
(do
(when done?
(abort this))
(.unlock mutex)))
(box true))
(do (.unlock mutex)
nil))))
(let [iter (.iterator takes)
[put-cb take-cb] (when (.hasNext iter)
(loop [^Lock taker (.next iter)]
(if (< (impl/lock-id handler) (impl/lock-id taker))
(do (.lock handler) (.lock taker))
(do (.lock taker) (.lock handler)))
(let [ret (when (and (impl/active? handler) (impl/active? taker))
[(impl/commit handler) (impl/commit taker)])]
(.unlock handler)
(.unlock taker)
(if ret
(do
(.remove iter)
ret)
(when (.hasNext iter)
(recur (.next iter)))))))]
(if (and put-cb take-cb)
(do
(.unlock mutex)
(dispatch/run (fn [] (take-cb val)))
(box true))
(if (and buf (not (impl/full? buf)))
(do
(.lock handler)
(let [put-cb (and (impl/active? handler) (impl/commit handler))]
(.unlock handler)
(if put-cb
(let [done? (reduced? (add! buf val))]
(when done?
(abort this))
(.unlock mutex)
(box true))
(do (.unlock mutex)
nil))))
(do
(when (impl/active? handler)
(assert-unlock mutex
(< (.size puts) impl/MAX-QUEUE-SIZE)
(str "No more than " impl/MAX-QUEUE-SIZE
" pending puts are allowed on a single channel."
" Consider using a windowed buffer."))
(.add puts [handler val]))
(.unlock mutex)
nil))))))))
impl/ReadPort
(take!
[this handler]
(.lock mutex)
(cleanup this)
(let [^Lock handler handler
commit-handler (fn []
(.lock handler)
(let [take-cb (and (impl/active? handler) (impl/commit handler))]
(.unlock handler)
take-cb))]
(if (and buf (pos? (count buf)))
(do
(if-let [take-cb (commit-handler)]
(let [val (impl/remove! buf)
iter (.iterator puts)
[done? cbs]
(when (.hasNext iter)
(loop [cbs []
[^Lock putter val] (.next iter)]
(.lock putter)
(let [cb (and (impl/active? putter) (impl/commit putter))]
(.unlock putter)
(.remove iter)
(let [cbs (if cb (conj cbs cb) cbs)
done? (when cb (reduced? (add! buf val)))]
(if (and (not done?) (not (impl/full? buf)) (.hasNext iter))
(recur cbs (.next iter))
[done? cbs])))))]
(when done?
(abort this))
(.unlock mutex)
(doseq [cb cbs]
(dispatch/run #(cb true)))
(box val))
(do (.unlock mutex)
nil)))
(let [iter (.iterator puts)
[take-cb put-cb val]
(when (.hasNext iter)
(loop [[^Lock putter val] (.next iter)]
(if (< (impl/lock-id handler) (impl/lock-id putter))
(do (.lock handler) (.lock putter))
(do (.lock putter) (.lock handler)))
(let [ret (when (and (impl/active? handler) (impl/active? putter))
[(impl/commit handler) (impl/commit putter) val])]
(.unlock handler)
(.unlock putter)
(if ret
(do
(.remove iter)
ret)
(when-not (impl/active? putter)
(.remove iter)
(when (.hasNext iter)
(recur (.next iter))))))))]
(if (and put-cb take-cb)
(do
(.unlock mutex)
(dispatch/run #(put-cb true))
(box val))
(if @closed
(do
(when buf (add! buf))
(let [has-val (and buf (pos? (count buf)))]
(if-let [take-cb (commit-handler)]
(let [val (when has-val (impl/remove! buf))]
(.unlock mutex)
(box val))
(do
(.unlock mutex)
nil))))
(do
(assert-unlock mutex
(< (.size takes) impl/MAX-QUEUE-SIZE)
(str "No more than " impl/MAX-QUEUE-SIZE
" pending takes are allowed on a single channel."))
(.add takes handler)
(.unlock mutex)
nil)))))))
impl/Channel
(closed? [_] @closed)
(close!
[this]
(.lock mutex)
(cleanup this)
(if @closed
(do
(.unlock mutex)
nil)
(do
(reset! closed true)
(when (and buf (.isEmpty puts))
(add! buf))
(let [iter (.iterator takes)]
(when (.hasNext iter)
(loop [^Lock taker (.next iter)]
(.lock taker)
(let [take-cb (and (impl/active? taker) (impl/commit taker))]
(.unlock taker)
(when take-cb
(let [val (when (and buf (pos? (count buf))) (impl/remove! buf))]
(dispatch/run (fn [] (take-cb val)))))
(.remove iter)
(when (.hasNext iter)
(recur (.next iter)))))))
(.unlock mutex)
nil))))
(defn- ex-handler [ex]
(-> (Thread/currentThread)
.getUncaughtExceptionHandler
(.uncaughtException (Thread/currentThread) ex))
nil)
(defn- handle [buf exh t]
(let [else ((or exh ex-handler) t)]
(if (nil? else)
buf
(impl/add! buf else))))
(defn chan
([buf] (chan buf nil))
([buf xform] (chan buf xform nil))
([buf xform exh]
(ManyToManyChannel.
(LinkedList.) (LinkedList.) buf (atom false) (mutex/mutex)
(let [add! (if xform (xform impl/add!) impl/add!)]
(fn
([buf]
(try
(add! buf)
(catch Throwable t
(handle buf exh t))))
([buf val]
(try
(add! buf val)
(catch Throwable t
(handle buf exh t)))))))))