summaryrefslogtreecommitdiff
path: root/vim/bundle/slimv/swank-clojure/swank/util/concurrent/thread.clj
blob: fa77a2260b4c96d646ef16b21342b56060368c41 (plain)
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
(ns swank.util.concurrent.thread
  (:use (swank util)))

(defn- gen-name []
  (name (gensym "Thread-")))

(defn start-thread
  "Starts a thread that run the given function f"
  ([#^Runnable f]
     (doto (Thread. f)
       (.start))))

(defmacro dothread [& body]
  `(start-thread (fn [] ~@body)))

(defmacro dothread-keeping [bindings & body]
  `(start-thread (keep-bindings ~bindings (fn [] ~@body))))

(defmacro dothread-keeping-clj [more-bindings & body]
  (let [clj-star-syms (filter #(or (= (name %) "*e")
                                   (= (name %) "*1")
                                   (= (name %) "*2")
                                   (= (name %) "*3")
                                   (and (.startsWith #^String (name %) "*")
                                        (.endsWith #^String (name %) "*")
                                        (> (count (name %)) 1)))
                              (keys (ns-publics (find-ns 'clojure.core))))]
    `(dothread-keeping [~@clj-star-syms ~@more-bindings]
       ~@body)))

(defn current-thread []
  (Thread/currentThread))

(defn thread-set-name
  ([name] (thread-set-name (current-thread) name))
  ([#^Thread thread name]
     (.setName thread name)))

(defn thread-name
  ([] (thread-name (current-thread)))
  ([#^Thread thread]
     (.getName thread)))

(defn thread-id
  ([] (thread-id (current-thread)))
  ([#^Thread thread]
     (.getId thread)))

(defn thread-alive? [#^Thread t]
  (.isAlive t))