Skip to content

Commit 4ce39ea

Browse files
committed
Set version to 9.1.538
1 parent 588c112 commit 4ce39ea

File tree

6 files changed

+98
-25
lines changed

6 files changed

+98
-25
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog #
22

3+
4+
## Version 9.1.538
5+
6+
- Add `px/current-thread` helper function
7+
- Add `px/thread-interrupted?` helper function
8+
- Add `px/interrupt-thread!` helper function
9+
- Add `px/join!` helper function
10+
- Add `px/thread-id` helper function
11+
12+
313
## Version 9.1.536
414

515
- Assign default parallelism to scheduled executor (based on CPUs).

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ Here you can look a detailed [documentation][1].
1616
deps.edn:
1717

1818
```clojure
19-
funcool/promesa {:mvn/version "9.1.536"}
19+
funcool/promesa {:mvn/version "9.1.538"}
2020
```
2121

2222
Leiningen:
2323

2424
```clojure
25-
[funcool/promesa "9.1.536"]
25+
[funcool/promesa "9.1.538"]
2626
```
2727

2828
## On the REPL

deps.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{com.bhauman/rebel-readline-cljs {:mvn/version "RELEASE"},
66
com.bhauman/rebel-readline {:mvn/version "RELEASE"},
77
org.clojure/tools.namespace {:mvn/version "RELEASE"},
8+
org.clojure/core.async {:mvn/version "1.6.673"}
89
criterium/criterium {:mvn/version "RELEASE"}
910
thheller/shadow-cljs {:mvn/version "RELEASE"}}
1011
:extra-paths ["test" "dev" "src"]},

dev/user.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[clojure.test :as test]
55
[clojure.tools.namespace.repl :as r]
66
[clojure.walk :refer [macroexpand-all]]
7+
[clojure.core.async :as a]
78
[criterium.core :refer [quick-bench bench with-progress-reporting]]
89
[promesa.core :as p]
910
[promesa.exec :as px]
@@ -14,7 +15,8 @@
1415
(:import
1516
java.util.concurrent.CompletableFuture
1617
java.util.concurrent.CompletionStage
17-
java.util.function.Function))
18+
java.util.function.Function
19+
java.util.concurrent.atomic.AtomicLong))
1820

1921
(defmacro run-quick-bench
2022
[& exprs]

scripts/repl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
set -ex
33
# clojure -M:dev -e "(set! *warn-on-reflection* true)" -m rebel-readline.main
44
# clojure -M:dev -m rebel-readline.main
5-
clojure -J--enable-preview -M:dev -m rebel-readline.main
5+
clojure -J-Djdk.tracePinnedThreads=short -J-Djdk.virtualThreadScheduler.parallelism=16 -J--enable-preview -M:dev -m rebel-readline.main

src/promesa/exec.cljc

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
(:import
1515
clojure.lang.Var
1616
java.lang.AutoCloseable
17+
java.time.Duration
1718
java.util.concurrent.Callable
1819
java.util.concurrent.CompletableFuture
1920
java.util.concurrent.Executor
@@ -32,7 +33,6 @@
3233

3334
#?(:clj (set! *warn-on-reflection* true))
3435

35-
3636
;; --- Globals & Defaults (with CLJS Impl)
3737

3838
(declare #?(:clj scheduled-executor :cljs ->ScheduledExecutor))
@@ -223,7 +223,16 @@
223223
(^Thread newThread [_ ^Runnable runnable]
224224
(func runnable)))))
225225

226-
#?(:clj (def ^{:no-doc true} counter (AtomicLong. 0)))
226+
#?(:clj (def ^{:no-doc true :dynamic true}
227+
*default-counter*
228+
(AtomicLong. 0)))
229+
230+
#?(:clj
231+
(defn get-next
232+
"Get next value from atomic long counter"
233+
{:no-doc true}
234+
([] (.getAndIncrement ^AtomicLong *default-counter*))
235+
([counter] (.getAndIncrement ^AtomicLong counter))))
227236

