Search in sources :

Example 6 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class MavenProjectsProcessor method waitForCompletion.

public void waitForCompletion() {
    if (isStopped)
        return;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        synchronized (myQueue) {
            while (!myQueue.isEmpty()) {
                startProcessing(myQueue.poll());
            }
        }
        return;
    }
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    scheduleTask(new MavenProjectsProcessorTask() {

        public void perform(Project project, MavenEmbeddersManager embeddersManager, MavenConsole console, MavenProgressIndicator indicator) throws MavenProcessCanceledException {
            semaphore.up();
        }
    });
    while (true) {
        if (isStopped || semaphore.waitFor(1000))
            return;
    }
}
Also used : Project(com.intellij.openapi.project.Project) Semaphore(com.intellij.util.concurrency.Semaphore) SoutMavenConsole(org.jetbrains.idea.maven.execution.SoutMavenConsole)

Example 7 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class MavenUtil method smartInvokeAndWait.

public static void smartInvokeAndWait(final Project p, final ModalityState state, final Runnable r) {
    if (isNoBackgroundMode() || ApplicationManager.getApplication().isDispatchThread()) {
        r.run();
    } else {
        final Semaphore semaphore = new Semaphore();
        semaphore.down();
        DumbService.getInstance(p).smartInvokeLater(() -> {
            try {
                r.run();
            } finally {
                semaphore.up();
            }
        }, state);
        semaphore.waitFor();
    }
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore)

Example 8 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class SvnBusyOnAddTest method testStatusDoesNotLockForWrite.

