|
32 | 32 | (finally
|
33 | 33 | (.set executor-thread-local executor#)))))
|
34 | 34 |
|
| 35 | +(defn- ^Thread new-thread |
| 36 | + "Creates a new `java.lang.Thread`. |
| 37 | +
|
| 38 | + It represents the default implementation on `thread-factory` when the |
| 39 | + `new-thread-fn` argument is not passed. |
| 40 | +
|
| 41 | + Some libraries require a different implementation of a `java.lang.Thread`. |
| 42 | + That's the case of Netty which behaves differently when |
| 43 | + running on a `io.netty.util.concurrent.FastThreadLocalThread`." |
| 44 | + [group target name stack-size] |
| 45 | + (Thread. group target name stack-size)) |
| 46 | + |
35 | 47 | (defn ^ThreadFactory thread-factory
|
| 48 | + "Returns a `java.util.concurrent.ThreadFactory`. |
| 49 | +
|
| 50 | + |:---|:---- |
| 51 | + | `name-generator` | a zero-argument function, which, when invoked returns the name of the `java.lang.Thread` that will be created. | |
| 52 | + | `executor-promise` | a promise eventually containing a `java.util.concurrent.Executor` that will be stored on `manifold.executor/executor-thread-local`. | |
| 53 | + | `stack-size` | the desired stack size for the new thread, or nil/zero to indicate that this parameter is to be ignored. | |
| 54 | + | `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. | |
| 55 | + | `new-thread-fn` | a four arguments function which returns an implementation of `java.lang.Thread` when called. |" |
36 | 56 | ([name-generator executor-promise]
|
37 |
| - (thread-factory name-generator executor-promise nil true)) |
| 57 | + (thread-factory name-generator executor-promise nil true nil)) |
38 | 58 | ([name-generator executor-promise stack-size]
|
39 |
| - (thread-factory name-generator executor-promise stack-size true)) |
| 59 | + (thread-factory name-generator executor-promise stack-size true nil)) |
40 | 60 | ([name-generator executor-promise stack-size daemon?]
|
41 |
| - (reify ThreadFactory |
42 |
| - (newThread [_ runnable] |
43 |
| - (let [name (name-generator) |
44 |
| - curr-loader (.getClassLoader (class thread-factory)) |
45 |
| - f #(do |
46 |
| - (.set executor-thread-local @executor-promise) |
47 |
| - (.run ^Runnable runnable))] |
48 |
| - (doto |
49 |
| - (if stack-size |
50 |
| - (Thread. nil f name stack-size) |
51 |
| - (Thread. nil f name)) |
52 |
| - (.setDaemon daemon?) |
53 |
| - (.setContextClassLoader curr-loader))))))) |
| 61 | + (thread-factory name-generator executor-promise stack-size daemon? nil)) |
| 62 | + ([name-generator executor-promise stack-size daemon? new-thread-fn] |
| 63 | + (let [new-thread (or new-thread-fn new-thread)] |
| 64 | + (reify ThreadFactory |
| 65 | + (newThread [_ runnable] |
| 66 | + (let [name (name-generator) |
| 67 | + curr-loader (.getClassLoader (class thread-factory)) |
| 68 | + f #(do |
| 69 | + (.set executor-thread-local @executor-promise) |
| 70 | + (.run ^Runnable runnable)) |
| 71 | + thread ^Thread (new-thread nil f name (or stack-size 0))] |
| 72 | + (doto thread |
| 73 | + (.setDaemon daemon?) |
| 74 | + (.setContextClassLoader curr-loader)))))))) |
54 | 75 |
|
55 | 76 | ;;;
|
56 | 77 |
|
|
0 commit comments