Janet 1.38.0-73334f3 Documentation
(Other Versions: 1.37.1 1.36.0 1.35.0 1.34.0 1.31.0 1.29.1 1.28.0 1.27.0 1.26.0 1.25.1 1.24.0 1.23.0 1.22.0 1.21.0 1.20.0 1.19.0 1.18.1 1.17.1 1.16.1 1.15.0 1.13.1 1.12.2 1.11.1 1.10.1 1.9.1 1.8.1 1.7.0 1.6.0 1.5.1 1.5.0 1.4.0 1.3.1 )

Event Module

Functions related to evented (asynchronous) IO and a fiber based event loop.

Index

ev/acquire-lock ev/acquire-rlock ev/acquire-wlock ev/all-tasks ev/call ev/cancel ev/capacity ev/chan ev/chan-close ev/chunk ev/close ev/count ev/deadline ev/do-thread ev/full ev/gather ev/give ev/give-supervisor ev/go ev/lock ev/read ev/release-lock ev/release-rlock ev/release-wlock ev/rselect ev/rwlock ev/select ev/sleep ev/spawn ev/spawn-thread ev/take ev/thread ev/thread-chan ev/to-file ev/with-deadline ev/with-lock ev/with-rlock ev/with-wlock ev/write


ev/acquire-lock cfunction source

(ev/acquire-lock lock)

Acquire a lock such that this operating system thread is the only thread with access to this resource. This will block this entire thread until the lock becomes available, and will not yield to other fibers on this system thread.

Community Examples

ev/acquire-rlock cfunction source

(ev/acquire-rlock rwlock)

Acquire a read lock an a read-write lock.

Community Examples

ev/acquire-wlock cfunction source

(ev/acquire-wlock rwlock)

Acquire a write lock on a read-write lock.

Community Examples

ev/all-tasks cfunction source

(ev/all-tasks)

Get an array of all active fibers that are being used by the scheduler.

Community Examples

ev/call function source

(ev/call f & args)

Call a function asynchronously. Returns a fiber that is scheduled to run the function.

Community Examples

ev/cancel cfunction source

(ev/cancel fiber err)

Cancel a suspended fiber in the event loop. Differs from cancel in that it returns the canceled fiber immediately.

Community Examples

ev/capacity cfunction source

(ev/capacity channel)

Get the number of items a channel will store before blocking writers.

Community Examples

ev/chan cfunction source

(ev/chan &opt capacity)

Create a new channel. capacity is the number of values to queue before blocking writers, defaults to 0 if not provided. Returns a new channel.

Community Examples

ev/chan-close cfunction source

(ev/chan-close chan)

Close a channel. A closed channel will cause all pending reads and writes to return nil. Returns the channel.

Community Examples

ev/chunk cfunction source

(ev/chunk stream n &opt buffer timeout)

Same as ev/read, but will not return early if less than n bytes are available. If an end of stream is reached, will also return early with the collected bytes.

Community Examples

ev/close cfunction source

(ev/close stream)

Close a stream. This should be the same as calling (:close stream) for all streams.

Community Examples

ev/count cfunction source

(ev/count channel)

Get the number of items currently waiting in a channel.

Community Examples

ev/deadline cfunction source

(ev/deadline sec &opt tocancel tocheck)

Schedules the event loop to try to cancel the tocancel task as with ev/cancel. After sec seconds, the event loop will attempt cancellation of tocancel if the tocheck fiber is resumable. sec is a number that can have a fractional part. tocancel defaults to (fiber/root), but if specified, must be a task (root fiber). tocheck defaults to (fiber/current), but if specified, should be a fiber. Returns tocancel immediately.

Community Examples

ev/do-thread macro source

(ev/do-thread & body)

Run some code in a new thread. Suspends the current fiber until the thread is complete, and evaluates to nil.

Community Examples

ev/full cfunction source

(ev/full channel)

Check if a channel is full or not.

Community Examples

ev/gather macro source

(ev/gather & bodies)

Run a number of fibers in parallel on the event loop, and join when they complete. Returns the gathered results in an array.

Community Examples

ev/give cfunction source

(ev/give channel value)

Write a value to a channel, suspending the current fiber if the channel is full. Returns the channel if the write succeeded, nil otherwise.

Community Examples

ev/give-supervisor cfunction source

(ev/give-supervisor tag & payload)

Send a message to the current supervisor channel if there is one. The message will be a tuple of all of the arguments combined into a single message, where the first element is tag. By convention, tag should be a keyword indicating the type of message. Returns nil.

Community Examples

ev/go cfunction source

(ev/go fiber-or-fun &opt value supervisor)

