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

platform: pump_message_loop, run_idle_tasks, GLOBAL_PLATFORM #706

Merged
merged 3 commits into from
Jul 1, 2021
Merged

platform: pump_message_loop, run_idle_tasks, GLOBAL_PLATFORM #706

merged 3 commits into from
Jul 1, 2021

Conversation

inteon
Copy link
Contributor

@inteon inteon commented Jun 20, 2021

Added pump_message_loop and run_idle_tasks to v8::Platform.
The GLOBAL_PLATFORM now manages the active platform.

@bartlomieju
Copy link
Member

@inteon very nice! The code looks mostly good to me, however a segfault happens in test_single_threaded_default_platform test. It's an already existing test so my hunch is that some default values have changed in this PR.

@bartlomieju bartlomieju requested a review from bnoordhuis June 23, 2021 11:06
src/platform.rs Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
@inteon inteon marked this pull request as ready for review June 23, 2021 12:42
src/V8.rs Outdated Show resolved Hide resolved
@piscisaureus piscisaureus self-assigned this Jun 23, 2021
Copy link
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request, @inteon.

I like the general thrust but I have mixed feelings about bypassing V8's public API and using the internal DefaultPlatform directly.

I assume you're doing that so you can use placement new but is that really necessary? Is the current UniquePtr<Platform> approach not workable?

src/V8.rs Outdated Show resolved Hide resolved
examples/hello_world.rs Outdated Show resolved Hide resolved
src/binding.cc Outdated Show resolved Hide resolved
src/V8.rs Outdated Show resolved Hide resolved
@inteon
Copy link
Contributor Author

inteon commented Jun 25, 2021

@bnoordhuis @piscisaureus @bartlomieju The main challenge I encountered implementing this PR, is the pump_message_loop function that needs a pointer to platform as a function argument. This means that unique ownership (like UniquePtr<Platform>) cannot be used, because the call to initialize_platform would transfer ownership, and the platform pointer is still needed for the pump_message_loop call.

For that reason, I'm using Pin<Arc<Platform>>. This however means that I have to allocate the heap space in rust and do a v8 in-place construction of the Platform object. This also means that the logic for the number of threads in the thread pool is skipped. Therefore this logic has to be implemented in rust (if thread_pool_size = 0 for call to new_default_platform instead the number of cpu cores - 1 should be used as the thread_pool_size; this is the logic from v8).

@piscisaureus I think the reference counter for the Arc increases when calling get_current_platform or when cloning the Arc. I could be wrong, but I think that the reference count will exceed 1, right?

@bnoordhuis
Copy link
Contributor

@inteon Would SharedPtr work? They're immutable shared references but I'd say it's okay to bend the rules a little behind the scenes and convert to a mutable pointer at the Rust/C++ edge.

@inteon
Copy link
Contributor Author

inteon commented Jun 29, 2021

@inteon Would SharedPtr work? They're immutable shared references but I'd say it's okay to bend the rules a little behind the scenes and convert to a mutable pointer at the Rust/C++ edge.

For a SharedPtr to work, I think you have to implement the Shared trait, and that requires a shared pointer to be kept on the cpp side. I previously started implementing that solution, but I thought it wouldn't be a simpler/ more elegant solution.

I'll retry that solution, because ofc this solution also adds a lot of challenges.

@inteon
Copy link
Contributor Author

inteon commented Jun 29, 2021

@inteon Would SharedPtr work? They're immutable shared references but I'd say it's okay to bend the rules a little behind the scenes and convert to a mutable pointer at the Rust/C++ edge.

Just finished creating a solution based on SharedPtr 🥳 . It requires a bit more code, but requires less hacks.

@bartlomieju
Copy link
Member

@inteon nice work! This looks much better to me. I'll defer to review from @bnoordhuis and @piscisaureus

@piscisaureus
Copy link
Member

LGTM
That said, I'm still a bit nervous about it - @bnoordhuis can I get your sign off as well?

Copy link
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @inteon. Ideally, the test wouldn't use natives syntax (not stable across V8 releases) but if this is the only reliable way to test then so be it.

src/V8.rs Outdated Show resolved Hide resolved
src/platform.rs Outdated Show resolved Hide resolved
@bartlomieju bartlomieju merged commit 14bcf04 into denoland:main Jul 1, 2021
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

Successfully merging this pull request may close these issues.

5 participants