Reading PSI from other than UI thread
Hello,
As we know reading the PSI outside of a read lock throws the following exception.
java.lang.Throwable: Read access is allowed from event dispatch thread or inside
read-action only (see com.intellij.openapi.application.Application.runReadAction())
As per the documentation:
However, read operations performed from any other thread need to be wrapped in a read action by using
ApplicationManager.getApplication().runReadAction()
As I understand, before every read action, a lock is obtained and the lock is released after the action has been completed.
I'm trying to read the PSI from a number of custom threads. In my case, I know there won't be any Write actions while I try to read the PSI.
So, in my case, it seems the lock is unnecessary as I know for sure there won't be any write operations.
Is there any way/API where I can skip the redundant lock unlocks just to read the PSI? (Just to reduce the overhead added by locks and increase performance)
Please sign in to leave a comment.
I'm trying to read the PSI from a number of custom threads. In my case, I know there won't be any Write actions while I try to read the PSI.
Please give more details:
- What are "custom threads"? What is the use-case?
- How can you guarantee there won't be Write Action? User could interact with IDE anytime?
By custom threads, I meant other than the UI thread.
We are using the PyCharm plugin to build a code analysis engine on top of that which runs in headless mode. So, there is no IDE for users to interact with.
User interaction is just one possible case of Write Action starting. Any inspection that (wrongly) starts it would interfere.
More details:
We want to read the expressions in a method in control flow order and run abstract interpretation on that. What we are seeing is that if we want to access each expression and then process them, then we have to write a lot of code. On the other hand, if we access the entire method, process its expressions all inside one thread, then we will lose the parallel processing opportunity.
So, I was wondering, if there are any other ways to access the data in a read-only manner without the idiom specified in the original question.
Sorry for delay.
You can run multiple read actions simultaneously https://plugins.jetbrains.com/docs/intellij/general-threading-rules.html