Skip to content

Commit

Permalink
Small perf changes (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored Feb 2, 2021
1 parent 951eb6d commit 83584a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,14 @@ public override string ServerVersion
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ServerProcessId/*' />
public int ServerProcessId
{
get => State.Equals(ConnectionState.Open) | State.Equals(ConnectionState.Executing) | State.Equals(ConnectionState.Fetching) ?
GetOpenTdsConnection().ServerProcessId : 0;
get
{
if ((State & (ConnectionState.Open | ConnectionState.Executing | ConnectionState.Fetching)) > 0)
{
return GetOpenTdsConnection().ServerProcessId;
}
return 0;
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnection.xml' path='docs/members[@name="SqlConnection"]/State/*' />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2139,6 +2139,8 @@ public static MethodInfo GetPromotedToken
/// </summary>
internal class ConcurrentQueueSemaphore
{
private static readonly Action<Task, object> s_continuePop = ContinuePop;

private readonly SemaphoreSlim _semaphore;
private readonly ConcurrentQueue<TaskCompletionSource<bool>> _queue =
new ConcurrentQueue<TaskCompletionSource<bool>>();
Expand All @@ -2152,18 +2154,27 @@ public Task WaitAsync(CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
_queue.Enqueue(tcs);
_semaphore.WaitAsync().ContinueWith(t =>
{
if (_queue.TryDequeue(out TaskCompletionSource<bool> popped))
popped.SetResult(true);
}, cancellationToken);
_semaphore.WaitAsync().ContinueWith(
continuationAction: s_continuePop,
state: _queue,
cancellationToken: cancellationToken
);
return tcs.Task;
}

public void Release()
{
_semaphore.Release();
}

private static void ContinuePop(Task task, object state)
{
ConcurrentQueue<TaskCompletionSource<bool>> queue = (ConcurrentQueue<TaskCompletionSource<bool>>)state;
if (queue.TryDequeue(out TaskCompletionSource<bool> popped))
{
popped.SetResult(true);
}
}
}

}//namespace

0 comments on commit 83584a1

Please sign in to comment.