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;
}
}
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();
}
}
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();
}
}
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;
}
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();
}
Aggregations