No matching definitions.

tur/reactor

stdlib/reactor.tur

lightweight evented I/O reactor.

Since: Phase R1

defopaque

EventSourceId

(EventSourceId)
defopaque linear

Reactor

(Reactor)
defn

reactor-new

(reactor-new)

create a new reactor for the current thread.

ptr<void> -- opaque reactor handle, or nil on error

(let [r (reactor-new)]
    (reactor-run r)
    (reactor-free r))

Since: Phase R1

defn

reactor-free

(reactor-free [r : Reactor])

free a reactor and unregister all attached sources.

rreactor handle returned by reactor-new
(reactor-free r)

Since: Phase R1

defn

reactor-add-fd

(reactor-add-fd [^borrow r : Reactor fd : int events : int cb : int user-data ptr<void>])

watch a file descriptor for I/O events.

rreactor handle
fdfile descriptor to watch (must already be open)
eventsbitmask of event types to watch: READ | WRITE
cbclosure: (fn [id :int events :int user :ptr<void>] :nil)
user-dataopaque value forwarded to cb as its third argument

EventSourceId -- source id (>= 0), or -1 on error (unwrap with (:: id :int))

(reactor-add-fd r listen-fd READ
    (fn [id events user]
      (handle-accept listen-fd))
    nil)

Since: Phase R2

defn

reactor-modify-fd

(reactor-modify-fd [^borrow r : Reactor id : EventSourceId new-events : int])

change the watched event mask for a source.

rreactor handle
idsource id returned by reactor-add-fd
new-eventsreplacement event bitmask

int -- 0 on success, -1 if id is not found or the modify fails

(reactor-modify-fd r source-id WRITE)

Since: Phase R2

defn

reactor-remove

(reactor-remove [^borrow r : Reactor id : EventSourceId])

unregister a source by id.

rreactor handle
idsource id returned by reactor-add-fd

int -- 0 on success, -1 if id is not found

(reactor-remove r source-id)

Since: Phase R2

defn

reactor-add-timer

(reactor-add-timer [^borrow r : Reactor delay-ms : int cb : int user-data ptr<void>])

register a one-shot timer that fires once.

rreactor handle
delay-msmilliseconds until the timer fires (>= 0)
cbclosure: (fn [id :int user :ptr<void>] :nil)
user-dataopaque value forwarded to cb as its second argument

EventSourceId -- source id (>= 0), or -1 on error (unwrap with (:: id :int))

(reactor-add-timer r 500
    (fn [id user] :nil (println "fired"))
    nil)

Since: Phase R4

defn

reactor-add-interval

(reactor-add-interval [^borrow r : Reactor first-ms : int interval-ms : int cb : int user-data ptr<void>])

register a repeating interval timer.

rreactor handle
first-msdelay before the first fire in milliseconds
interval-msperiod between subsequent fires in milliseconds
cbclosure: (fn [id :int user :ptr<void>] :nil)
user-dataopaque value forwarded to cb

EventSourceId -- source id (>= 0), or -1 on error (unwrap with (:: id :int))

(reactor-add-interval r 0 1000
    (fn [id user] :nil (println "tick"))
    nil)

Since: Phase R4

defn

reactor-add-signal

(reactor-add-signal [^borrow r : Reactor signum : int cb : int user-data ptr<void>])

watch an OS signal via the reactor.

rreactor handle
signumOS signal number (e.g. 2 for SIGINT)
cbclosure: (fn [id :int signum :int user :ptr<void>] :nil)
user-dataopaque value forwarded to cb

EventSourceId -- source id (>= 0), or -1 on error (unwrap with (:: id :int))

(reactor-add-signal r 2
    (fn [id sig user] :nil (println "got SIGINT"))
    nil)

Since: Phase R5

defn

reactor-add-chan

(reactor-add-chan [^borrow r : Reactor ch ptr<void> cb : int user-data ptr<void>])

watch a channel for one incoming value (one-shot).

rreactor handle
chchannel handle (ptr<void> from chan-new or async-chan-new)
cbclosure: (fn [id :int value :int user :ptr<void>] :nil)
user-dataopaque value forwarded to cb

EventSourceId -- source id (>= 0), or -1 on error (unwrap with (:: id :int))

(reactor-add-chan r stop-chan
    (fn [id v user] :nil (reactor-stop r))
    nil)

Since: Phase R6

defn

reactor-poll

(reactor-poll [^borrow r : Reactor timeout-ms : int])

poll for events once, dispatching ready callbacks.

rreactor handle
timeout-msmaximum wait in milliseconds (-1 = forever, 0 = non-blocking)

int -- number of callbacks dispatched (>= 0), or -1 on error

(reactor-poll r 100)  ; => 0..n

Since: Phase R1

defn

reactor-run

(reactor-run [^borrow r : Reactor])

run the event loop until stopped or all sources removed.

rreactor handle
(reactor-run r)

Since: Phase R1

defn

reactor-stop

(reactor-stop [^borrow r : Reactor])

ask reactor-run to return after the current dispatch round.

rreactor handle
(reactor-stop r)

Since: Phase R1

defn

reactor-wake

(reactor-wake [^borrow r : Reactor])

interrupt a reactor blocked in reactor-poll or reactor-run.

rreactor handle
(reactor-wake r)

Since: Phase R1

defn

local-fiber-group-new

(local-fiber-group-new [^borrow r : Reactor])

create a fiber group bound to a reactor.

rreactor handle returned by reactor-new

ptr<void> -- opaque local-fiber-group handle, or nil on error

(let [r (reactor-new)
        g (local-fiber-group-new r)]
    (reactor-run-fibers g)
    (local-fiber-group-free g)
    (reactor-free r))

Since: Phase F2

defn

local-fiber-group-free

(local-fiber-group-free [g ptr<void>])

free a fiber group and cancel any parked fibers.

ggroup handle returned by local-fiber-group-new
(local-fiber-group-free g)

Since: Phase F2

defn

local-spawn

(local-spawn [g ptr<void> body : int user-data ptr<void>])

spawn a fiber into a group.

ggroup handle
bodyclosure: (fn [user :ptr<void>] :nil)
user-dataopaque value forwarded to body as its only argument

int -- fiber id (>= 0), or -1 on error

(local-spawn g
    (fn [user] :nil (println "hello from a fiber"))
    nil)

Since: Phase F3

defn

reactor-run-fibers

(reactor-run-fibers [g ptr<void>])

drive the reactor and pump the group's fibers.

ggroup handle

int -- number of fibers that ran to completion, or -1 on re-entry/error

(local-spawn g (fn [u] :nil (println "hi")) nil)
  (reactor-run-fibers g)  ; => 1

Since: Phase F3

defn

local-park-fd

(local-park-fd [g ptr<void> fd : int events : int timeout-ms : int])

park the running fiber until an fd is ready.

ggroup handle
fdfile descriptor to wait on
eventsevent bitmask (READ | WRITE)
timeout-mstimeout in milliseconds, or -1 to wait forever

int -- the fired event mask (>= 0), -2 on timeout, or -1 if not in a fiber

(local-park-fd g read-fd READ -1)  ; => 1 when readable

Since: Phase F4

defn

local-park-chan

(local-park-chan [g ptr<void> ch ptr<void>])

park the running fiber until a channel has a value.

ggroup handle
chchannel handle (ptr<void> from chan-new or async-chan-new)

int -- the received value, or -1 if not in a fiber

(local-park-chan g ch)  ; => the next value sent on ch

Since: Phase F5

Internal definitions