Reading PSI from other than UI thread

Answered

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)

5 comments
Comment actions Permalink

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?

0
Comment actions Permalink

What are "custom threads"? What is the use-case?

By custom threads, I meant other than the UI thread.

How can you guarantee there won't be Write Action? User could interact with IDE anytime?

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.

0
Comment actions Permalink

User interaction is just one possible case of Write Action starting. Any inspection that (wrongly) starts it would interfere.

0
Comment actions Permalink

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.

0
Comment actions Permalink

Sorry for delay.

You can run multiple read actions simultaneously https://plugins.jetbrains.com/docs/intellij/general-threading-rules.html

0

Please sign in to leave a comment.