public void CancelEven() { var bal = new BinaryAsyncLock(true); var cts = new CancellationTokenSource(); var task1 = bal.ResetAsync(); var task2 = bal.ResetAsync(cts.Token); var task3 = bal.ResetAsync(cts.Token); var task4 = bal.WaitAsync(); var task5 = bal.WaitAsync(cts.Token); task1.Should().BeRanToCompletion(); task2.Should().NotBeCompleted(); task3.Should().NotBeCompleted(); task4.Should().NotBeCompleted(); task5.Should().NotBeCompleted(); cts.Cancel(); task1.Should().BeRanToCompletion(); task2.Should().BeCanceled(); task3.Should().BeCanceled(); task4.Should().NotBeCompleted(); task5.Should().BeCanceled(); task1.Result.Reset(); task1.Should().BeRanToCompletion(); task2.Should().BeCanceled(); task3.Should().BeCanceled(); task4.Should().BeRanToCompletion(); task5.Should().BeCanceled(); bal.IsSet.Should().BeFalse(); }
public void SequentalWaitReset_SetToken() { var bal = new BinaryAsyncLock(true); var task1 = bal.ResetAsync(); var task2 = bal.WaitAsync(); var task3 = bal.ResetAsync(); var task4 = bal.WaitAsync(); var task5 = bal.ResetAsync(); task1.Should().BeRanToCompletion(); task2.Should().NotBeCompleted(); task3.Should().NotBeCompleted(); task4.Should().NotBeCompleted(); task5.Should().NotBeCompleted(); bal.IsSet.Should().BeFalse(); task1.Result.Set(); task1.Should().BeRanToCompletion(); task2.Should().BeRanToCompletion(); task3.Should().BeRanToCompletion(); task4.Should().NotBeCompleted(); task5.Should().NotBeCompleted(); bal.IsSet.Should().BeFalse(); task3.Result.Set(); task1.Should().BeRanToCompletion(); task2.Should().BeRanToCompletion(); task3.Should().BeRanToCompletion(); task4.Should().BeRanToCompletion(); task5.Should().BeRanToCompletion(); bal.IsSet.Should().BeFalse(); }
public void WaitAsync_WaitAsync_SetIfLast_SetIfLast() { var bal = new BinaryAsyncLock(); var task1 = bal.WaitAsync(); var task2 = bal.WaitAsync(); task1.Result.Set(); task2.Result.Set(); task1.Should().BeRanToCompletion(); task2.Should().BeRanToCompletion(); bal.IsSet.Should().BeTrue(); }
public async Task BuildIndexAsync(IPackageIndex packageIndex = null) { packageIndex = packageIndex ?? _coreShell.ExportProvider.GetExportedValue <IPackageIndex>(); var ready = await _buildIndexLock.WaitAsync(); try { if (!ready) { // First populate index for popular packages that are commonly preloaded foreach (var pi in packageIndex.Packages) { foreach (var f in pi.Functions) { // Avoid duplicates. Packages are normally populated from base (intrinsic) // to user so we don't want new packages to override base function information if (!_functionToPackageMap.ContainsKey(f.Name)) { _functionToPackageMap[f.Name] = pi.Name; } } } } } finally { _buildIndexLock.Release(); } }
/// <summary> /// Builds function index /// </summary> /// <param name="packageIndex">Package index, if available. If not available, /// index builder will attempt to obtain it from the service container</param> public async Task BuildIndexAsync(IPackageIndex packageIndex = null, CancellationToken ct = default(CancellationToken)) { packageIndex = packageIndex ?? Services.GetService <IPackageIndex>(); var lockToken = await _buildIndexLock.WaitAsync(ct); try { if (!lockToken.IsSet) { // First populate index for popular packages that are commonly preloaded foreach (var pi in packageIndex.Packages) { if (ct.IsCancellationRequested) { break; } foreach (var f in pi.Functions) { if (ct.IsCancellationRequested) { break; } RegisterFunction(f.Name, pi.Name, f.IsInternal); } } } } finally { lockToken.Set(); } }
public void WaitAsync_ResetAsync() { var bal = new BinaryAsyncLock(); var task1 = bal.WaitAsync(); var task2 = bal.ResetAsync(); task1.Should().BeRanToCompletion(); task2.Should().NotBeCompleted(); bal.IsSet.Should().BeFalse(); }
public void CancelWaitAsync_Set() { var bal = new BinaryAsyncLock(true); var cts = new CancellationTokenSource(); var tasks = Enumerable.Range(0, 4).Select(i => bal.WaitAsync(cts.Token)).ToList(); tasks.Should().OnlyContain(t => t.Status == TaskStatus.RanToCompletion); cts.Cancel(); tasks.Should().OnlyContain(t => t.Status == TaskStatus.RanToCompletion); }
public void CancelResetAsync_Reset() { var bal = new BinaryAsyncLock(); var cts = new CancellationTokenSource(); var tasks = Enumerable.Range(0, 4).Select(i => bal.WaitAsync(cts.Token)).ToList(); tasks.Should().ContainSingle(t => t.IsCompleted); cts.Cancel(); tasks.Should().OnlyContain(t => t.IsCompleted); }
public void ResetAsync_ResetAsyncCanceled_WaitAsync() { var bal = new BinaryAsyncLock(true); var cts = new CancellationTokenSource(); cts.Cancel(); var task1 = bal.ResetAsync(cts.Token); var task2 = bal.WaitAsync(); task1.Should().BeCanceled(); task2.Should().BeRanToCompletion(); bal.IsSet.Should().BeTrue(); }
private async Task EnsureAvailablePackagesLoadedAsync() { var availablePackagesLoaded = await _availableLock.WaitAsync(); try { if (!availablePackagesLoaded) { await LoadAvailablePackagesAsync(); } } catch (RPackageManagerException ex) { _coreShell.DispatchOnUIThread(() => AddErrorMessage(ex.Message)); } finally { _availableLock.Release(); } }
public async Task Release() { var bal = new BinaryAsyncLock(); var count = 0; await ParallelTools.InvokeAsync(4, async i => { var isSet = await bal.WaitAsync(); if (!isSet) { Interlocked.Increment(ref count); bal.Release(); } }); count.Should().Be(1); }
private async Task EnsureInstalledAndAvailablePackagesLoadedAsync() { var areLoaded = await _availableAndInstalledLock.WaitAsync(); if (!areLoaded) { IsLoading = true; try { await LoadInstalledAndAvailablePackagesAsync(); } finally { IsLoading = false; _availableAndInstalledLock.Release(); } } }
public async Task WaitAsync_SetInCtor() { var bal = new BinaryAsyncLock(true); var count = 0; await ParallelTools.InvokeAsync(4, async i => { await bal.WaitAsync(); if (!bal.IsSet) { await Task.Delay(50); Interlocked.Increment(ref count); } }); count.Should().Be(0); }
public async Task WaitAsync_Reset() { var bal = new BinaryAsyncLock(); var count = 0; await ParallelTools.InvokeAsync(4, async i => { var token = await bal.WaitAsync(); if (!bal.IsSet) { await Task.Delay(50); Interlocked.Increment(ref count); token.Reset(); } }); count.Should().Be(4); }
private async Task ReloadInstalledAndLoadedPackagesAsync() { _installedAndLoadedLock.ResetIfNotWaiting(); var areLoaded = await _installedAndLoadedLock.WaitAsync(); try { if (!areLoaded) { await LoadInstalledAndLoadedPackagesAsync(); } } catch (RPackageManagerException ex) { _coreShell.DispatchOnUIThread(() => AddErrorMessage(ex.Message)); } finally { _installedAndLoadedLock.Release(); } }
private async Task EnsureBrokerStartedAsync(CancellationToken cancellationToken) { DisposableBag.ThrowIfDisposed(); await TaskUtilities.SwitchToBackgroundThread(); var lockToken = await _connectLock.WaitAsync(cancellationToken); try { if (!lockToken.IsSet) { await ConnectToBrokerWorker(cancellationToken); } lockToken.Set(); } finally { lockToken.Reset(); } }
protected override async Task ConnectToBrokerAsync() { DisposableBag.ThrowIfDisposed(); await TaskUtilities.SwitchToBackgroundThread(); try { if (!await _connectLock.WaitAsync()) { if (_brokerProcess == null) { await ConnectToBrokerWorker(); } } } finally { _connectLock.Release(); } }
public async Task EnsureBrokerStartedAsync(string name) { var lockToken = await _connectLock.WaitAsync(); try { if (!lockToken.IsSet) { _process = new RemoteBrokerProcess(name, _logFolder, _fileSystem, _installations, _processService); await _process.StartAsync(() => { _process = null; _connectLock.EnqueueReset(); }); } lockToken.Set(); } finally { lockToken.Reset(); } }
public async Task EnsureBrokerStartedAsync(string name, IServiceContainer services) { var lockToken = await _connectLock.WaitAsync(); try { if (!lockToken.IsSet) { _process = new RemoteBrokerProcess(name, _logFolder, services.FileSystem(), services.GetService <IRInstallationService>(), services.Process()); await _process.StartAsync(() => { _process = null; _connectLock.EnqueueReset(); }); } lockToken.Set(); } finally { lockToken.Reset(); } }
public async Task Reset() { var bal = new BinaryAsyncLock(); var count = 0; await ParallelTools.InvokeAsync(4, async i => { try { var isSet = await bal.WaitAsync(); if (!isSet) { await Task.Delay(50); bal.Reset(); } } catch (OperationCanceledException) { Interlocked.Increment(ref count); } }); count.Should().Be(3); }
public async Task BuildIndexAsync() { var ready = await _buildIndexLock.WaitAsync(); try { if (!ready) { var startTotalTime = DateTime.Now; await TaskUtilities.SwitchToBackgroundThread(); await _host.CreateSessionAsync(); Debug.WriteLine("R function host start: {0} ms", (DateTime.Now - startTotalTime).TotalMilliseconds); var startTime = DateTime.Now; // Fetch list of available packages from R session await BuildPackageListAsync(); Debug.WriteLine("R package names/description: {0} ms", (DateTime.Now - startTime).TotalMilliseconds); // Populate function index for preloaded packages first startTime = DateTime.Now; await BuildPreloadedPackagesFunctionListAsync(); Debug.WriteLine("R function index (preloaded): {0} ms", (DateTime.Now - startTime).TotalMilliseconds); // Populate function index for all remaining packages startTime = DateTime.Now; await BuildRemainingPackagesFunctionListAsync(); Debug.WriteLine("R function index (remaining): {0} ms", (DateTime.Now - startTime).TotalMilliseconds); await _functionIndex.BuildIndexAsync(this); Debug.WriteLine("R function index total: {0} ms", (DateTime.Now - startTotalTime).TotalMilliseconds); } } catch (RHostDisconnectedException ex) { Debug.WriteLine(ex.Message); } finally { _buildIndexLock.Release(); } }
public async Task DisconnectAsync() { if (_runTask == null) { return; } await TaskUtilities.SwitchToBackgroundThread(); // We may get MessageTransportException from any concurrent SendAsync or ReceiveAsync when the host // drops connection after we request it to do so. To ensure that those don't bubble up to the // client, cancel this token to indicate that we're shutting down the host - SendAsync and // ReceiveAsync will take care of wrapping any WSE into OperationCanceledException. _cts.Cancel(); var token = await _disconnectLock.WaitAsync(); if (!token.IsSet) { try { // Don't use _cts, since it's already cancelled. We want to try to send this message in // any case, and we'll catch MessageTransportException if no-one is on the other end anymore. await _transport.CloseAsync(); token.Set(); } catch (OperationCanceledException) { } catch (MessageTransportException) { } finally { token.Reset(); } } try { if (_runTask.Status == TaskStatus.Running) { await _runTask; } } catch (OperationCanceledException) { // Expected during disconnect. } catch (MessageTransportException) { // Possible and valid during disconnect. } }
public void ResetIfNotWaiting_ResetAsync_Skip_WaitAsync_Set() { var bal = new BinaryAsyncLock(); var task = bal.ResetAsync(); task.Should().BeRanToCompletion(); bal.IsSet.Should().BeFalse(); task.Result.Reset(); bal.IsSet.Should().BeFalse(); task = bal.WaitAsync(); task.Should().BeRanToCompletion(); bal.IsSet.Should().BeFalse(); task.Result.Set(); bal.IsSet.Should().BeTrue(); }
/// <summary> /// Builds function index /// </summary> /// <param name="packageIndex">Package index, if available. If not available, /// index builder will attempt to obtain it from the service container</param> public async Task BuildIndexAsync(IPackageIndex packageIndex = null) { packageIndex = packageIndex ?? Services.GetService <IPackageIndex>(); var lockToken = await _buildIndexLock.WaitAsync(); try { if (!lockToken.IsSet) { // First populate index for popular packages that are commonly preloaded foreach (var pi in packageIndex.Packages) { foreach (var f in pi.Functions) { RegisterFunction(f.Name, pi.Name); } } } } finally { lockToken.Set(); } }
private async Task EnsureCreatedAsync() { var token = await _initializationLock.WaitAsync(); try { if (!token.IsSet) { // Delay-create log since permission is established when settings are loaded // which may happen after ctor is called. _logs = new IActionLogWriter[Enum.GetValues(typeof(LogVerbosity)).Length]; _logs[(int)LogVerbosity.None] = NullLogWriter.Instance; IActionLogWriter mainWriter = NullLogWriter.Instance; if (_permissions.CurrentVerbosity >= LogVerbosity.Minimal) { mainWriter = _writer ?? FileLogWriter.InTempFolder(_appName); } // Unfortunately, creation of event sources in OS logs requires local admin rights. // http://www.christiano.ch/wordpress/2009/12/02/iis7-web-application-writing-to-event-log-generates-security-exception/ // So we can't use OS event logs as in Dev15 there is no MSI which could elevate.. // _maxLogLevel >= LogLevel.Minimal ? (_writer ?? new ApplicationLogWriter(_appName)) : NullLogWriter.Instance; _logs[(int)LogVerbosity.Minimal] = mainWriter; _logs[(int)LogVerbosity.Normal] = _permissions.CurrentVerbosity >= LogVerbosity.Normal ? mainWriter : NullLogWriter.Instance; if (_permissions.CurrentVerbosity == LogVerbosity.Traffic) { _logs[(int)LogVerbosity.Traffic] = _writer ?? FileLogWriter.InTempFolder(_appName + ".traffic"); } else { _logs[(int)LogVerbosity.Traffic] = NullLogWriter.Instance; } } } finally { token.Set(); } }
public async Task CreateSessionAsync() { await _lock.WaitAsync(); try { if (Session == null) { Session = _sessionProvider.GetOrCreate(SessionId, _brokerConnector); } if (!Session.IsHostRunning) { int timeout = _coreShell.IsUnitTestEnvironment ? 10000 : 3000; await Session.StartHostAsync(new RHostStartupInfo { Name = "IntelliSense", CranMirrorName = RToolsSettings.Current.CranMirror, CodePage = RToolsSettings.Current.RCodePage }, null, timeout); } } finally { _lock.Release(); } }
public async Task BuildIndexAsync(CancellationToken ct = default(CancellationToken)) { var lockToken = await _buildIndexLock.WaitAsync(ct); await BuildIndexAsync(lockToken, ct); }
public async Task BuildIndexAsync() { var lockToken = await _buildIndexLock.WaitAsync(); await BuildIndexAsync(lockToken); }