class

Crystal::EventLoop

Inherits Crystal::EventLoop::Socket / Crystal::EventLoop::FileDescriptor / Reference / Object

Constructors

create(parallelism : Int32 = 1) : self

Creates an event loop instance.

The parallelism arg is informational. It reports how many schedulers are expected to register with the event loop instance. Because schedulers are dynamically started and execution contexts can be resized, more or less schedulers may really register in practice.

Source
current
Source

Class methods

backend_class
Source
current?
Source
default_file_blocking?
Source
default_socket_blocking?
Source
remove(file_descriptor : Crystal::System::FileDescriptor) : Nil

Removes the file descriptor from the event loop. Can be used to free up memory resources associated with the file descriptor, as well as removing the file descriptor from kernel data structures.

Called by ::IO::FileDescriptor#finalize before closing the file descriptor. Errors shall be silently ignored.

Source
remove(socket : ::Socket) : Nil

Removes the socket from the event loop. Can be used to free up memory resources associated with the socket, as well as removing the socket from kernel data structures.

Called by ::Socket#finalize before closing the socket. Errors shall be silently ignored.

Source

Instance methods

create_resume_event(fiber : Fiber) : Event

Create a new resume event for a fiber.

NOTE: optional.

Source
create_timeout_event(fiber : Fiber) : Event

Creates a timeout_event.

Source
drain

Blocks the current scheduler until all the pending events have completed. Must yield every runnable fiber.

Optional.

Source
interrupt

Tells a blocking run loop to no longer wait for events to activate. It may for example enqueue a NOOP event with an immediate (or past) timeout. Having activated an event, the loop shall return, allowing the blocked thread to continue.

Should be a NOOP when the loop isn't running or is running in a nonblocking mode.

NOTE: we assume that multiple threads won't run the event loop at the same time in parallel, but this assumption may change in the future!

Source
interrupt?

Same as #interrupt but returns true if a running event loop has likely been interrupted, and false otherwise.

Source
lock?

Tries to lock the event loop and yields if the lock was acquired. Must unlock before returning. Returns true if the lock was acquired, false otherwise.

Only needed when there should be a single scheduler running the event loop at any time (e.g. epoll, kqueue and IOCP). Can be a NOOP that always yields and returns true (io_uring).

Source
register(scheduler : Fiber::ExecutionContext::Scheduler, index : Int32) : Nil

Called once before scheduler is started. Optional hook.

Source
run(queue : Pointer(Fiber::List), blocking : Bool) : Nil

Same as #run but collects runnable fibers into queue instead of enqueueing in parallel, so the caller is responsible and in control for when and how the fibers will be enqueued.

Source
run(blocking : Bool) : Bool

Runs the loop.

Returns immediately if events are activable. Set blocking to false to return immediately if there are no activable events; set it to true to wait for activable events, which will block the current thread until then.

Returns true on normal returns (e.g. has activated events, has pending events but blocking was false) and false when there are no registered events.

Source
sleep(duration : Time::Span) : Nil

Suspend the current fiber for duration.

Source
unregister(scheduler : Fiber::ExecutionContext::Scheduler) : Nil

Called once before scheduler is shut down. Optional hook.

Source

Nested types