sigildocs

(sigil async)

(sigil async) - Cooperative Async Runtime

Provides a cooperative multitasking runtime with lightweight tasks, async I/O, and timer support. Tasks yield control explicitly via blocking operations (sleep, I/O waits, channel operations).

Basic Usage

(import (sigil async))

(with-async
  (go (begin
        (display "Task A\n")
        (sleep 0.1)
        (display "Task A done\n")))
  (go (begin
        (display "Task B\n")
        (sleep 0.05)
        (display "Task B done\n"))))

I/O Integration

(with-async
  (go (begin
        (await-readable sock)
        (display (socket-read sock)))))

Exports

schedulerprocedure

Scheduler record type.

The scheduler manages concurrent tasks with support for I/O and timer-based waiting.

scheduler?procedure

Test if a value is a scheduler struct.

Get the run-queue field of a scheduler struct.

Get the blocked-count field of a scheduler struct.

Get the io-waiters field of a scheduler struct.

Get the timer-waiters field of a scheduler struct.

Set the run-queue field of a scheduler struct.

Set the blocked-count field of a scheduler struct.

Set the io-waiters field of a scheduler struct.

Set the timer-waiters field of a scheduler struct.

Create a new scheduler.

Get the current scheduler.

Returns #f if not running inside with-async.

Add a new task to the scheduler.

scheduler-runprocedure

Run all scheduler tasks until completion or deadlock.

Run async code in a scheduler context.

Creates a scheduler and runs the body until all tasks complete. Use go within the body to spawn concurrent tasks.

(with-async
  (go (task-1))
  (go (task-2)))
gosyntax

Spawn a concurrent task.

Must be called within with-async. The task runs concurrently with other tasks, yielding at blocking operations.

(with-async
  (go (display "hello\n")))
yieldprocedure

Yield control to the scheduler.

Allows other tasks to run before continuing.

sleepprocedure

Sleep for a specified number of seconds.

Yields to the scheduler, allowing other tasks to run. Other tasks continue during the sleep.

(with-async
  (go (begin
        (display "start\n")
        (sleep 1)
        (display "done\n"))))

Wait until a socket is readable.

Yields to the scheduler until the socket has data available.

Wait until a socket is writable.

Yields to the scheduler until the socket can accept data.