228237
#?(:clj
229238
(defn default-thread-factory
@@ -232,37 +241,27 @@
232241
:or {daemon true
233242
priority Thread/NORM_PRIORITY
234243
name "promesa/thread/%s"}}]
235-
(reify ThreadFactory
236-
(newThread [this runnable]
237-
(doto (Thread. ^Runnable runnable)
238-
(.setPriority priority)
239-
(.setDaemon ^Boolean daemon)
240-
(.setName (format name (.getAndIncrement ^AtomicLong counter))))))))
244+
(let [counter (AtomicLong. 0)]
245+
(reify ThreadFactory
246+
(newThread [this runnable]
247+
(doto (Thread. ^Runnable runnable)
248+
(.setPriority priority)
249+
(.setDaemon ^Boolean daemon)
250+
(.setName (format name (get-next counter)))))))))
241251

242252
#?(:clj
243253
(defn default-forkjoin-thread-factory
244254
^ForkJoinPool$ForkJoinWorkerThreadFactory
245255
[& {:keys [name daemon] :or {name "promesa/forkjoin/%s" daemon true}}]
246-
(let [^AtomicLong counter (AtomicLong. 0)]
256+
(let [counter (AtomicLong. 0)]
247257
(reify ForkJoinPool$ForkJoinWorkerThreadFactory
248258
(newThread [_ pool]
249259
(let [thread (.newThread ForkJoinPool/defaultForkJoinWorkerThreadFactory pool)
250-
tname (format name (.getAndIncrement counter))]
260+
tname (format name (get-next counter))]
251261
(.setName ^ForkJoinWorkerThread thread ^String tname)
252262
(.setDaemon ^ForkJoinWorkerThread thread ^Boolean daemon)
253263
thread))))))
254264

255-
#?(:clj
256-
(defn- opts->thread-factory
257-
[{:keys [daemon priority]
258-
:or {daemon true priority Thread/NORM_PRIORITY}}]
259-
(fn->thread-factory
260-
(fn [runnable]
261-
(let [thread (Thread. ^Runnable runnable)]
262-
(.setDaemon thread daemon)
263-
(.setPriority thread priority)
264-
thread)))))
265-
266265
#?(:clj
267266
(defn- resolve-thread-factory
268267
{:no-doc true}
@@ -591,4 +590,65 @@
591590
(cons (map first ss) (step-fn (map rest ss)))))))]
592591
(pmap #(apply f %) (step-fn (cons coll colls)))))))
593592

593+
#?(:clj
594+
(defmacro thread
595+
"A low-level, not-pooled thread constructor."
596+
[opts & body]
597+
(let [[opts body] (if (map? opts)
598+
[opts body]
599+
[nil (cons opts body)])]
600+
`(let [opts# ~opts
601+
thr# (Thread. (^:once fn* [] ~@body))]
602+
(.setName thr# (or (:name ~opts) (format "promesa/unnamed-thread/%s" (get-next))))
603+
(.setDaemon thr# (:daemon? ~opts false))
604+
(.setPriority thr# (:priority ~opts Thread/NORM_PRIORITY))
605+
(.start thr#)
606+
thr#))))
607+
608+
#?(:clj
609+
(defn current-thread
610+
"Return the current thread."
611+
[]
612+
(Thread/currentThread)))
594613

614+
#?(:clj
615+
(defn thread-interrupted?
616+
"Check if the thread has the interrupted flag set.
617+
618+
There are two special cases:
619+
620+
Using the `:current` keyword as argument will check the interrupted
621+
flag on the current thread.
622+
623+
Using the arity 0 (passing no arguments), then the current thread
624+
will be checked and **WARNING** the interrupted flag reset to
625+
`false`."
626+
([]
627+
(Thread/interrupted))
628+
([thread]
629+
(if (= :current thread)
630+
(.isInterrupted (Thread/currentThread))
631+
(.isInterrupted ^Thread thread)))))
632+
633+
#?(:clj
634+
(defn thread-id
635+
"Retrieves the thread ID."
636+
([]
637+
(.getId ^Thread (Thread/currentThread)))
638+
([^Thread thread]
639+
(.getId thread))))
640+
641+
#?(:clj
642+
(defn interrupt-thread!
643+
[^Thread thread]
644+
(.interrupt thread)))
645+
646+
#?(:clj
647+
(defn join!
648+
"Waits for the specified thread to terminate."
649+
([^Thread thread]
650+
(.join thread))
651+
([^Thread thread duration]
652+
(if (instance? Duration duration)
653+
(.join thread ^Duration duration)
654+
(.join thread (long duration))))))

0 commit comments

Comments
 (0)