|
32 | 32 | (finally
|
33 | 33 | (.set executor-thread-local executor#)))))
|
34 | 34 |
|
35 |
| -(defn- make-thread-fn |
36 |
| - [thread-class] |
37 |
| - (eval `(fn ([group# target# name#] |
38 |
| - (new ~thread-class group# target# name#)) |
39 |
| - ([group# target# name# stack-size#] |
40 |
| - (new ~thread-class group# target# name# stack-size#))))) |
| 35 | +(defn- ^Thread new-thread |
| 36 | + ([group target name] |
| 37 | + (Thread. group target name)) |
| 38 | + ([group target name stack-size] |
| 39 | + (Thread. group target name stack-size))) |
41 | 40 |
|
42 |
| -(defn thread-factory |
43 |
| - "Returns a `java.util.concurrent.ThreadFactory`. |
| 41 | +(defn ^ThreadFactory thread-factory |
| 42 | + "Returns a `java.util.concurrent.ThreadFactory`. |
44 | 43 |
|
45 | 44 | |:---|:----
|
46 | 45 | | `name-generator` | a zero-argument function, which, when invoked returns the name of the `java.lang.Thread` that will be created. |
|
47 | 46 | | `executor-promise` | a promise eventually containing a `java.util.concurrent.Executor` that will be stored on `manifold.executor/executor-thread-local`. |
|
48 | 47 | | `stack-size` | the desired stack size for the new thread, or nil/zero to indicate that this parameter is to be ignored. |
|
49 | 48 | | `daemon?` | marks the created threads as either daemon or user threads. The Java Virtual Machine exits when the only threads running are all daemon threads. |
|
50 |
| - | `thread-class` | a `java.lang.Class` that extends `java.lang.Thread` which defines the created threads. |" |
51 |
| - ^ThreadFactory |
| 49 | + | `new-thread-fn` | a three/four arguments function which returns an implementation of `java.lang.Thread` when called. |" |
52 | 50 | ([name-generator executor-promise]
|
53 |
| - (thread-factory name-generator executor-promise nil true Thread)) |
| 51 | + (thread-factory name-generator executor-promise nil true nil)) |
54 | 52 | ([name-generator executor-promise stack-size]
|
55 |
| - (thread-factory name-generator executor-promise stack-size true Thread)) |
| 53 | + (thread-factory name-generator executor-promise stack-size true nil)) |
56 | 54 | ([name-generator executor-promise stack-size daemon?]
|
57 |
| - (thread-factory name-generator executor-promise stack-size daemon? Thread)) |
58 |
| - ([name-generator executor-promise stack-size daemon? thread-class] |
59 |
| - (let [make-thread (make-thread-fn thread-class)] |
60 |
| - (reify ThreadFactory |
61 |
| - (newThread [_ runnable] |
62 |
| - (let [name (name-generator) |
63 |
| - curr-loader (.getClassLoader (class thread-factory)) |
64 |
| - f #(do |
65 |
| - (.set executor-thread-local @executor-promise) |
66 |
| - (.run ^Runnable runnable))] |
67 |
| - (doto |
68 |
| - (if stack-size |
69 |
| - ^Thread (make-thread nil f name stack-size) |
70 |
| - ^Thread (make-thread nil f name)) |
71 |
| - (.setDaemon daemon?) |
72 |
| - (.setContextClassLoader curr-loader)))))))) |
| 55 | + (thread-factory name-generator executor-promise stack-size daemon? nil)) |
| 56 | + ([name-generator executor-promise stack-size daemon? new-thread-fn] |
| 57 | + (let [new-thread (or new-thread-fn new-thread)] |
| 58 | + (reify ThreadFactory |
| 59 | + (newThread [_ runnable] |
| 60 | + (let [name (name-generator) |
| 61 | + curr-loader (.getClassLoader (class thread-factory)) |
| 62 | + f #(do |
| 63 | + (.set executor-thread-local @executor-promise) |
| 64 | + (.run ^Runnable runnable)) |
| 65 | + thread (if stack-size |
| 66 | + ^Thread (new-thread nil f name stack-size) |
| 67 | + ^Thread (new-thread nil f name))] |
| 68 | + (doto thread |
| 69 | + (.setDaemon daemon?) |
| 70 | + (.setContextClassLoader curr-loader)))))))) |
73 | 71 |
|
74 | 72 | ;;;
|
75 | 73 |
|
|
0 commit comments