public void testStatusDoesNotLockForWrite() throws Exception {
    final File ioFile = new File(myWorkingCopyRoot, filename);
    ioFile.getParentFile().mkdirs();
    /*SVNWCClient client11 = new SVNWCClient((ISVNRepositoryPool)null, new DefaultSVNOptions());
    client11.doAdd(ioFile.getParentFile(), true, false, true, true);*/
    ioFile.createNewFile();
    try {
        final SVNStatusClient readClient = new SVNStatusClient((ISVNRepositoryPool) null, new DefaultSVNOptions());
        final Semaphore semaphore = new Semaphore();
        final Semaphore semaphoreMain = new Semaphore();
        final Semaphore semaphoreWokeUp = new Semaphore();
        final AtomicReference<Boolean> wasUp = new AtomicReference<>(false);
        final ISVNStatusHandler handler = status -> {
            semaphore.waitFor();
            wasUp.set(true);
        };
        semaphore.down();
        semaphoreMain.down();
        semaphoreWokeUp.down();
        final SVNException[] exception = new SVNException[1];
        Thread thread = new Thread(() -> {
            try {
                semaphoreMain.up();
                readClient.doStatus(myWorkingCopyRoot, true, false, true, false, handler);
                semaphoreWokeUp.up();
            } catch (SVNException e) {
                exception[0] = e;
            }
        }, "svn test");
        thread.start();
        semaphoreMain.waitFor();
        TimeoutUtil.sleep(5);
        SVNWCClient client = new SVNWCClient((ISVNRepositoryPool) null, new DefaultSVNOptions());
        client.doAdd(ioFile.getParentFile(), true, false, true, true);
        semaphore.up();
        semaphoreWokeUp.waitFor();
        Assert.assertEquals(true, wasUp.get().booleanValue());
        if (exception[0] != null) {
            throw exception[0];
        }
        thread.join();
    } finally {
        ioFile.delete();
    }
}
Also used : SVNCancelException(org.tmatesoft.svn.core.SVNCancelException) PluginPathManager(com.intellij.openapi.application.PluginPathManager) SVNException(org.tmatesoft.svn.core.SVNException) PathManager(com.intellij.openapi.application.PathManager) Test(org.junit.Test) SVNErrorCode(org.tmatesoft.svn.core.SVNErrorCode) ISVNWCDb(org.tmatesoft.svn.core.internal.wc17.db.ISVNWCDb) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) SVNWCContext(org.tmatesoft.svn.core.internal.wc17.SVNWCContext) org.tmatesoft.svn.core.wc(org.tmatesoft.svn.core.wc) Nullable(org.jetbrains.annotations.Nullable) Assert(junit.framework.Assert) TimeoutUtil(com.intellij.util.TimeoutUtil) Semaphore(com.intellij.util.concurrency.Semaphore) TestCase(junit.framework.TestCase) DefaultSVNOptions(org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions) SVNWCDb(org.tmatesoft.svn.core.internal.wc17.db.SVNWCDb) Before(org.junit.Before) DefaultSVNOptions(org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(com.intellij.util.concurrency.Semaphore) SVNException(org.tmatesoft.svn.core.SVNException) File(java.io.File)

Example 9 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class DefaultCodeFragmentFactory method createCodeFragment.

public JavaCodeFragment createCodeFragment(TextWithImports item, PsiElement context, final Project project) {
    final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
    final String text = item.getText();
    final JavaCodeFragment fragment;
    if (CodeFragmentKind.EXPRESSION == item.getKind()) {
        final String expressionText = StringUtil.endsWithChar(text, ';') ? text.substring(0, text.length() - 1) : text;
        fragment = factory.createExpressionCodeFragment(expressionText, context, null, true);
    } else /*if (CodeFragmentKind.CODE_BLOCK == item.getKind())*/
    {
        fragment = factory.createCodeBlockCodeFragment(text, context, true);
    }
    if (item.getImports().length() > 0) {
        fragment.addImportsFromString(item.getImports());
    }
    fragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE);
    //noinspection HardCodedStringLiteral
    fragment.putUserData(KEY, "DebuggerComboBoxEditor.IS_DEBUGGER_EDITOR");
    fragment.putCopyableUserData(JavaCompletionUtil.DYNAMIC_TYPE_EVALUATOR, (expression, parameters) -> {
        if (!RuntimeTypeEvaluator.isSubtypeable(expression)) {
            return null;
        }
        if (parameters.getInvocationCount() <= 1 && JavaCompletionUtil.mayHaveSideEffects(expression)) {
            final CompletionService service = CompletionService.getCompletionService();
            if (parameters.getInvocationCount() < 2) {
                service.setAdvertisementText("Invoke completion once more to see runtime type variants");
            }
            return null;
        }
        final DebuggerContextImpl debuggerContext = DebuggerManagerEx.getInstanceEx(project).getContext();
        DebuggerSession debuggerSession = debuggerContext.getDebuggerSession();
        if (debuggerSession != null && debuggerContext.getSuspendContext() != null) {
            final Semaphore semaphore = new Semaphore();
            semaphore.down();
            final AtomicReference<PsiType> nameRef = new AtomicReference<>();
            final RuntimeTypeEvaluator worker = new RuntimeTypeEvaluator(null, expression, debuggerContext, ProgressManager.getInstance().getProgressIndicator()) {

                @Override
                protected void typeCalculationFinished(@Nullable PsiType type) {
                    nameRef.set(type);
                    semaphore.up();
                }
            };
            debuggerSession.getProcess().getManagerThread().invoke(worker);
            for (int i = 0; i < 50; i++) {
                ProgressManager.checkCanceled();
                if (semaphore.waitFor(20))
                    break;
            }
            return nameRef.get();
        }
        return null;
    });
    return fragment;
}
Also used : RuntimeTypeEvaluator(com.intellij.debugger.codeinsight.RuntimeTypeEvaluator) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(com.intellij.util.concurrency.Semaphore) DebuggerSession(com.intellij.debugger.impl.DebuggerSession) CompletionService(com.intellij.codeInsight.completion.CompletionService) DebuggerContextImpl(com.intellij.debugger.impl.DebuggerContextImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with Semaphore

use of com.intellij.util.concurrency.Semaphore in project intellij-community by JetBrains.

the class DebugProcessImpl method createVirtualMachine.

private void createVirtualMachine(final DebugEnvironment environment) {
    final String sessionName = environment.getSessionName();
    final long pollTimeout = environment.getPollTimeout();
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    final AtomicBoolean connectorIsReady = new AtomicBoolean(false);
    myDebugProcessDispatcher.addListener(new DebugProcessListener() {

        @Override
        public void connectorIsReady() {
            connectorIsReady.set(true);
            semaphore.up();
            myDebugProcessDispatcher.removeListener(this);
        }
    });
    // reload to make sure that source positions are initialized
    DebuggerManagerEx.getInstanceEx(myProject).getBreakpointManager().reloadBreakpoints();
    getManagerThread().schedule(new DebuggerCommandImpl() {

        @Override
        protected void action() {
            VirtualMachine vm = null;
            try {
                final long time = System.currentTimeMillis();
                do {
                    try {
                        vm = createVirtualMachineInt();
                        break;
                    } catch (final ExecutionException e) {
                        if (pollTimeout > 0 && !myConnection.isServerMode() && e.getCause() instanceof IOException) {
                            synchronized (this) {
                                try {
                                    wait(500);
                                } catch (InterruptedException ignored) {
                                    break;
                                }
                            }
                        } else {
                            ProcessHandler processHandler = getProcessHandler();
                            boolean terminated = processHandler != null && (processHandler.isProcessTerminating() || processHandler.isProcessTerminated());
                            fail();
                            DebuggerInvocationUtil.swingInvokeLater(myProject, () -> {
                                // this problem to the user
                                if ((myExecutionResult != null && !terminated) || !connectorIsReady.get()) {
                                    ExecutionUtil.handleExecutionError(myProject, ToolWindowId.DEBUG, sessionName, e);
                                }
                            });
                            break;
                        }
                    }
                } while (System.currentTimeMillis() - time < pollTimeout);
            } finally {
                semaphore.up();
            }
            if (vm != null) {
                final VirtualMachine vm1 = vm;
                afterProcessStarted(() -> getManagerThread().schedule(new DebuggerCommandImpl() {

                    @Override
                    protected void action() throws Exception {
                        commitVM(vm1);
                    }
                }));
            }
        }

        @Override
        protected void commandCancelled() {
            try {
                super.commandCancelled();
            } finally {
                semaphore.up();
            }
        }
    });
    semaphore.waitFor();
}
Also used : Semaphore(com.intellij.util.concurrency.Semaphore) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CantRunException(com.intellij.execution.CantRunException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DebuggerCommandImpl(com.intellij.debugger.engine.events.DebuggerCommandImpl) ExecutionException(com.intellij.execution.ExecutionException)

Aggregations

Semaphore (com.intellij.util.concurrency.Semaphore)74 Ref (com.intellij.openapi.util.Ref)10 Project (com.intellij.openapi.project.Project)8 IOException (java.io.IOException)8 Nullable (org.jetbrains.annotations.Nullable)8 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)7 NotNull (org.jetbrains.annotations.NotNull)7 Test (org.junit.Test)7 VcsFileRevision (com.intellij.openapi.vcs.history.VcsFileRevision)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ProcessEvent (com.intellij.execution.process.ProcessEvent)5 File (java.io.File)5 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)4 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)4 Disposable (com.intellij.openapi.Disposable)4 Application (com.intellij.openapi.application.Application)4 Task (com.intellij.openapi.progress.Task)4 VcsAbstractHistorySession (com.intellij.openapi.vcs.history.VcsAbstractHistorySession)4 VcsAppendableHistorySessionPartner (com.intellij.openapi.vcs.history.VcsAppendableHistorySessionPartner)4 VcsHistoryProvider (com.intellij.openapi.vcs.history.VcsHistoryProvider)4