|
6 | 6 | #?(:clj (:import [clojure.data.int_map PersistentIntMap]))) |
7 | 7 |
|
8 | 8 |
|
9 | | -(defrecord ThreadRegistry [;; atom of int-map with flow-id -> thread-id -> indexes |
10 | | - registry |
| 9 | +(defrecord FlowsThreadsRegistry [;; atom of int-map with flow-id -> thread-id -> thread-info |
| 10 | + registry |
11 | 11 |
|
12 | | - ;; atom of int-map with flow-id -> TotalOrderTimeline |
13 | | - total-order-timelines |
| 12 | + ;; atom of int-map with flow-id -> TotalOrderTimeline |
| 13 | + total-order-timelines] |
14 | 14 |
|
15 | | - ;; atom with threads events callbacks, like thread creation |
16 | | - callbacks] |
17 | | - |
18 | | - index-protos/ThreadRegistryP |
| 15 | + index-protos/FlowsThreadsRegistryP |
19 | 16 |
|
20 | 17 | (all-threads [_] |
21 | 18 | (reduce-kv (fn [all-ths fid threads] |
22 | 19 | (into all-ths (mapv |
23 | 20 | (fn [tid] [fid tid]) |
24 | 21 | (keys threads)))) |
25 | 22 | #{} |
26 | | - @registry)) |
| 23 | + @registry)) |
27 | 24 |
|
28 | 25 | (flow-threads-info [_ flow-id] |
29 | 26 | (->> (get @registry flow-id) |
|
37 | 34 | (flow-exists? [_ flow-id] |
38 | 35 | (contains? @registry flow-id)) |
39 | 36 |
|
40 | | - (get-thread-indexes [_ flow-id thread-id] |
| 37 | + (get-thread-tracker [_ flow-id thread-id] |
41 | 38 | #?(:clj |
42 | 39 | (some-> ^PersistentIntMap @registry |
43 | 40 | ^PersistentIntMap (.get flow-id) |
44 | | - ^clojure.lang.PersistentArrayMap (.get thread-id) |
45 | | - (.get :thread/indexes)) |
| 41 | + ^clojure.lang.PersistentArrayMap (.get thread-id)) |
46 | 42 | :cljs |
47 | 43 | (some-> @registry |
48 | 44 | (get flow-id) |
49 | | - (get thread-id) |
50 | | - (get :thread/indexes)))) |
51 | | - |
52 | | - (register-thread-indexes [this flow-id thread-id thread-name form-id indexes] |
53 | | - (when-not (index-protos/flow-exists? this flow-id) |
54 | | - (swap! total-order-timelines assoc flow-id (total-order-timeline/make-total-order-timeline flow-id))) |
55 | | - |
56 | | - (swap! registry update flow-id |
57 | | - (fn [threads] |
58 | | - (assoc (or threads (int-map)) thread-id {:thread/id thread-id |
59 | | - :thread/name (if (str/blank? thread-name) |
60 | | - (str "Thread-" thread-id) |
61 | | - thread-name) |
62 | | - :thread/indexes indexes |
63 | | - :thread/blocked nil}))) |
64 | | - |
65 | | - (when-let [otc (:on-thread-created @callbacks)] |
66 | | - (otc {:flow-id flow-id |
67 | | - :thread-id thread-id |
68 | | - :thread-name thread-name |
69 | | - :form-id form-id}))) |
70 | | - |
71 | | - (set-thread-blocked [this flow-id thread-id breakpoint] |
72 | | - (when (index-protos/get-thread-indexes this flow-id thread-id) |
73 | | - (swap! registry assoc-in [flow-id thread-id :thread/blocked] breakpoint))) |
| 45 | + (get thread-id)))) |
| 46 | + |
| 47 | + (register-thread [this flow-id thread-id thread-name timeline init-fn-call-limits] |
| 48 | + (let [thread-tracker {:thread/id thread-id |
| 49 | + :thread/name (if (str/blank? thread-name) |
| 50 | + (str "Thread-" thread-id) |
| 51 | + thread-name) |
| 52 | + :thread/timeline timeline |
| 53 | + :thread/*fn-call-limits (atom init-fn-call-limits) |
| 54 | + :thread/*thread-limited (atom nil) |
| 55 | + :thread/blocked nil}] |
| 56 | + (when-not (index-protos/flow-exists? this flow-id) |
| 57 | + (swap! total-order-timelines assoc flow-id (total-order-timeline/make-total-order-timeline flow-id))) |
| 58 | + |
| 59 | + (swap! registry update flow-id |
| 60 | + (fn [threads] |
| 61 | + (assoc (or threads (int-map)) thread-id thread-tracker))) |
| 62 | + thread-tracker)) |
| 63 | + |
| 64 | + (set-thread-blocked [_ flow-id thread-id breakpoint] |
| 65 | + (swap! registry assoc-in [flow-id thread-id :thread/blocked] breakpoint)) |
74 | 66 |
|
75 | 67 | (discard-threads [this flow-threads-ids] |
76 | 68 | (doseq [[fid tid] flow-threads-ids] |
|
79 | 71 | ;; remove empty flows from the registry since flow-exist? uses it |
80 | 72 | ;; kind of HACKY... |
81 | 73 | (let [empty-flow-ids (reduce-kv (fn [efids fid threads-map] |
82 | | - (if (empty? threads-map) |
83 | | - (conj efids fid) |
84 | | - efids)) |
85 | | - #{} |
86 | | - @registry)] |
| 74 | + (if (empty? threads-map) |
| 75 | + (conj efids fid) |
| 76 | + efids)) |
| 77 | + #{} |
| 78 | + @registry)] |
87 | 79 | (swap! registry (fn [flows-map] (apply dissoc flows-map empty-flow-ids)))) |
88 | 80 |
|
89 | 81 | (doseq [[fid] flow-threads-ids] |
90 | 82 | (index-protos/tot-clear-all (index-protos/total-order-timeline this fid)))) |
91 | 83 |
|
92 | | - (start-thread-registry [thread-reg cbs] |
93 | | - (reset! callbacks cbs) |
94 | | - thread-reg) |
95 | | - |
96 | | - (stop-thread-registry [_] nil) |
97 | | - |
98 | 84 | (record-total-order-entry [_ flow-id th-timeline th-idx] |
99 | 85 | (-> (get @total-order-timelines flow-id) |
100 | 86 | (index-protos/tot-add-entry th-timeline th-idx))) |
101 | 87 |
|
102 | 88 | (total-order-timeline [_ flow-id] |
103 | 89 | (get @total-order-timelines flow-id))) |
104 | 90 |
|
105 | | -(defn make-thread-registry [] |
106 | | - (map->ThreadRegistry {:registry (atom (int-map)) |
107 | | - :total-order-timelines (atom (int-map)) |
108 | | - :callbacks (atom {})})) |
| 91 | +(defn make-flows-threads-registry [] |
| 92 | + (map->FlowsThreadsRegistry {:registry (atom (int-map)) |
| 93 | + :total-order-timelines (atom (int-map))})) |
0 commit comments