Skip to content

Allow multiple Clang instances per thread.#46

Open
reitermarkus wants to merge 8 commits intoKyleMayes:masterfrom
reitermarkus:load-api
Open

Allow multiple Clang instances per thread.#46
reitermarkus wants to merge 8 commits intoKyleMayes:masterfrom
reitermarkus:load-api

Conversation

@reitermarkus
Copy link

@reitermarkus reitermarkus commented Sep 17, 2022

Depends on KyleMayes/clang-sys#145.

Previously, you could only create one Clang across all threads. This is a problem when trying to use Clang in tests which run in parallel. This change allows creating multiple Clang instances per thread while still only loading libclang once per thread.

@reitermarkus
Copy link
Author

@KyleMayes, I updated this to not use KyleMayes/clang-sys#145.

src/lib.rs Outdated
/// Constructs a new `Clang`.
///
/// Only one instance of `Clang` is allowed at a time.
/// Only one instance of `Clang` is allowed per thread.
Copy link
Author

@reitermarkus reitermarkus Nov 4, 2022

Choose a reason for hiding this comment

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

Note that I'm not actually sure if this true.

Depending on whether libclang is thread-safe or not, we either need to allow

1 Clang instance per thread where Clang: !Send + !Sync

or we need to allow

1 Clang instance per process where Clang: Send.

@reitermarkus
Copy link
Author

@KyleMayes, could you have another look here?

@reitermarkus
Copy link
Author

@nagisa, maybe you could also take a look here since this seems relevant for #26.

@nagisa
Copy link

nagisa commented Nov 14, 2022

FWIW #26 was so long ago, I pretty much forgotten all about what it was about. That said, it isn't clear to me some of the premises here are accurate:

This change allows creating multiple Clang instances per thread while still only loading libclang once per thread.

Dynamically loading a library is still a process-wide reference-counted affair (this is just how dynamic loaders work). This cannot be changed unless some non-portable dl* APIs are used. It just that constructing a structure containing function pointers to all the symbols is gonna happen per-thread which seems like a wasted effort, given that the code could just have a static CLANG: Once<...>.

@KyleMayes
Copy link
Owner

KyleMayes commented Nov 14, 2022 via email

@reitermarkus
Copy link
Author

It just that constructing a structure containing function pointers to all the symbols is gonna happen per-thread which seems like a wasted effort, given that the code could just have a static CLANG: Once<...>.

That's exactly the problem: Clang is not Send, so you cannot have a static containing it.

Currently, we can only have a single instance per-process, which cannot be shared, so we need one of the two options described in #46 (comment).

@RobbeDGreef
Copy link

Are there any updates on this? I am encountering the same problem.

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.

4 participants