Skip to content

WorkerThreadPool expose add_template_group_task and add_template_task #1368

Open

Description

@ozzr

Godot version

master

godot-cpp version

master

System information

Windows 10

Issue description

Allow using add_template_group_task and add_template_task methods from WorkerThreadPool class that are available inside modules but in extensions.

Steps to reproduce

look at godot engine source code usage of the mentioned methods

Minimal reproduction project

create a templated method

Activity

  1. dsnopek commented on Apr 9, 2024

    @dsnopek
    Collaborator

    Thanks!

    Now that we have callable_mp(), you could simulate add_template_group_task(). So, rather than doing:

    WorkerThreadPool::get_singleton()->add_template_group_task(this, &MyClass::method, data, ...);
    

    ... you can do:

    WorkerThreadPool::get_singleton()->add_group_task(callable_mp(this, &MyClass::method).bind(data), ...);
    

    I think that code should work in a module as well!

    That said, for compatibility, we could probably generate an add_template_group_task() that would do the callable_mp() thing for you.

  2. added
    enhancementThis is an enhancement on the current functionality
    on Apr 9, 2024
  3. added this to the 4.x milestone on Apr 9, 2024
  4. ozzr commented on Apr 10, 2024

    @ozzr
    Author

    Hi, as long as I know Callables only bind Variant arguments, plus maybe it is intended, but inside modules I havent been able of binding any PackedVector as an argument to a callable. So I started using the add_template_group_task method with pointers to arrays.
    This is a method taken from my landscape system under development:

    // Inside the header
    #ifdef GDEXTENSION
    // Workaround Method
    	Vector<Vector2i>              _temp_save_region_keys;
    	void                          _thread_save_regions(uint32_t index);
    #else
    //Original Method
    	void                          _thread_save_regions(uint32_t index,const Vector2i* pool);
    #endif // GDEXTENSION

    Also

    WorkerThreadPool::get_singleton()->add_template_group_task allows passing data using custom structs and there are diferences in the way each WorkerThreadPool::get_singleton()->add_XXXX_task method schedule the task. Just by looking at the source code I can see this:

    // method add_template_group_task
    _add_group_task(Callable(), nullptr, nullptr, ud, p_elements, p_tasks, p_high_priority, p_description);
    
    // method add_native_group_task
    _add_group_task(Callable(), p_func, p_userdata, nullptr, p_elements, p_tasks, p_high_priority, p_description);
    
    // method add_group_task
    _add_group_task(p_action, nullptr, nullptr, nullptr, p_elements, p_tasks, p_high_priority, p_description);
    

    So, in summary, using add_template_group_task is good for passing custom structs, and using a Callable for mimiking the behaviour will most likely not work

    as a plus.... I have read a big chunk of godot source code by now and I have not find any internal use of a Callable binding a PackedVector, instead, for cases where the use of a Vector or PackedVector is necesary, the templated_task method is the one used

  5. dsnopek commented on Apr 11, 2024

    @dsnopek
    Collaborator

    It's not ideal, but you can store pointers as uint64_t as a way to put them into a Variant. This strategy is used in a bunch of places in the XR APIs exposed to GDExtension.

  6. ozzr commented on Apr 11, 2024

    @ozzr
    Author

    Interesting, I will take a look at it, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis is an enhancement on the current functionality

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions