New coroutine based thread system.

Hi All,

I’ve been working on a new thread system with an aim to simplify some of the callbacks used for many ObjC APIs.
On the backend it resumes each thread automatically once every frame. This allows for some pretty neat threading tricks including Semaphores & Promises (which run on top of semaphores) which allow for some super simple asynchronous → synchronous conversions.

I’m just trying to gauge interest, gather suggestions and get some feedback on the API really.

If you have a similar use case that you’ve not been able to find a solution for so far let me know and I may be able to incorporate it.

Here’s how some sample usage looks in its current state:

function getResources()
    local p = Promise()
    -- Run this on the main lua thread so the callback actually works
    runMain(http.request, "", function(data)
    -- Wait for the data to be received
    return p:get()

-- User Thread
    -- Download the resources
    print("Downloading resources")
    -- Only start drawing when we have the resources
    while true do
        background(180, 40, 90)
        -- Frame complete so do a backbuffer swap

^ It’s worth noting that this can run standalone in the ‘Main’ tab with no setup as long as the backend is a dependency.

Thanks all

Very interesting! promises would be particularly cool. I notice there are quite a few implementations for Lua promises. Are you following any of those?

@RonJeffries Ah, no. It hadn’t actually crossed my mind to look at pre-existing lua implementations.
Though having said that, I’m not sure my ‘Promises’ work as promises in other languages do just yet. The way I’m using them is to literally wait for a value to become available before continuing execution which greatly simplifies turning an async function into a synchronous one.

Yes … I suspect we can’t really do full-on promises, since I think they require true threads. We can do cooperative ones via coroutines, as I believe you’re doing.