|
| 1 | +(ns pixie.channels |
| 2 | + (require pixie.stacklets :as st) |
| 3 | + (require pixie.buffers :as b)) |
| 4 | + |
| 5 | +(defprotocol ICancelable |
| 6 | + (-canceled? [this] "Determines if a request (such as a callback) that can be canceled") |
| 7 | + (-commit! [this])) |
| 8 | + |
| 9 | +(defprotocol IReadPort |
| 10 | + (-take! [this cfn] "Take a value from this port passing it to a cancellable function")) |
| 11 | + |
| 12 | +(defprotocol IWritePort |
| 13 | + (-put! [this itm cfn] "Write a value to this port passing true if the write succeeds and the |
| 14 | + callback isn't canceled")) |
| 15 | + |
| 16 | +(defprotocol ICloseable |
| 17 | + (-close! [this] "Closes the channel, future writes will be rejected, future reads will |
| 18 | + drain the channel before returning nil.")) |
| 19 | + |
| 20 | +(deftype OpCell [val cfn] |
| 21 | + IIndexed |
| 22 | + (-nth [this idx] |
| 23 | + (cond |
| 24 | + (= idx 0) val |
| 25 | + (= idx 1) cfn |
| 26 | + :else (throw "Index out of range"))) |
| 27 | + (-nth-not-found [this idx not-found] |
| 28 | + (cond |
| 29 | + (= idx 0) val |
| 30 | + (= idx 1) cfn |
| 31 | + :else not-found)) |
| 32 | + ICounted |
| 33 | + (-count [this] |
| 34 | + 2) |
| 35 | + ICancelable |
| 36 | + (-canceled? [this] |
| 37 | + (canceled? cfn))) |
| 38 | + |
| 39 | +(defn canceled? [this] |
| 40 | + (-canceled? this)) |
| 41 | + |
| 42 | + |
| 43 | +(defn -move-puts-to-buffer [puts buffer] |
| 44 | + (loop [] |
| 45 | + (if (or (b/full? buffer) |
| 46 | + (b/empty-buffer? puts)) |
| 47 | + nil |
| 48 | + (let [[val cfn] (b/remove! puts)] |
| 49 | + (if (cancelled? cfn) |
| 50 | + (recur) |
| 51 | + (do (st/-run-later (partial cfn true)) |
| 52 | + (b/add! buffer val) |
| 53 | + (recur))))))) |
| 54 | + |
| 55 | +(defn -get-non-canceled! [buffer] |
| 56 | + (loop [] |
| 57 | + (if (b/empty-buffer? buffer) |
| 58 | + nil |
| 59 | + (let [v (b/remove! buffer)] |
| 60 | + (if (canceled? v) |
| 61 | + (recur) |
| 62 | + v))))) |
| 63 | + |
| 64 | + |
| 65 | +(deftype MultiReaderWriterChannel [puts takes buffer closed? ops-since-last-clean] |
| 66 | + IReadPort |
| 67 | + (-take! [this cfn] |
| 68 | + (if (canceled? cfn) |
| 69 | + false |
| 70 | + (if (and closed? |
| 71 | + (b/empty-buffer? buffer) |
| 72 | + (b/empty-buffer? puts)) |
| 73 | + (do (-commit! cfn) |
| 74 | + (st/-run-later (partial cfn nil)) |
| 75 | + false) |
| 76 | + (if (not (b/empty-buffer? buffer)) |
| 77 | + (do (-commit! cfn) |
| 78 | + (st/-run-later (partial cfn (b/remove! buffer))) |
| 79 | + (-move-puts-to-buffer puts buffer)) |
| 80 | + |
| 81 | + (if-let [[v pcfn] (-get-non-canceled! puts)] |
| 82 | + (do (-commit! pcfn) |
| 83 | + (-commit! cfn) |
| 84 | + (st/-run-later (partial pcfn true)) |
| 85 | + (st/-run-later (partial cfn v)) |
| 86 | + true) |
| 87 | + (do (set-field! this :ops-since-last-clean (inc ops-since-last-clean)) |
| 88 | + (b/add-unbounded! takes cfn) |
| 89 | + true)))))) |
| 90 | + IWritePort |
| 91 | + (-put! [this val cfn] |
| 92 | + (if (or (canceled? cfn)) |
| 93 | + false |
| 94 | + (if closed? |
| 95 | + (do (-commit! cfn) |
| 96 | + (st/-run-later (partial cfn false)) |
| 97 | + false) |
| 98 | + (if-let [tfn (-get-non-canceled! takes)] |
| 99 | + (do (-commit! cfn) |
| 100 | + (-commit! tfn) |
| 101 | + (st/-run-later (partial tfn val)) |
| 102 | + (st/-run-later (partial cfn true)) |
| 103 | + true) |
| 104 | + (if (not (b/full? buffer)) |
| 105 | + (do (b/add! buffer val) |
| 106 | + (-commit! cfn) |
| 107 | + (st/-run-later (partial cfn true)) |
| 108 | + true) |
| 109 | + (do (b/add-unbounded! puts (->OpCell val cfn)) |
| 110 | + (set-field! this :ops-since-last-clean (inc ops-since-last-clean)) |
| 111 | + true)))))) |
| 112 | + ICloseable |
| 113 | + (-close! [this] |
| 114 | + (set-field! this :closed? true) |
| 115 | + (when (not (b/empty-buffer? takes)) |
| 116 | + (loop [] |
| 117 | + (when-let [tfn (-get-non-canceled! takes)] |
| 118 | + (-commit! tfn) |
| 119 | + (st/-run-later (partial tfn nil)) |
| 120 | + (recur)))))) |
| 121 | + |
| 122 | +(defn chan |
| 123 | + "Creates a CSP channel with the given buffer. If an integer is provided as the argument |
| 124 | + creates a channel with a fixed buffer of that size. " |
| 125 | + ([] |
| 126 | + (chan 0)) |
| 127 | + ([size-or-buffer] |
| 128 | + (if (= 0 size-or-buffer) |
| 129 | + (->MultiReaderWriterChannel (b/ring-buffer 8) |
| 130 | + (b/ring-buffer 8) |
| 131 | + b/null-buffer |
| 132 | + false |
| 133 | + 0) |
| 134 | + (if (integer? size-or-buffer) |
| 135 | + (->MultiReaderWriterChannel (b/ring-buffer 8) |
| 136 | + (b/ring-buffer 8) |
| 137 | + (b/fixed-buffer size-or-buffer) |
| 138 | + false |
| 139 | + 0) |
| 140 | + (->MultiReaderWriterChannel (b/ring-buffer 8) |
| 141 | + (b/ring-buffer 8) |
| 142 | + size-or-buffer |
| 143 | + false |
| 144 | + 0))))) |
| 145 | + |
| 146 | +(deftype AltHandler [atm f] |
| 147 | + ICancelable |
| 148 | + (-canceled? [this] |
| 149 | + @atm) |
| 150 | + (-commit! [this] |
| 151 | + (reset! atm true)) |
| 152 | + IFn |
| 153 | + (-invoke [this & args] |
| 154 | + (apply f args))) |
| 155 | + |
| 156 | +(defn alt-handlers [fns] |
| 157 | + (mapv (partial ->AltHandler (atom false)) fns)) |
| 158 | + |
| 159 | +(extend -canceled? IFn |
| 160 | + (fn [this] false)) |
| 161 | + |
| 162 | +(extend -commit! IFn |
| 163 | + (fn [this] nil)) |
| 164 | + |
| 165 | +(defn alts! [ops k options] |
| 166 | + (let [handler-atom (atom false)] |
| 167 | + (reduce |
| 168 | + (fn [_ op] |
| 169 | + (if (vector? op) |
| 170 | + (let [[c val] op |
| 171 | + f (fn [v] |
| 172 | + (st/-run-later (partial k [c v])))] |
| 173 | + (-put! c val (->AltHandler handler-atom f))) |
| 174 | + (let [c op |
| 175 | + f (fn [v] |
| 176 | + (st/-run-later (partial k [c v])))] |
| 177 | + (-take! c (->AltHandler handler-atom f))))) |
| 178 | + nil |
| 179 | + ops) |
| 180 | + (when (and (contains? options :default) |
| 181 | + (not @handler-atom)) |
| 182 | + (reset! handler-atom true) |
| 183 | + (st/-run-later (partial k [:default (:default options)]))))) |
0 commit comments