Skip to content

Remove Nito.AsyncEx dependency, add replacements #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" />
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -71,7 +59,6 @@
<None Include="..\PowerShellEditorServices.Host\App.config">
<Link>App.config</Link>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\submodules\PSScriptAnalyzer\Engine\ScriptAnalyzerEngine.csproj">
Expand Down
4 changes: 0 additions & 4 deletions src/PowerShellEditorServices.Host.x86/packages.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" />
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -69,7 +57,6 @@
<Content Include="Microsoft.PowerShell.EditorServices.Host.DebugAdapter.cmd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\submodules\PSScriptAnalyzer\Engine\ScriptAnalyzerEngine.csproj">
Expand Down
4 changes: 0 additions & 4 deletions src/PowerShellEditorServices.Host/packages.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
//

using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Nito.AsyncEx;
using System.Threading.Tasks;

namespace Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
//

using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Nito.AsyncEx;
using System.Threading.Tasks;

namespace Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//

using Microsoft.PowerShell.EditorServices.Utility;
using Nito.AsyncEx;
using System;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -26,6 +25,9 @@ public class MessageDispatcher

private Action<Message> responseHandler;

private CancellationTokenSource messageLoopCancellationToken =
new CancellationTokenSource();

#endregion

#region Properties
Expand Down Expand Up @@ -65,22 +67,24 @@ public MessageDispatcher(

public void Start()
{

// Start the main message loop thread. The Task is
// not explicitly awaited because it is running on
// an independent background thread.
this.messageLoopThread = new AsyncContextThread(true);
this.messageLoopThread = new AsyncContextThread("Message Dispatcher");
this.messageLoopThread
.Factory
.Run(this.ListenForMessages)
.Run(() => this.ListenForMessages(this.messageLoopCancellationToken.Token))
.ContinueWith(this.OnListenTaskCompleted);
}

public void Stop()
{
// By disposing the thread we cancel all existing work
// and cause the thread to be destroyed.
// Stop the message loop thread
if (this.messageLoopThread != null)
this.messageLoopThread.Dispose();
{
this.messageLoopCancellationToken.Cancel();
this.messageLoopThread.Stop();
}
}

public void SetRequestHandler<TParams, TResult>(
Expand Down Expand Up @@ -181,13 +185,13 @@ protected void OnUnhandledException(Exception unhandledException)

#region Private Methods

private async Task ListenForMessages()
private async Task ListenForMessages(CancellationToken cancellationToken)
{
this.SynchronizationContext = SynchronizationContext.Current;

// Run the message loop
bool isRunning = true;
while (isRunning)
while (isRunning && !cancellationToken.IsCancellationRequested)
{
Message newMessage = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.PowerShell.EditorServices.Utility;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Nito.AsyncEx;
using System.IO;
using System.Text;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@
<HintPath>..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
using Microsoft.PowerShell.EditorServices.Protocol.Messages;
using Microsoft.PowerShell.EditorServices.Utility;
using Nito.AsyncEx;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -780,7 +779,7 @@ private Task RunScriptDiagnostics(
if (!this.currentSettings.ScriptAnalysis.Enable.Value)
{
// If the user has disabled script analysis, skip it entirely
return TaskConstants.Completed;
return Task.FromResult(true);
}

// If there's an existing task, attempt to cancel it
Expand All @@ -806,7 +805,9 @@ private Task RunScriptDiagnostics(
"Exception while cancelling analysis task:\n\n{0}",
e.ToString()));

return TaskConstants.Canceled;
TaskCompletionSource<bool> cancelTask = new TaskCompletionSource<bool>();
cancelTask.SetCanceled();
return cancelTask.Task;
}

// Create a fresh cancellation token and then start the task.
Expand All @@ -826,7 +827,7 @@ private Task RunScriptDiagnostics(
TaskCreationOptions.None,
TaskScheduler.Default);

return TaskConstants.Completed;
return Task.FromResult(true);
}

private static async Task DelayThenInvokeDiagnostics(
Expand Down
1 change: 0 additions & 1 deletion src/PowerShellEditorServices.Protocol/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
<package id="Nito.AsyncEx" version="3.0.1" targetFramework="net45" />
</packages>
20 changes: 5 additions & 15 deletions src/PowerShellEditorServices/PowerShellEditorServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@
<DocumentationFile>bin\Release\Microsoft.PowerShell.EditorServices.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management.Automation" />
Expand Down Expand Up @@ -109,7 +97,12 @@
<Compile Include="Session\SessionPSHostRawUserInterface.cs" />
<Compile Include="Session\SessionPSHostUserInterface.cs" />
<Compile Include="Session\SessionStateChangedEventArgs.cs" />
<Compile Include="Utility\AsyncContextThread.cs" />
<Compile Include="Utility\AsyncLock.cs" />
<Compile Include="Utility\AsyncQueue.cs" />
<Compile Include="Utility\AsyncContext.cs" />
<Compile Include="Utility\Logger.cs" />
<Compile Include="Utility\ThreadSynchronizationContext.cs" />
<Compile Include="Utility\Validate.cs" />
<Compile Include="Workspace\BufferRange.cs" />
<Compile Include="Workspace\FileChange.cs" />
Expand All @@ -119,9 +112,6 @@
<Compile Include="Workspace\ScriptRegion.cs" />
<Compile Include="Workspace\Workspace.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\submodules\PSScriptAnalyzer\Engine\ScriptAnalyzerEngine.csproj">
<Project>{f4bde3d0-3eef-4157-8a3e-722df7adef60}</Project>
Expand Down
66 changes: 27 additions & 39 deletions src/PowerShellEditorServices/Session/PowerShellContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using Microsoft.PowerShell.EditorServices.Console;
using Microsoft.PowerShell.EditorServices.Utility;
using Nito.AsyncEx;
using System;
using System.Collections;
using System.Globalization;
Expand Down Expand Up @@ -44,8 +43,7 @@ public class PowerShellContext : IDisposable
private TaskCompletionSource<IPipelineExecutionRequest> pipelineResultTask;

private object runspaceMutex = new object();
private RunspaceHandle currentRunspaceHandle;
private IAsyncWaitQueue<RunspaceHandle> runspaceWaitQueue = new DefaultAsyncWaitQueue<RunspaceHandle>();
private AsyncQueue<RunspaceHandle> runspaceWaitQueue = new AsyncQueue<RunspaceHandle>();

#endregion

Expand Down Expand Up @@ -115,6 +113,7 @@ public PowerShellContext()
this.ownsInitialRunspace = true;

this.Initialize(runspace);

}

/// <summary>
Expand Down Expand Up @@ -164,6 +163,10 @@ private void Initialize(Runspace initialRunspace)
#endif

this.SessionState = PowerShellContextState.Ready;

// Now that the runspace is ready, enqueue it for first use
RunspaceHandle runspaceHandle = new RunspaceHandle(this.currentRunspace, this);
this.runspaceWaitQueue.EnqueueAsync(runspaceHandle).Wait();
}

private Version GetPowerShellVersion()
Expand Down Expand Up @@ -198,21 +201,19 @@ private Version GetPowerShellVersion()
/// <returns>A RunspaceHandle instance that gives access to the session's runspace.</returns>
public Task<RunspaceHandle> GetRunspaceHandle()
{
lock (this.runspaceMutex)
{
if (this.currentRunspaceHandle == null)
{
this.currentRunspaceHandle = new RunspaceHandle(this.currentRunspace, this);
TaskCompletionSource<RunspaceHandle> tcs = new TaskCompletionSource<RunspaceHandle>();
tcs.SetResult(this.currentRunspaceHandle);
return tcs.Task;
}
else
{
// TODO: Use CancellationToken?
return this.runspaceWaitQueue.Enqueue();
}
}
return this.GetRunspaceHandle(CancellationToken.None);
}

/// <summary>
/// Gets a RunspaceHandle for the session's runspace. This
/// handle is used to gain temporary ownership of the runspace
/// so that commands can be executed against it directly.
/// </summary>
/// <param name="cancellationToken">A CancellationToken that can be used to cancel the request.</param>
/// <returns>A RunspaceHandle instance that gives access to the session's runspace.</returns>
public Task<RunspaceHandle> GetRunspaceHandle(CancellationToken cancellationToken)
{
return this.runspaceWaitQueue.DequeueAsync(cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -532,30 +533,17 @@ internal void ReleaseRunspaceHandle(RunspaceHandle runspaceHandle)
{
Validate.IsNotNull("runspaceHandle", runspaceHandle);

IDisposable dequeuedTask = null;

lock (this.runspaceMutex)
if (this.runspaceWaitQueue.IsEmpty)
{
if (runspaceHandle != this.currentRunspaceHandle)
{
throw new InvalidOperationException("Released runspace handle was not the current handle.");
}

this.currentRunspaceHandle = null;

if (!this.runspaceWaitQueue.IsEmpty)
{
this.currentRunspaceHandle = new RunspaceHandle(this.currentRunspace, this);
dequeuedTask =
this.runspaceWaitQueue.Dequeue(
this.currentRunspaceHandle);
}
var newRunspaceHandle = new RunspaceHandle(this.currentRunspace, this);
this.runspaceWaitQueue.EnqueueAsync(newRunspaceHandle).Wait();
}

// If a queued task was dequeued, call Dispose to cause it to be executed.
if (dequeuedTask != null)
else
{
dequeuedTask.Dispose();
// Write the situation to the log since this shouldn't happen
Logger.Write(
LogLevel.Error,
"The PowerShellContext.runspaceWaitQueue has more than one item");
}
}

Expand Down
Loading