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

Question: Consider alternative #3

Open
MHDante opened this issue Dec 10, 2021 · 6 comments
Open

Question: Consider alternative #3

MHDante opened this issue Dec 10, 2021 · 6 comments

Comments

@MHDante
Copy link

MHDante commented Dec 10, 2021

Hey, I was quite fond of finding this package, however, It seems a bit brittle to modify the IL directly. It falls strictly into the realm of "dark magic" 🧙

I've achieved similar success by calling the dispatch method myself:

using System;
using System.Reflection;
using System.Threading;
using UnityEngine;

public static class WebGlThreadPoolPumper
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
    public static void StartPumping()
    {
        if (Application.platform != RuntimePlatform.WebGLPlayer || Application.isEditor) return;

        var dispatchMethod = Type
            .GetType("System.Threading.ThreadPoolWorkQueue")?
            .GetMethod("Dispatch", BindingFlags.NonPublic | BindingFlags.Static)?
            .CreateDelegate(typeof(Func<bool>)) as Func<bool>;

        Pump(dispatchMethod);
    }

    public static void Pump(object dispatchMethod)
    {
        var method = (Func<bool>)dispatchMethod;
        var didFinishWork = method();
        SynchronizationContext.Current.Post(Pump, dispatchMethod);
    }
}

However, this may not be as robust as your solution. In particular, I'm not sure I understand why the timer callback is necessary. (is it for time-sensitive callbacks?)

@VolodymyrBS
Copy link
Owner

Hi!
in fact, it's a great option but my intention was to mimic mono-wasm ThreadPool behaviour. It included updated code for GetMinThreads, GetMaxThreads, SetMinThreads, SetMaxThreads and GetAvailableThreads. Also, my wanted to rewrite System.Threading.Timer
but I have used your solution to run System.Threading.Timers in newer versions of Unity 😄

@VolodymyrBS
Copy link
Owner

Also, I'm thinking about commenting all "dark magic" methods with the example code that they generate and how they do it.

@olokobayusuf
Copy link

This is off-topic, but @VolodymyrBS emitting raw IL to patch mscorlib is a feat of engineering you rarely ever see. You, sir, are an artist of the highest abilities. I'm a huge fan 🤯 .

@xucian
Copy link

xucian commented Jul 26, 2022

This is off-topic, but @VolodymyrBS emitting raw IL to patch mscorlib is a feat of engineering you rarely ever see. You, sir, are an artist of the highest abilities. I'm a huge fan 🤯 .

My thoughts exactly. 😊 I really appreciate the effort!

@Bian-Sh
Copy link

Bian-Sh commented Nov 7, 2022

Also, I'm thinking about commenting all "dark magic" methods with the example code that they generate and how they do it.

any blog artic for us to learn this dark magic ,栓 q~

@StephenHodgson
Copy link

StephenHodgson commented Jun 3, 2023

I could not get this implementation to work for me with 2023+

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

No branches or pull requests

6 participants