#platform-independent #run #system #loops #consistent #interface #thread

irondash_run_loop

Consistent, platform-independent interface to system run loop

8 releases (4 breaking)

0.5.0 Dec 26, 2023
0.4.0-dev.2 Nov 29, 2023
0.3.0 Jul 28, 2023
0.2.0 Jul 1, 2023
0.1.0 Nov 15, 2022

#315 in Asynchronous

Download history 538/week @ 2023-12-23 463/week @ 2023-12-30 472/week @ 2024-01-06 401/week @ 2024-01-13 515/week @ 2024-01-20 715/week @ 2024-01-27 1027/week @ 2024-02-03 705/week @ 2024-02-10 1532/week @ 2024-02-17 1096/week @ 2024-02-24 1170/week @ 2024-03-02 989/week @ 2024-03-09 898/week @ 2024-03-16 1227/week @ 2024-03-23 1135/week @ 2024-03-30 1320/week @ 2024-04-06

4,705 downloads per month
Used in 2 crates

MIT license

135KB
3.5K SLoC

irondash_run_loop

This crate provides a consistent, platform independent interface to system run loop.

Getting RunLoop for current thread

let run_loop = RunLoop::current();

If there is no run loop associated with current thread, this will create one. RunLoop is backed by platform specific implementation:

  • CFRunLoop on iOS nad macOS
  • ALooper on Android
  • GMainContext on Linux
  • HWND message loop on Windows

Calling RunLoop from other threads

RunLoop is neither Send, nor Sync. All interaction with it must be carried on thread where the run loop belongs.

To interact with run loop from other threads, use RunLoopSender:

let run_loop = RunLoop::current();
let sender = run_loop.new_sender();

// sender is Sync, Send and Clone
thread::spawn(move||{
    println("Hello from other thread!");
    sender.send(||{
        println!("Back on RunLoop thread");
    });
});

At any point, without needing a RunLoop instance, you can request sender that sends the closure to main thread. For this to work on all platforms your Dart application must depend on the irondash_engine_context plugin.

thread::spawn(move||{
    let sender = RunLoop::sender_for_main_thread().unwrap();
    sender.send(||{
        println!("Back on main thread");
        // run_loop is main thread run loop
        let run_loop = RunLoop::current();
    });
});

Depending on irondash_engine_context plugin is necessary because the Rust code may be part of FFI plugin that gets loaded from UI thread or other background isolate, and on some platforms it is not possible to jump back to main thread without having some preparation done on main thread first (which is facilitated by the native code part of irondash_engine_context plugin).

If you want to use RunLoop without irondash_engine_context plugin, you can call RunLoop::set_main_thread() on the main thread as the very first method on the RunLoop.

Scheduling timers

RunLoop can also be used to schedule delayed execution of closures:

let run_loop = RunLoop::current();
let handle = run_loop.schedule(Duration::from_secs(10), || {
    println!("This will be printed after 10 seconds");
});

RunLoop::schedule returns a Handle instance. If handle is dropped before timer executes, timer will be cancelled. If you don't want that, call detach on the handle:

let run_loop = RunLoop::current();
self.run_loop(Duration::from_secs(10), || {
    println!("This will be printed after 10 seconds");
}).detach();

You can also call handle.cancel() to cancel the timer without dropping the handle.

Timers do not repeat. Every scheduled timer will be executed at most once.

Async support

RunLoop can be used as future executor:

RunLoop::current().spawn(async move ||{
    RunLoop::current().wait(Duration::from_secs(10)).await;
    println("After 10 second delay");
});

// or use crate::spawn variant:

spawn(async move ||{
    RunLoop::current().wait(Duration::from_secs(10)).await;
    println("After 10 second delay");
});

Because futures are executed on single thread to which the RunLoop belongs, they do not need to be Send.

What exactly is main tread?

This slightly varies per platform.

  • On iOS and macOS, it is the very first thread created when application is launched. It is the thread for which pthread_main_np() returns 1.
  • On Linux, for the purpose of RunLoop, main thread is the very first thread similar to iOS and macOS.
  • On Android, there is a concept of main thread (i.e. Looper.getMainLooper()).
  • On Windows, main thread is the first thread created when application was launched, similar to macOS and iOS. If you create windows and pump the message loop on different thread, RunLoop::sender_for_main_thread() will not work as expected.

Dependencies

~0.9–15MB
~143K SLoC