multitheftauto / mtasa-blue

Multi Theft Auto is a game engine that incorporates an extendable network play element into a proprietary commercial single-player game.
https://multitheftauto.com
GNU General Public License v3.0
1.38k stars 424 forks source link

Add multi-threading support to Lua #918

Open MegadreamsBE opened 5 years ago

MegadreamsBE commented 5 years ago

Is your feature request related to a problem? Please describe.

While Lua is fast we are still bound to limitations that can make it hard to do some expensive calculations for things like path finding or dealing with running a SCM virtual machine (Sphene). It would be much nicer if this code could be moved into a separate thread and therefore prevent freezes.

Lua itself doesn't support multi-threading but is has been shown here that there is definitely a way to do so.

Describe the solution you'd like

I'd like for new functions to be exposed that allow for a thread to be created and for data to be transmitted between them. In line with the example of LÖVE I propose the following functions to be added at least:

Please note that this is an initial thought and that this API should be heavily discussed and adjusted where possible.

Describe alternatives you've considered

Using coroutines but you can only run one at once so it can slow down execution even more if improperly used. Coroutines work better for things like task scheduling.

qaisjp commented 5 years ago

I like love's approach with Channels. Very powerful imo

There was some discussion about this on #mta.dev (to be copied here)

MegadreamsBE commented 5 years ago

I agree, wasn't sure if it would have been easily accepted here so I went for a more simple API but figured it would come up if desired. Glad it did. Do you propose to keep a similar API for the channels as LÖVE or with modifications to it?

qaisjp commented 5 years ago

I would be happy with an identical or similar API.

sbx320 commented 5 years ago

I'd love to get createThread(function() return 3 end) working. Not quite sure how though. Would need some investigation.

MegadreamsBE commented 5 years ago

That would be ideal and from what I can read online possible but it's rather delicate to move a function to a different Lua state so it will require a lot of testing to make sure the garbage collector for a start doesn't get confused..

qaisjp commented 5 years ago

I'd love to get createThread(function() return 3 end) working. Not quite sure how though. Would need some investigation.

string.dump is usable in the simplest case

(upvalues would not be copied, to keep behaviour between createThread(filename) and createThread(fn) consistent ... but this means you can't call other "non-stdlib" functions)

jushar commented 4 years ago

I stumbled upon this project (https://github.com/effil/effil) today. Perhaps we can use that or at least consider it as an inspiration.

qaisjp commented 4 years ago

runner = effil.thread(func)

Creates thread runner. Runner spawns new thread for each invocation.

input: func - Lua function

@sbx320 I remember you were saying that you really wanted something like this where you could just pass a function

Aha:

Functions are dumped using lua_dump. Upvalues are captured according to the rules.