Recursive AI

Waking the event loop from another thread

All oneplatform docs

PlatformApp.PumpEvents(timeout) blocks until the system has an event or the timeout passes. Work that arrives on another thread (a request from a control listener, a D-Bus callback, a timer, a task continuation) calls PlatformApp.Wake() to end that wait at once, so the loop needs no polling interval.

  • Wake() is safe from any thread, any number of times, at any time, including during Dispose.
  • It makes the current wait return; if no wait is running, the next one returns immediately. Wakes that pile up before the loop looks coalesce into one early return.
  • A woken PumpEvents queues no event. After every return, check your own work queue.
  • A wake is not an event, so there is no PlatformEventKind for it.
BackendMechanism
Wayland, X11A non-blocking pipe polled beside the connection descriptor. A flag keeps a burst of wakes to one byte.
HeadlessA timed pump sleeps on a semaphore while nothing is queued; Wake releases it.
Win32An auto-reset event passed to MsgWaitForMultipleObjects beside the message queue.
CocoaAn application-defined NSEvent posted with postEvent:atStart:, which may be called from any thread.
AndroidALooper_wake on the loop's looper.
Browser, iOSNo blocking wait; Wake() does nothing.

Headless PumpEvents(timeout) used to return at once; it now waits up to timeout when no event is queued and no wake arrives.