Pipe resources. Works across threads, not processes nor over the network.

Pipes are used to support selectors in custom resources.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(ns foo.baz
  (:api dunaj)
  (:require [dunaj.resource.pipe :refer [pipe-factory]]
            [dunaj.concurrent.thread :refer [sleep]]))

;; blocking example
(with-scope
  (let [[sink source] (acquire! pipe-factory)]
    (future
      (println! "monitor enter " (config sink))
      (io!
       (try
         (doall (map #(println! "received batch" (vec %))
                     (batched (read! source))))
         (catch java.nio.channels.AsynchronousCloseException e
           (println! "monitor exit"))
         (catch java.lang.Exception e
           (println! "exception in monitor "
                     (clojure.stacktrace/print-stack-trace e))))))

    (write! sink [1 2 3 4 5 6])

    (sleep 1000)

    (write! sink [7 8 9 10])

    (sleep 1000)))
;; monitor enter  {:batch-size 8192 :selector-provider #<EPollSelectorProvider [email protected]> :pipe #<PipeImpl [email protected]> :non-blocking? false}
;; received batch [1 2 3 4 5 6]
;; received batch [7 8 9 10]
;; monitor exit
;;=> nil

;; non-blocking with selector
(with-io-scope
  (let [[sink1 source1] (acquire! pipe-factory :non-blocking? true)
        [sink2 source2] (acquire! pipe-factory :non-blocking? true)
        sel (acquire! (selector))
        dumpf #(dored [x (read! (:resource %))] (println! "got" x))
        rf (fn [ret val]
             (println! "resource is ready" val) (dumpf val) ret)]
    (register! sel source1 :all)
    (register! sel source2 :all)
    (thread
     (io!
      (loop [x (reduce rf nil (read! sel))]
        (if (postponed? x)
          (do (println! "resources ready:" (select sel))
              (recur (unsafe-advance! x)))
          (println! "end")))))
    (write! sink1 [1 2 3])
    (write! sink2 [4 5 6])
    (sleep 1000)
    (write! sink2 [7 8 9])
    (sleep 1000)))
;; resources ready: 2
;; resource is ready {:ready (:read) :resource #<SourceResource [email protected]>}
;; got 1
;; got 2
;; got 3
;; resource is ready {:ready (:read) :resource #<SourceResource [email protected]>}
;; got 4
;; got 5
;; got 6
;; resources ready: 1
;; resource is ready {:ready (:read) :resource #<SourceResource [email protected]>}
;; got 7
;; got 8
;; got 9
;; resources ready: 0
;; end
;;=> nil

pipe

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (pipe & {:as opts})

Type signature:
  • (Any) → IAcquirableFactory

Returns pipe factory with given opts set. acquire! will return pair of sink and source resources.

See also: pipe-factory

pipe-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type IAcquirableFactory

Pipe resource factory. Current options are:

  • :batch-size - batch size, low level

  • :selector-provider - nil or host specific selector provider

  • :non-blocking? - nil (default) or true if non-blocking mode is requested.

See also: pipe