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

[core_bind] Add Thread::is_alive. Replace is_active with is_started to align with core/os/Thread API. #53455

Merged
merged 1 commit into from
Oct 6, 2021

Conversation

briansemrau
Copy link
Contributor

@briansemrau briansemrau commented Oct 6, 2021

This adds the function is_alive to Thread. This is a usability improvement allowing users to check if a thread is still doing work. This helps resolve confusion around is_active (renamed to is_started), which was reasonably expected to behave as is_alive.

The current workaround to this is to create a finished_executing flag for each thread, set to false before thread.start and set to true at the end of the threaded function.


If/when this gets approved, I'll open a branch for 3.x that doesn't break compatibility.

@briansemrau briansemrau requested review from a team as code owners October 6, 2021 05:00
@Chaosus Chaosus added this to the 4.0 milestone Oct 6, 2021
@akien-mga
Copy link
Member

I think if it's implemented in core_bind::Thread, it should also be implemented in Thread for consistency. CC @RandomShaper

@briansemrau
Copy link
Contributor Author

briansemrau commented Oct 6, 2021

I think if it's implemented in core_bind::Thread, it should also be implemented in Thread for consistency.

If you're referring to core/os/thread, then is_active doesn't exist there either. Regardless, if it's decided that they should be consistent then I can look into that.

Though, it appears that core/os Thread::is_started should behave the same as core_bind Thread::is_active, but they have different implementations. Not sure why it was done that way.

@akien-mga
Copy link
Member

Though, it appears that core/os Thread::is_started should behave the same as core_bind Thread::is_active, but they have different implementations. Not sure why it was done that way.

I think that was an oversight when core/os Thread was refactored in #45315. I ran into this (and other related core_bind discrepancies when trying to port a Godot module to the new GDExtension (which use the core_bind APIs for core classes): godotengine/godot-cpp#633

@RandomShaper
Copy link
Member

  • Bindings' Thread::is_active() could be reimplemented just as a call-through to C++'s Thread::is_started(). And it could also be renamed so both are called the same (probably is_started() would be the best; see below).
  • Regarding having an is_executing() method, a couple of thoughts:
    1. I'd like to suggest naming it is_alive(), to match what Java and Python do (that's why is_started() would be better than is_active(), so there's a bigger difference between both words).
    2. It would be good to stress in the documentation the difference between is_started() and is_alive(). The former corresponds to the typical usage pattern of starting a thread and joining when you know it will end. The latter would be for cases when that's not possible or convenient and you'd rather poll (for instance, every frame).

@briansemrau
Copy link
Contributor Author

briansemrau commented Oct 6, 2021

@RandomShaper Do you think implementing is_alive in core/os/Thread is desired, creating parity with core_bind Thread::is_alive? If so, core/os/Thread must be used with reference counting (inherit RefCounted if even possible, otherwise refactor all thread usage to use shared_ptr?). This is to prevent undefined behavior when resetting the is_alive flag (p_self->running.clear();) if the thread gets detached. Detaching the thread is incorrect usage and prints a warning, but I'd prefer not to create threaded, undefined behavior under any circumstance.

I'm heavily against doing this because of the complexity, but perhaps there's a simple solution I'm missing. Also, as far as I know, this doesn't currently have a use in core.

@briansemrau briansemrau changed the title Add is_executing to Thread [core_bind] Add is_alive to Thread. Replace is_active with is_started to align with core/os Thread API. Oct 6, 2021
…arted`.

Replacing `is_active` resolves an API discrepancy between core_bind Thread and core/os Thread.
@briansemrau briansemrau changed the title [core_bind] Add is_alive to Thread. Replace is_active with is_started to align with core/os Thread API. [core_bind] Add Thread::is_alive. Replace is_active with is_started to align with core/os/Thread API. Oct 6, 2021
@RandomShaper
Copy link
Member

I agree with you; probably too involved to be worthy when the C++ side has been good without it so far.

@RandomShaper
Copy link
Member

The only potential concern now is the exotic looking commit message, because of the square bracketed tag. Let's hear @akien-mga's opinion on that.

@akien-mga
Copy link
Member

The only potential concern now is the exotic looking commit message, because of the square bracketed tag.

Exotic indeed in our codebase, but we don't have a fixed convention and it does convey important information here (C++ core vs bindings) so I guess it's fine as is.

@akien-mga akien-mga merged commit 26f4848 into godotengine:master Oct 6, 2021
@akien-mga
Copy link
Member

Thanks!

@briansemrau
Copy link
Contributor Author

I'll put a little more consideration into commit names in the future 😅

@briansemrau briansemrau deleted the thread-is-executing branch October 25, 2021 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Thread remains in active state when function has finished
4 participants