Search in sources :

Example 1 with BlazeAndroidModel

use of com.google.idea.blaze.android.sync.model.idea.BlazeAndroidModel in project intellij by bazelbuild.

the class BlazeAndroidProjectStructureSyncer method updateModuleFacetInMemoryState.

/**
 * @param resourceRepositoryExecutor a single thread executor to execute resource repository
 *     creation in the background, off the EDT, and in smart mode. This prevents hanging if we let
 *     it happen later on the EDT, and deadlocks if we let it happen during dumb mode.
 *     <p>{@link ModuleResourceRepository#getOrCreateInstance} and {@link
 *     ModuleResourceRepository#getItems} grabs {@link MultiResourceRepository#ITEM_MAP_LOCK} and
 *     the read lock in reverse order, so if the following scenario occurs:
 *     <ol>
 *       <li>Thread A enters getItems and grabs the ITEM_MAP_LOCK
 *       <li>Thread B enters getOrCreateInstance and grabs the read lock
 *       <li>The EDT tries run a write action, but gets blocked by Thread B
 *       <li>Thread B tries to grab the ITEM_MAP_LOCK but gets blocked by Thread A
 *       <li>Thread A tries to grab the read lock, but gets blocked by the EDT
 *     </ol>
 *     we will get a 3-way deadlock. To prevent this, we use a single thread executor, so
 *     getOrCreateInstance and getItems cannot happen concurrently.
 */
private static void updateModuleFacetInMemoryState(Project project, AndroidSdkPlatform androidSdkPlatform, Module module, File moduleDirectory, File manifest, String resourceJavaPackage, Collection<File> resources, @Nullable Executor resourceRepositoryExecutor) {
    SourceProvider sourceProvider = new SourceProviderImpl(module.getName(), manifest, resources);
    BlazeAndroidModel androidModel = new BlazeAndroidModel(project, module, moduleDirectory, sourceProvider, manifest, resourceJavaPackage, androidSdkPlatform.androidMinSdkLevel);
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet != null) {
        facet.setAndroidModel(androidModel);
        if (resourceRepositoryExecutor != null) {
            // Create resource repository and table early so it doesn't hold up the EDT later.
            resourceRepositoryExecutor.execute(() -> {
                DumbService.getInstance(project).waitForSmartMode();
                ModuleResourceRepository.getOrCreateInstance(facet).getItems();
            });
        }
    }
}
Also used : BlazeAndroidModel(com.google.idea.blaze.android.sync.model.idea.BlazeAndroidModel) SourceProvider(com.android.builder.model.SourceProvider) SourceProviderImpl(com.google.idea.blaze.android.sync.model.idea.SourceProviderImpl) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Aggregations

SourceProvider (com.android.builder.model.SourceProvider)1 BlazeAndroidModel (com.google.idea.blaze.android.sync.model.idea.BlazeAndroidModel)1 SourceProviderImpl (com.google.idea.blaze.android.sync.model.idea.SourceProviderImpl)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1