-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathexec.cljc
1011 lines (889 loc) · 33 KB
/
exec.cljc
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) Andrey Antukh <[email protected]>
(ns promesa.exec
"Executors & Schedulers facilities."
(:refer-clojure :exclude [run! pmap await])
(:require
[promesa.protocols :as pt]
[promesa.util :as pu]
#?(:cljs [goog.object :as gobj])
#?(:cljs [promesa.impl.promise :as pimpl]))
#?(:clj
(:import
clojure.lang.Var
java.lang.AutoCloseable
java.lang.Thread$UncaughtExceptionHandler
java.time.Duration
java.time.Instant
java.time.temporal.TemporalAmount
java.util.concurrent.BlockingQueue
java.util.concurrent.Callable
java.util.concurrent.CancellationException
java.util.concurrent.CompletableFuture
java.util.concurrent.CompletionException
java.util.concurrent.CompletionStage
java.util.concurrent.CountDownLatch
java.util.concurrent.ExecutionException
java.util.concurrent.Executor
java.util.concurrent.ExecutorService
java.util.concurrent.Executors
java.util.concurrent.ForkJoinPool
java.util.concurrent.ForkJoinPool$ForkJoinWorkerThreadFactory
java.util.concurrent.ForkJoinPool$ManagedBlocker
java.util.concurrent.ForkJoinWorkerThread
java.util.concurrent.Future
java.util.concurrent.ScheduledExecutorService
java.util.concurrent.ScheduledThreadPoolExecutor
java.util.concurrent.SynchronousQueue
java.util.concurrent.ThreadFactory
java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.TimeUnit
java.util.concurrent.TimeoutException
java.util.concurrent.atomic.AtomicLong
java.util.function.Supplier)))
#?(:clj (set! *warn-on-reflection* true))
(declare scheduled-executor)
(declare current-thread-executor)
#?(:clj (declare thread-factory))
#?(:clj (declare thread-per-task-executor))
#?(:clj (declare vthread-per-task-executor))
#?(:clj (declare cached-executor))
#?(:cljs (declare microtask-executor))
(def ^:dynamic *default-scheduler* nil)
(def ^:dynamic *default-executor* nil)
(def virtual-threads-available?
"Var that indicates the availability of virtual threads."
#?(:clj (and (pu/has-method? Thread "ofVirtual")
;; the following should succeed with the `--enable-preview` java argument:
;; eval happens on top level = compile time, which is ok for GraalVM
(pu/can-eval? '(Thread/ofVirtual)))
:cljs false))
(def structured-task-scope-available?
#?(:clj (and (pu/class-exists? "java.util.concurrent.StructuredTaskScope")
(pu/can-eval? '(java.util.concurrent.StructuredTaskScope.)))
:cljs false))
(def ^{:no-doc true} noop (constantly nil))
#?(:clj
(defn get-available-processors
[]
(.availableProcessors (Runtime/getRuntime))))
(defonce
^{:doc "Default scheduled executor instance."}
default-scheduler
(delay
#?(:clj (scheduled-executor :parallelism (min (int (* (get-available-processors) 0.2)) 2)
:thread-factory {:prefix "promesa/default-scheduler/"})
:cljs (scheduled-executor))))
(defonce
^{:doc "Default executor instance, ForkJoinPool/commonPool in JVM, MicrotaskExecutor on JS."}
default-executor
(delay
#?(:clj (ForkJoinPool/commonPool)
:cljs (microtask-executor))))
;; Executor that executes the task in the calling thread
(def ^{:doc "Default Executor instance that runs the task in the same thread."}
default-current-thread-executor
(delay (current-thread-executor)))
(defonce
^{:doc "A global, cached thread executor service."
:no-doc true}
default-cached-executor
(delay
#?(:clj (cached-executor)
:cljs default-executor)))
(defonce
^{:doc "A global, thread per task executor service."
:no-doc true}
default-thread-executor
#?(:clj (pu/with-compile-cond virtual-threads-available?
(delay (thread-per-task-executor))
default-cached-executor)
:cljs default-executor))
(defonce
^{:doc "A global, virtual thread per task executor service."
:no-doc true}
default-vthread-executor
#?(:clj (pu/with-compile-cond virtual-threads-available?
(delay (vthread-per-task-executor))
default-cached-executor)
:cljs default-executor))
(defn executor?
"Returns true if `o` is an instane of Executor or satisfies IExecutor protocol."
[o]
#?(:clj (or (instance? Executor o)
(satisfies? pt/IExecutor o))
:cljs (satisfies? pt/IExecutor o)))
#?(:clj
(defn shutdown!
"Shutdowns the executor service."
[^ExecutorService executor]
(.shutdown executor)))
#?(:clj
(defn shutdown-now!
"Shutdowns and interrupts the executor service."
[^ExecutorService executor]
(.shutdownNow executor)))
#?(:clj
(defn shutdown?
"Check if execitor is in shutdown state."
[^ExecutorService executor]
(.isShutdown executor)))
(defn resolve-executor
{:no-doc true}
([] (resolve-executor nil))
([executor]
(case executor
(nil :default) @default-executor
:cached @default-cached-executor
(:platform :thread) @default-thread-executor
(:virtual :vthread) @default-vthread-executor
(:same-thread
:current-thread) @default-current-thread-executor
(cond
(executor? executor) executor
(delay? executor) (resolve-executor @executor)
:else
(throw #?(:clj (IllegalArgumentException. "invalid executor")
:cljs (js/TypeError. "invalid executor")))))))
(defn resolve-scheduler
{:no-doc true}
([] (resolve-scheduler nil))
([scheduler]
(if (or (nil? scheduler) (= :default scheduler))
@default-scheduler
(pu/maybe-deref scheduler))))
#?(:clj
(defn- binding-conveyor-inner
[f]
(let [frame (clojure.lang.Var/cloneThreadBindingFrame)]
(fn
([]
(clojure.lang.Var/resetThreadBindingFrame frame)
(cond
(instance? clojure.lang.IFn f)
(.invoke ^clojure.lang.IFn f)
(instance? java.lang.Runnable f)
(.run ^java.lang.Runnable f)
(instance? java.util.concurrent.Callable f)
(.call ^java.util.concurrent.Callable f)
:else
(throw (ex-info "Unsupported function type" {:f f :type (type f)}))))
([x]
(clojure.lang.Var/resetThreadBindingFrame frame)
(cond
(instance? clojure.lang.IFn f)
(.invoke ^clojure.lang.IFn f x)
(instance? java.util.function.Function f)
(.apply ^java.util.function.Function f x)
:else
(throw (ex-info "Unsupported function type" {:f f :type (type f)}))))
([x y]
(clojure.lang.Var/resetThreadBindingFrame frame)
(f x y))
([x y z]
(clojure.lang.Var/resetThreadBindingFrame frame)
(f x y z))
([x y z & args]
(clojure.lang.Var/resetThreadBindingFrame frame)
(apply f x y z args))))))
(defn wrap-bindings
{:no-doc true}
;; Passes on local bindings from one thread to another. Compatible with `clojure.lang.IFn`,
;; `java.lang.Runnable`, `java.util.concurrent.Callable`, and `java.util.function.Function`.
;; Adapted from `clojure.core/binding-conveyor-fn`."
[f]
#?(:cljs f
:clj (reify
clojure.lang.IFn
(invoke [_ f] (binding-conveyor-inner f))
java.util.function.Function
(apply [_ f] (binding-conveyor-inner f)))))
#?(:clj
(defn thread-factory?
"Checks if `o` is an instance of ThreadFactory"
[o]
(instance? ThreadFactory o)))
#?(:clj
(def ^{:no-doc true :dynamic true}
*default-counter*
(AtomicLong. 0)))
#?(:clj
(defn get-next
"Get next value from atomic long counter"
{:no-doc true}
([] (.getAndIncrement ^AtomicLong *default-counter*))
([counter] (.getAndIncrement ^AtomicLong counter))))
#?(:clj
(defn thread-factory
"Create a new thread factory instance"
[& {:keys [name daemon priority prefix virtual]}]
(pu/with-compile-cond virtual-threads-available?
(if virtual
(let [thb (Thread/ofVirtual)
thb (cond
(string? name) (.name thb ^String name)
(string? prefix) (.name thb ^String prefix 0)
:else thb)]
(.factory thb))
(let [thb (Thread/ofPlatform)
thb (if (some? priority)
(.priority thb (int priority))
thb)
thb (if (some? daemon)
(.daemon thb ^Boolean daemon)
(.daemon thb true))
thb (cond
(string? name) (.name thb ^String name)
(string? prefix) (.name thb ^String prefix 0)
:else thb)]
(.factory thb)))
(let [counter (AtomicLong. 0)]
(reify ThreadFactory
(newThread [this runnable]
(let [thr (Thread. ^Runnable runnable)]
(when (some? priority)
(.setPriority thr (int priority)))
(when (some? daemon)
(.setDaemon thr ^Boolean daemon))
(when (string? name)
(.setName thr ^String name))
(when (string? prefix)
(.setName thr (str prefix (get-next counter))))
thr)))))))
#?(:clj
(defonce default-thread-factory
(delay (thread-factory :prefix "promesa/platform/"))))
#?(:clj
(defonce default-vthread-factory
(delay (thread-factory :prefix "promesa/virtual/" :virtual true))))
#?(:clj
(defn resolve-thread-factory
{:no-doc true}
^ThreadFactory
[tf]
(cond
(nil? tf) nil
(thread-factory? tf) tf
(= :default tf) @default-thread-factory
(= :platform tf) @default-thread-factory
(= :virtual tf) @default-vthread-factory
(map? tf) (thread-factory tf)
(fn? tf) (reify ThreadFactory
(^Thread newThread [_ ^Runnable runnable]
(tf runnable)))
:else (throw (ex-info "Invalid thread factory" {})))))
#?(:clj
(defn- options->thread-factory
{:no-doc true}
(^ThreadFactory [options]
(resolve-thread-factory
(or (:thread-factory options)
(:factory options))))
(^ThreadFactory [options default]
(resolve-thread-factory
(or (:thread-factory options)
(:factory options)
default)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PUBLIC API
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn exec!
"Run the task in the provided executor, returns `nil`. Analogous to
the `(.execute executor f)`. Fire and forget.
Exception unsafe, can raise exceptions if the executor
rejects the task."
([f]
(let [f (if (fn? f) (wrap-bindings f) f)]
(pt/-exec! (resolve-executor *default-executor*) f)))
([executor f]
(let [f (if (fn? f) (wrap-bindings f) f)]
(pt/-exec! (resolve-executor executor) f))))
(defn run!
"Run the task in the provided executor.
Exception unsafe, can raise exceptions if the executor
rejects the task."
([f]
(let [f (if (fn? f) (wrap-bindings f) f)]
(pt/-run! (resolve-executor *default-executor*) f)))
([executor f]
(let [f (if (fn? f) (wrap-bindings f) f)]
(pt/-run! (resolve-executor executor) f))))
(defn submit!
"Submit a task to be executed in a provided executor
and return a promise that will be completed with
the return value of a task.
Exception unsafe, can raise exceptions if the executor
rejects the task."
([f]
(let [f (if (fn? f) (wrap-bindings f) f)]
(pt/-submit! (resolve-executor *default-executor*) f)))
([executor f]
(let [f (if (fn? f) (wrap-bindings f) f)]
(pt/-submit! (resolve-executor executor) f))))
(defn schedule!
"Schedule a callable to be executed after the `ms` delay
is reached.
In JVM it uses a scheduled executor service and in JS
it uses the `setTimeout` function.
Exception unsafe, can raise exceptions if the executor
rejects the task."
([ms f]
(pt/-schedule! (resolve-scheduler) ms f))
([scheduler ms f]
(pt/-schedule! (resolve-scheduler scheduler) ms f)))
#?(:clj
(defn invoke!
"Invoke a function to be executed in the provided executor
or the default one, and waits for the result. Useful for using
in virtual threads."
([f] (pt/-await! (submit! f)))
([executor f] (pt/-await! (submit! executor f)))))
(defn- rejected
[v]
#?(:cljs (pimpl/rejected v)
:clj (let [p (CompletableFuture.)]
(.completeExceptionally ^CompletableFuture p v)
p)))
(defn run
"Exception safe version of `run!`. It always returns an promise instance."
([f]
(try
(run! f)
(catch #?(:clj Throwable :cljs :default) cause
(rejected cause))))
([executor f]
(try
(run! executor f)
(catch #?(:clj Throwable :cljs :default) cause
(rejected cause)))))
(defn submit
"Exception safe version of `submit!`. It always returns an promise instance."
([f]
(try
(submit! f)
(catch #?(:clj Throwable :cljs :default) cause
(rejected cause))))
([executor f]
(try
(submit! executor f)
(catch #?(:clj Throwable :cljs :default) cause
(rejected cause)))))
(defn schedule
"Exception safe version of `schedule!`. It always returns an promise instance."
([ms f]
(try
(schedule! ms f)
(catch #?(:clj Throwable :cljs :default) cause
(rejected cause))))
([scheduler ms f]
(try
(schedule! scheduler ms f)
(catch #?(:clj Throwable :cljs :default) cause
(rejected cause)))))
;; --- Pool & Thread Factories
#?(:clj
(defn forkjoin-thread-factory
^ForkJoinPool$ForkJoinWorkerThreadFactory
[& {:keys [name daemon] :or {name "promesa/forkjoin/%s" daemon true}}]
(let [counter (AtomicLong. 0)]
(reify ForkJoinPool$ForkJoinWorkerThreadFactory
(newThread [_ pool]
(let [thread (.newThread ForkJoinPool/defaultForkJoinWorkerThreadFactory pool)
tname (format name (get-next counter))]
(.setName ^ForkJoinWorkerThread thread ^String tname)
(.setDaemon ^ForkJoinWorkerThread thread ^Boolean daemon)
thread))))))
#?(:clj
(defn cached-executor
"A cached thread executor pool constructor."
[& {:keys [max-size keepalive]
:or {keepalive 60000 max-size Integer/MAX_VALUE}
:as options}]
(let [factory (or (options->thread-factory options)
(deref default-thread-factory))
queue (SynchronousQueue.)]
(ThreadPoolExecutor. 0
(long max-size)
(long keepalive)
TimeUnit/MILLISECONDS
^BlockingQueue queue
^ThreadFactory factory))))
#?(:clj
(defn fixed-executor
"A fixed thread executor pool constructor."
[& {:keys [parallelism]
:as options}]
(if-let [factory (options->thread-factory options)]
(Executors/newFixedThreadPool (int parallelism) ^ThreadFactory factory)
(Executors/newFixedThreadPool (int parallelism)))))
#?(:clj
(defn single-executor
"A single thread executor pool constructor."
[& {:as options}]
(if-let [factory (options->thread-factory options)]
(Executors/newSingleThreadExecutor factory)
(Executors/newSingleThreadExecutor))))
#?(:cljs
(deftype Scheduler []
pt/IScheduler
(-schedule! [_ ms f]
(let [df (pimpl/deferred)
tid (js/setTimeout
(fn []
(try
(pt/-resolve! df (f))
(catch :default cause
(pt/-reject! df cause))))
ms)]
(pt/-fnly df
(fn [_ c]
(when (pimpl/isCancellationError c)
(js/clearTimeout tid))))
df))))
(defn scheduled-executor
"A scheduled thread pool constructor. A ScheduledExecutor (IScheduler
in CLJS) instance allows execute asynchronous tasks some time later."
[& {:keys [parallelism] :or {parallelism 1} :as options}]
#?(:clj
(let [parallelism (or parallelism (get-available-processors))
factory (options->thread-factory options)
executor (if factory
(ScheduledThreadPoolExecutor. (int parallelism) ^ThreadFactory factory)
(ScheduledThreadPoolExecutor. (int parallelism)))]
(.setRemoveOnCancelPolicy executor true)
executor)
:cljs
(->Scheduler)))
(defn current-thread-executor
"Creates an executor instance that run tasks in the same thread."
[]
#?(:clj
(reify
Executor
(^void execute [_ ^Runnable f]
(.run f)))
:cljs
(reify
pt/IExecutor
(-exec! [this f]
(try
(f)
nil
(catch :default _
nil)))
(-run! [this f]
(try
(pt/-promise (comp noop f))
(catch :default cause
(pt/-promise cause))))
(-submit! [this f]
(try
(pt/-promise (f))
(catch :default cause
(pt/-promise cause)))))))
#?(:cljs
(defn microtask-executor
"An IExecutor that schedules tasks to be executed in the MicrotasksQueue."
[]
(reify
pt/IExecutor
(-exec! [this f]
(pimpl/nextTick f))
(-run! [this f]
(-> (pt/-promise nil)
(pt/-fmap (fn [_]
(try (f) (catch :default _ nil))))
(pt/-fmap noop)))
(-submit! [this f]
(-> (pt/-promise nil)
(pt/-fmap (fn [_] (f))))))))
#?(:clj
(pu/with-compile-cond virtual-threads-available?
(defn thread-per-task-executor
[& {:as options}]
(let [factory (or (options->thread-factory options)
(deref default-thread-factory))]
(Executors/newThreadPerTaskExecutor ^ThreadFactory factory)))))
#?(:clj
(pu/with-compile-cond virtual-threads-available?
(defn vthread-per-task-executor
[]
(Executors/newVirtualThreadPerTaskExecutor))))
#?(:clj
(defn forkjoin-executor
[& {:keys [factory async parallelism keepalive core-size max-size]
:or {max-size 0x7fff async true keepalive 60000}}]
(let [parallelism (or parallelism (get-available-processors))
core-size (or core-size parallelism)
factory (cond
(instance? ForkJoinPool$ForkJoinWorkerThreadFactory factory) factory
(nil? factory) (forkjoin-thread-factory)
:else (throw (UnsupportedOperationException. "Unexpected thread factory")))]
(ForkJoinPool. (int parallelism)
^ForkJoinPool$ForkJoinWorkerThreadFactory factory
nil
async
(int core-size)
(int max-size)
1,
nil
(long keepalive)
TimeUnit/MILLISECONDS))))
#?(:clj
(defn work-stealing-executor
"An alias for the `forkjoin-executor`."
[& params]
(apply forkjoin-executor params)))
#?(:clj
(defn configure-default-executor!
[& params]
(alter-var-root #'*default-executor*
(fn [executor]
(when (and (delay? executor) (realized? executor))
(.close ^AutoCloseable @executor))
(when (instance? AutoCloseable executor)
(.close ^AutoCloseable executor))
(apply forkjoin-executor params)))))
#?(:clj
(extend-type Executor
pt/IExecutor
(-exec! [this f]
(.execute ^Executor this ^Runnable f))
(-run! [this f]
(CompletableFuture/runAsync ^Runnable f ^Executor this))
(-submit! [this f]
(CompletableFuture/supplyAsync ^Supplier (pu/->Supplier f) ^Executor this))))
;; --- Scheduler
#?(:clj
(extend-type ScheduledExecutorService
pt/IScheduler
(-schedule! [this ms f]
(let [ms (if (instance? Duration ms)
(.toMillis ^Duration ms)
ms)
df (CompletableFuture.)
fut (.schedule this
^Runnable (fn []
(try
(pt/-resolve! df (f))
(catch Throwable cause
(pt/-reject! df cause))))
(long ms)
TimeUnit/MILLISECONDS)]
(pt/-fnly df
(fn [_ c]
(when (instance? CancellationException c)
(pt/-cancel! fut))))
df))))
(defmacro with-dispatch
"Helper macro for dispatch execution of the body to an executor
service. The returned promise is not cancellable (the body will be
executed independently of the cancellation)."
[executor & body]
`(-> (submit ~executor (^:once fn* [] ~@body))
(pt/-mcat pt/-promise)))
(defmacro with-dispatch!
"Blocking version of `with-dispatch`. Useful when you want to
dispatch a blocking operation to a separated thread and join current
thread waiting for result; effective when current thread is virtual
thread."
[executor & body]
(when (:ns &env)
(throw (ex-info "cljs not supported on with-dispatch! macro" {})))
`(-> (submit ~executor (^:once fn* [] ~@body))
(pt/-mcat pt/-promise)
(pt/-await!)))
(defmacro with-executor
"Binds the *default-executor* var with the provided executor,
executes the macro body. It also can optionally shutdown or shutdown
and interrupt on termination if you provide `^:shutdown` and
`^:interrupt` metadata.
**EXPERIMENTAL API:** This function should be considered
EXPERIMENTAL and may be changed or removed in future versions until
this notification is removed."
[executor & body]
(let [interrupt? (-> executor meta :interrupt)
shutdown? (-> executor meta :shutdown)
executor-sym (gensym "executor")]
`(let [~executor-sym ~executor
~executor-sym (if (fn? ~executor-sym) (~executor-sym) ~executor-sym)]
(binding [*default-executor* ~executor-sym]
(try
~@body
(finally
~(when (or shutdown? interrupt?)
(list (if interrupt? 'promesa.exec/shutdown-now! 'promesa.exec/shutdown!) executor-sym))))))))
#?(:clj
(defn pmap
"Analogous to the `clojure.core/pmap` with the excetion that it allows
use a custom executor (binded to *default-executor* var) The default
clojure chunk size (32) is used for evaluation and the real
parallelism is determined by the provided executor.
**EXPERIMENTAL API:** This function should be considered
EXPERIMENTAL and may be changed or removed in future versions until
this notification is removed."
{:experimental true}
([f coll]
(let [executor (resolve-executor *default-executor*)
frame (Var/cloneThreadBindingFrame)]
(->> coll
(map (fn [o] (pt/-submit! executor #(do
(Var/resetThreadBindingFrame frame)
(f o)))))
(clojure.lang.RT/iter)
(clojure.lang.RT/chunkIteratorSeq)
(map (fn [o] (.get ^CompletableFuture o))))))
([f coll & colls]
(let [step-fn (fn step-fn [cs]
(lazy-seq
(let [ss (map seq cs)]
(when (every? identity ss)
(cons (map first ss) (step-fn (map rest ss)))))))]
(pmap #(apply f %) (step-fn (cons coll colls)))))))
#?(:clj
(defn fn->thread
[f & {:keys [start] :or {start true} :as options}]
(let [factory (or (options->thread-factory options)
(thread-factory options))
f (wrap-bindings f)
thread (.newThread ^ThreadFactory factory ^Runnable f)]
(if start
(.start ^Thread thread))
thread)))
#?(:clj
(defmacro thread
"A low-level, not-pooled thread constructor, it accepts an optional
map as first argument and the body. The options map is interepreted
as options if a literal map is provided. The available options are:
`:name`, `:priority`, `:daemon` and `:virtual`. The `:virtual`
option is ignored if you are using a JVM that has no support for
Virtual Threads."
[opts & body]
(let [[opts body] (if (map? opts) [opts body] [{} (cons opts body)])]
`(fn->thread (^:once fn* [] ~@body) ~@(mapcat identity opts)))))
#?(:clj
(defn thread-call
"Advanced version of `p/thread-call` that creates and starts a thread
configured with `opts`. No executor service is used, this will start
a plain unpooled thread; returns a non-cancellable promise instance"
[f & {:as opts}]
(let [p (CompletableFuture.)]
(fn->thread #(try
(pt/-resolve! p (f))
(catch Throwable cause
(pt/-reject! p cause)))
(assoc opts :start true))
p)))
#?(:clj
(defn current-thread
"Return the current thread."
[]
(Thread/currentThread)))
#?(:clj
(defn set-name!
"Rename thread."
([name] (set-name! (current-thread) name))
([thread name] (.setName ^Thread thread ^String name))))
#?(:clj
(defn get-name
"Retrieve thread name"
([] (get-name (current-thread)))
([thread]
(.getName ^Thread thread))))
#?(:clj
(defn interrupted?
"Check if the thread has the interrupted flag set.
There are two special cases:
Using the `:current` keyword as argument will check the interrupted
flag on the current thread.
Using the arity 0 (passing no arguments), then the current thread
will be checked and **WARNING** the interrupted flag reset to
`false`."
([]
(Thread/interrupted))
([thread]
(if (= :current thread)
(.isInterrupted (Thread/currentThread))
(.isInterrupted ^Thread thread)))))
#?(:clj
(defn get-thread-id
"Retrieves the thread ID."
([]
(.getId ^Thread (Thread/currentThread)))
([^Thread thread]
(.getId thread))))
#?(:clj
(defn thread-id
"Retrieves the thread ID."
{:deprecated "11.0"}
([]
(.getId ^Thread (Thread/currentThread)))
([^Thread thread]
(.getId thread))))
#?(:clj
(defn interrupt!
"Interrupt a thread."
([]
(.interrupt (Thread/currentThread)))
([^Thread thread]
(.interrupt thread))))
#?(:clj
(defn thread?
"Check if provided object is a thread instance."
[t]
(instance? Thread t)))
#?(:clj
(defn sleep
"Turn the current thread to sleep accept a number of milliseconds or
Duration instance."
[ms]
(if (instance? Duration ms)
(Thread/sleep (int (.toMillis ^Duration ms)))
(Thread/sleep (int ms)))))
#?(:clj
(defn throw-uncaught!
"Throw an exception to the current uncaught exception handler."
[cause]
(let [thr (current-thread)
hdl (.getUncaughtExceptionHandler ^Thread thr)]
(.uncaughtException ^Thread$UncaughtExceptionHandler hdl
^Thread thr
^Throwable cause))))
#?(:clj
(defn structured-task-scope
([]
(pu/with-compile-cond structured-task-scope-available?
(java.util.concurrent.StructuredTaskScope.)
(throw (IllegalArgumentException. "implementation not available"))))
([& {:keys [name preset] :as options}]
(pu/with-compile-cond structured-task-scope-available?
(let [tf (options->thread-factory options :virtual)]
(case preset
:shutdown-on-success
(java.util.concurrent.StructuredTaskScope$ShutdownOnSuccess.
^String name ^ThreadFactory tf)
:shutdown-on-failure
(java.util.concurrent.StructuredTaskScope$ShutdownOnFailure.
^String name ^ThreadFactory tf)
(java.util.concurrent.StructuredTaskScope.
^String name ^ThreadFactory tf)))
(throw (IllegalArgumentException. "implementation not available"))))))
#?(:clj
(pu/with-compile-cond structured-task-scope-available?
(extend-type java.util.concurrent.StructuredTaskScope$Subtask
pt/IState
(-extract
([it]
(let [state (.state ^java.util.concurrent.StructuredTaskScope$Subtask it)]
(case (str state)
"UNAVAILABLE" nil
"FAILED" (.exception ^java.util.concurrent.StructuredTaskScope$Subtask it)
"SUCCESS" (.get ^java.util.concurrent.StructuredTaskScope$Subtask it))))
([it default]
(or (pt/-extract it) default)))
(-pending? [it]
(let [state (.state ^java.util.concurrent.StructuredTaskScope$Subtask it)]
(not= state java.util.concurrent.StructuredTaskScope$Subtask$State/UNAVAILABLE)))
(-rejected? [it]
(let [state (.state ^java.util.concurrent.StructuredTaskScope$Subtask it)]
(= state java.util.concurrent.StructuredTaskScope$Subtask$State/FAILED)))
(-resolved? [it]
(let [state (.state ^java.util.concurrent.StructuredTaskScope$Subtask it)]
(= state java.util.concurrent.StructuredTaskScope$Subtask$State/SUCCESS))))))
#?(:clj
(pu/with-compile-cond structured-task-scope-available?
(extend-type java.util.concurrent.StructuredTaskScope
pt/IAwaitable
(-await!
([it] (.join ^java.util.concurrent.StructuredTaskScope it))
([it duration]
(let [duration (if (instance? Duration duration)
duration
(Duration/ofMillis duration))
deadline (Instant/now)
deadline (.plus ^Instant deadline
^TemporalAmount duration)]
(.joinUntil ^java.util.concurrent.StructuredTaskScope it
^Instant deadline))))
pt/ICloseable
(-closed? [it]
(.isShutdown ^java.util.concurrent.StructuredTaskScope it))
(-close!
([it]
(.close ^java.util.concurrent.StructuredTaskScope it))
([it reason]
(.close ^java.util.concurrent.StructuredTaskScope it)))
pt/IExecutor
(-exec! [it task]
(let [task (wrap-bindings task)]
(.fork ^java.util.concurrent.StructuredTaskScope it ^Callable task)
nil))
(-run! [it task]
(let [task (wrap-bindings task)]
(.fork ^java.util.concurrent.StructuredTaskScope it ^Callable task)))
(-submit! [it task]
(let [task (wrap-bindings task)]
(.fork ^java.util.concurrent.StructuredTaskScope it ^Callable task))))))
#?(:clj
(pu/with-compile-cond structured-task-scope-available?
(extend-type java.util.concurrent.StructuredTaskScope$ShutdownOnFailure
pt/IAwaitable
(-await!
([it]
(.join ^java.util.concurrent.StructuredTaskScope$ShutdownOnFailure it)
(.throwIfFailed ^java.util.concurrent.StructuredTaskScope$ShutdownOnFailure it))
([it duration]
(let [duration (if (instance? Duration duration)
duration
(Duration/ofMillis duration))
deadline (Instant/now)
deadline (.plus ^Instant deadline
^TemporalAmount duration)]
(.joinUntil ^java.util.concurrent.StructuredTaskScope$ShutdownOnFailure it ^Instant deadline)
(.throwIfFailed ^java.util.concurrent.StructuredTaskScope$ShutdownOnFailure it)))))))
;; #?(:clj
;; (defn managed-blocker
;; {:no-doc true}
;; [f]
;; (let [state (volatile! nil)]
;; (reify
;; ForkJoinPool$ManagedBlocker
;; (block [_]
;; (try
;; (vreset! state (.call ^Callable f))
;; (catch Throwable cause#
;; (vreset! state cause#)))
;; true)
;; (isReleasable [_]
;; false)
;; clojure.lang.IDeref
;; (deref [_]
;; (let [v @state]
;; (if (instance? Throwable v)
;; (throw v)
;; v)))))))
;; (defmacro blocking
;; {:no-doc true}
;; [& body]
;; `(let [f# (^:once fn* [] ~@body)
;; m# (managed-blocker f#)]
;; (ForkJoinPool/managedBlock m#)
;; (deref m#)))
#?(:clj
(defn await!
"Generic await operation. Block current thread until some operatiomn terminates.
The return value is implementation specific."
([resource]