Crystal::EventLoop
Inherits Crystal::EventLoop::Socket / Crystal::EventLoop::FileDescriptor / Reference / Object
Constructors
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.
Class methods
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.
Instance methods
Create a new resume event for a fiber.
NOTE: optional.
Blocks the current scheduler until all the pending events have completed. Must yield every runnable fiber.
Optional.
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!
Same as #interrupt but returns true if a running event loop has likely
been interrupted, and false otherwise.
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).
Called once before scheduler is started. Optional hook.
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.
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.
Called once before scheduler is shut down. Optional hook.