Put a fiber on the event loop to be resumed later. If a function is used, it is wrapped with fiber/new first. Optionally pass a value to resume with, otherwise resumes with nil. Returns the fiber. An optional core/channel can be provided as a supervisor. When various events occur in the newly scheduled fiber, an event will be pushed to the supervisor. If not provided, the new fiber will inherit the current supervisor.

Community Examples

ev/lock cfunction source

(ev/lock)

Create a new lock to coordinate threads.

Community Examples

ev/read cfunction source

(ev/read stream n &opt buffer timeout)

Read up to n bytes into a buffer asynchronously from a stream. n can also be the keyword :all to read into the buffer until end of stream. Optionally provide a buffer to write into as well as a timeout in seconds after which to cancel the operation and raise an error. Returns the buffer if the read was successful or nil if end-of-stream reached. Will raise an error if there are problems with the IO operation.

Community Examples

ev/release-lock cfunction source

(ev/release-lock lock)

Release a lock such that other threads may acquire it.

Community Examples

ev/release-rlock cfunction source

(ev/release-rlock rwlock)

Release a read lock on a read-write lock

Community Examples

ev/release-wlock cfunction source

(ev/release-wlock rwlock)

Release a write lock on a read-write lock

Community Examples

ev/rselect cfunction source

(ev/rselect & clauses)

Similar to ev/select, but will try clauses in a random order for fairness.

Community Examples

ev/rwlock cfunction source

(ev/rwlock)

Create a new read-write lock to coordinate threads.

Community Examples

ev/select cfunction source

(ev/select & clauses)

Block until the first of several channel operations occur. Returns a tuple of the form [:give chan], [:take chan x], or [:close chan], where a :give tuple is the result of a write and a :take tuple is the result of a read. Each clause must be either a channel (for a channel take operation) or a tuple [channel x] (for a channel give operation). Operations are tried in order such that earlier clauses take precedence over later clauses. Both give and take operations can return a [:close chan] tuple, which indicates that the specified channel was closed while waiting, or that the channel was already closed.

Community Examples

ev/sleep cfunction source

(ev/sleep sec)

Suspend the current fiber for sec seconds without blocking the event loop.

Community Examples

ev/spawn macro source

(ev/spawn & body)

Run some code in a new fiber. This is shorthand for (ev/go (fn [] ;body)).

Community Examples

ev/spawn-thread macro source

(ev/spawn-thread & body)

Run some code in a new thread. Like ev/do-thread, but returns nil immediately.

Community Examples

ev/take cfunction source

(ev/take channel)

Read from a channel, suspending the current fiber if no value is available.

Community Examples

ev/thread cfunction source

(ev/thread main &opt value flags supervisor)

Run main in a new operating system thread, optionally passing value to resume with. The parameter main can either be a fiber, or a function that accepts 0 or 1 arguments. Unlike ev/go, this function will suspend the current fiber until the thread is complete. If you want to run the thread without waiting for a result, pass the :n flag to return nil immediately. Otherwise, returns nil. Available flags:

  • :n - return immediately
  • :t - set the task-id of the new thread to value. The task-id is passed in messages to the supervisor channel.
  • :a - don't copy abstract registry to new thread (performance optimization)
  • :c - don't copy cfunction registry to new thread (performance optimization)
Community Examples

ev/thread-chan cfunction source

(ev/thread-chan &opt limit)

Create a threaded channel. A threaded channel is a channel that can be shared between threads and used to communicate between any number of operating system threads.

Community Examples

ev/to-file cfunction source

(ev/to-file)

Create core/file copy of the stream. This value can be used when blocking IO behavior is needed.

Community Examples

ev/with-deadline macro source

(ev/with-deadline sec & body)

Create a fiber to execute body, schedule the event loop to cancel the task (root fiber) associated with body's fiber, and start body's fiber by resuming it.

The event loop will try to cancel the root fiber if body's fiber has not completed after at least sec seconds.

sec is a number that can have a fractional part.

Community Examples

ev/with-lock macro source

(ev/with-lock lock & body)

Run a body of code after acquiring a lock. Will automatically release the lock when done.

Community Examples

ev/with-rlock macro source

(ev/with-rlock lock & body)

Run a body of code after acquiring read access to an rwlock. Will automatically release the lock when done.

Community Examples

ev/with-wlock macro source

(ev/with-wlock lock & body)

Run a body of code after acquiring write access to an rwlock. Will automatically release the lock when done.

Community Examples

ev/write cfunction source

(ev/write stream data &opt timeout)

Write data to a stream, suspending the current fiber until the write completes. Takes an optional timeout in seconds, after which will return nil. Returns nil, or raises an error if the write failed.

Community Examples