Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MoarVM Support of Callbacks in a Different Thread #96

Open
Xliff opened this issue Aug 28, 2019 · 8 comments
Open

MoarVM Support of Callbacks in a Different Thread #96

Xliff opened this issue Aug 28, 2019 · 8 comments
Labels
rakudo Big changes to Rakudo

Comments

@Xliff
Copy link

Xliff commented Aug 28, 2019


I am currently working on NaitveCall bindings to a GLib-based multi-media library. However, when I attempted to add a Perl6 "callback" via the GLib signaling mechanism, I get the following error:

MoarVM panic: native callback ran on thread (140407967450880) unknown to MoarVM

As I understand it, this is due to a limitation where MoarVM will not handle being called from a thread created by the C library.

Are there any plans to add such a capability to MoarVM? Can the technical issues involved be explained? Would anyone have any ideas as to a workaround?

@Xliff Xliff added the rakudo Big changes to Rakudo label Aug 28, 2019
@Xliff
Copy link
Author

Xliff commented Aug 28, 2019

For those interested, here is a backtrace that illustrates the issue.

@jnthn
Copy link
Contributor

jnthn commented Aug 28, 2019

It should be possible, just needs somebody with the time to do it. The place where the error is given should be a good point to implement dealing with it in a more useful way.

One would need to react to such a situation by creating the required MoarVM ThreadContext data structure, making sure it's added into the threads list, etc. That's probably not so hard, but will likely need a bit of a refactoring of the thread creation code to make it possible (without a bunch of undesirable copy-paste). One would also need to make sure to mark the thread blocked for GC when returning from the callback (that may fall out of the code that's already there, but it should be checked).

Making sure the thread is marked as application lifetime at a MoarVM level means it won't try and join it at shutdown, which reasonably represents "we don't control its lifetime", but there's another issue: it means that anything creating numerous short-lived threads and doing callbacks with them will leak memory over time. Provided the C code is just re-using the same threads, it'll be fine, though, and this could be treated as a separate follow-up problem (or documented as an implementation restriction). I don't know exactly how we could resolve it; we'd need to get a callback when the thread goes away, or perhaps poll for its existence.

@Xliff
Copy link
Author

Xliff commented Aug 28, 2019

@jnthn: Thanks for the details! Would something like this be a possible addition for 6.e?

@vrurg
Copy link
Contributor

vrurg commented Aug 28, 2019

@Xliff This is not really a matter of language specification because neither of language behaviours are changing. Just a technical implementation which could happen any time when its ready.

@jnthn
Copy link
Contributor

jnthn commented Aug 29, 2019

Indeed, NativeCall isn't actually part of the language spec. This can happen as soon as somebody has the time/patience to make it happen.

For @Xliff or anyone who wants to help this happen sooner, but doesn't feel like wading into MoarVM internals: a PR to Rakudo that adds a todo'd NativeCall test that should pass if this is addressed would be great.

@Xliff
Copy link
Author

Xliff commented Aug 29, 2019

@jnthn: Thanks for the suggestion. Would this be something to add to roast?

@salortiz
Copy link

@Xliff: No, Rakudo has it's own tests in https://github.com/rakudo/rakudo/tree/master/t/04-nativecall

Xliff added a commit to Xliff/p6-GtkPlus that referenced this issue Oct 4, 2019
- Fix pointer role reference in GIO::Task

- Completes (for now) work on t/gio/03-cancellable.t -- NO TESTS RUN, 
see Raku/problem-solving#96
@lizmat lizmat unassigned jnthn Oct 5, 2020
@Xliff
Copy link
Author

Xliff commented Jun 1, 2022

Hello,

Just reporting that I have encountered this issue, again.

Over the past 3 years, I've run into this in the following projects:

GtkPlus
GStreamer

and now:

FluidSynth.

I have bindings for all of these projects. Now Fluidsynth in particular is of a heavily multi-threaded nature, and my bindings cannot unlock their full potential in Rakudo without this issue being looked into.

I would do it myself, but I have utterly no experience writing multi-threaded code in C. If I had some help, I would gladly take a stab at it.

So consider this a call forf help.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rakudo Big changes to Rakudo
Projects
None yet
Development

No branches or pull requests

4 participants