Giter Site home page Giter Site logo

async-wormhole's Introduction

async-wormhole

Documentation

This library is still super experimental, I use it to prototype the foundation for Lunatic .

Currently only works in Rust nightly, as it depends on switcheroo.

async-wormhole allows you to .await async calls in non-async functions, like extern "C" or JIT generated code. It runs on Windows, MacOs and Linux (x64 & AArch64).

Motivation

Sometimes, when running inside an async environment you need to call into JIT generated code (e.g. wasm) and .await from there. Because the JIT code is not available at compile time, the Rust compiler can't do their "create a state machine" magic. In the end you can't have .await statements in non-async functions.

This library creates a special stack for executing the JIT code, so it's possible to suspend it at any point of the execution. Once you pass it a closure inside AsyncWormhole::new you will get back a future that you can .await on. The passed in closure is going to be executed on a new stack.

Sometimes you also need to preserve thread local storage as the code inside the closure expects it to stay the same, but the actual execution can be moved between threads. There is a proof of concept API to allow you to move your thread local storage with the closure across threads.

Example

use async_wormhole::{AsyncWormhole, AsyncYielder};
use switcheroo::stack::*;

// non-async function
extern "C" fn non_async(mut yielder: AsyncYielder<u32>) -> u32 {
	// Suspend the runtime until async value is ready.
	// Can contain .await calls.
    yielder.async_suspend(async { 42 })
}

fn main() {
    let stack = EightMbStack::new().unwrap();
    let task = AsyncWormhole::<_, _, ()>::new(stack, |yielder| {
        let result = non_async(yielder);
        assert_eq!(result, 42);
        64
    })
    .unwrap();

    let outside = futures::executor::block_on(task);
    assert_eq!(outside.unwrap(), 64);

Performance

There should be almost no performance overhead to .await calls inside the closure passed to AsyncWormhole::new and caught by async_suspend. But instantiating a new AsyncWormhole will require one memory allocation. And of course you are not going to get perfectly sized stacks.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

async-wormhole's People

Contributors

bkolobara avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.