Skip to content

Commit f59ff02

Browse files
committed
Share Deque
1 parent e469136 commit f59ff02

File tree

7 files changed

+30
-150
lines changed

7 files changed

+30
-150
lines changed
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ public void EnqueueTail(T item)
4747
// _size++;
4848
//}
4949

50-
public T PeekHead()
51-
{
52-
Debug.Assert(!IsEmpty); // caller's responsibility to make sure there are elements remaining
53-
return _array[_head];
54-
}
55-
56-
public T PeekTail()
57-
{
58-
Debug.Assert(!IsEmpty); // caller's responsibility to make sure there are elements remaining
59-
return _array[_head];
60-
}
61-
6250
public T DequeueHead()
6351
{
6452
Debug.Assert(!IsEmpty); // caller's responsibility to make sure there are elements remaining
@@ -75,6 +63,23 @@ public T DequeueHead()
7563
return item;
7664
}
7765

66+
public T PeekHead()
67+
{
68+
Debug.Assert(!IsEmpty); // caller's responsibility to make sure there are elements remaining
69+
return _array[_head];
70+
}
71+
72+
public T PeekTail()
73+
{
74+
Debug.Assert(!IsEmpty); // caller's responsibility to make sure there are elements remaining
75+
var index = _tail - 1;
76+
if (index == -1)
77+
{
78+
index = _array.Length - 1;
79+
}
80+
return _array[index];
81+
}
82+
7883
public T DequeueTail()
7984
{
8085
Debug.Assert(!IsEmpty); // caller's responsibility to make sure there are elements remaining

src/libraries/System.Threading.Channels/src/System.Threading.Channels.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ System.Threading.Channel&lt;T&gt;</PackageDescription>
1111
</PropertyGroup>
1212
<ItemGroup>
1313
<Compile Include="System\VoidResult.cs" />
14-
<Compile Include="System\Collections\Generic\Deque.cs" />
1514
<Compile Include="System\Threading\Channels\AsyncOperation.cs" />
1615
<Compile Include="System\Threading\Channels\AsyncOperation.netcoreapp.cs"
1716
Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'" />
@@ -40,6 +39,8 @@ System.Threading.Channel&lt;T&gt;</PackageDescription>
4039
Link="Common\Internal\Padding.cs" />
4140
<Compile Include="$(CommonPath)System\Collections\Concurrent\SingleProducerConsumerQueue.cs"
4241
Link="Common\System\Collections\Concurrent\SingleProducerConsumerQueue.cs" />
42+
<Compile Include="$(CommonPath)System\Collections\Generic\Deque.cs"
43+
Link="Common\System\Collections\Generic\Deque.cs" />
4344
</ItemGroup>
4445
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
4546
<Reference Include="System.Collections" />

src/libraries/System.Threading.Channels/src/System/Collections/Generic/Deque.cs

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/libraries/System.Threading.RateLimiting/src/System.Threading.RateLimiting.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ System.Threading.RateLimiting.TokenBucketRateLimiter
1111
System.Threading.RateLimiting.RateLimitLease</PackageDescription>
1212
</PropertyGroup>
1313
<ItemGroup>
14-
<Compile Include="System\Threading\RateLimiting\Internal\Deque.cs" />
1514
<Compile Include="System\Threading\RateLimiting\ConcurrencyLimiter.cs" />
1615
<Compile Include="System\Threading\RateLimiting\ConcurrencyLimiterOptions.cs" />
1716
<Compile Include="System\Threading\RateLimiting\MetadataName.cs" />
@@ -21,6 +20,8 @@ System.Threading.RateLimiting.RateLimitLease</PackageDescription>
2120
<Compile Include="System\Threading\RateLimiting\RateLimitLease.cs" />
2221
<Compile Include="System\Threading\RateLimiting\TokenBucketRateLimiter.cs" />
2322
<Compile Include="System\Threading\RateLimiting\TokenBucketRateLimiterOptions.cs" />
23+
<Compile Include="$(CommonPath)System\Collections\Generic\Deque.cs"
24+
Link="Common\System\Collections\Generic\Deque.cs" />
2425
</ItemGroup>
2526
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
2627
<Reference Include="System.Runtime" />

src/libraries/System.Threading.RateLimiting/src/System/Threading/RateLimiting/RateLimitLease.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace System.Threading.RateLimiting
88
{
99
/// <summary>
1010
/// Abstraction for leases returned by <see cref="RateLimiter"/> implementations.
11-
/// A lease represents the success or failure to acquire a resource and any potential metadata that is relevant to the acquisition operation.
11+
/// A lease represents the success or failure to acquire a resource and contains potential metadata that is relevant to the acquisition operation.
1212
/// </summary>
1313
public abstract class RateLimitLease : IDisposable
1414
{

src/libraries/System.Threading.RateLimiting/tests/ConcurrencyLimiterTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public override async Task CanAcquireResourceAsync_QueuesAndGrabsOldest()
7272
[Fact]
7373
public override async Task CanAcquireResourceAsync_QueuesAndGrabsNewest()
7474
{
75-
var limiter = new ConcurrencyLimiter(new ConcurrencyLimiterOptions(1, QueueProcessingOrder.NewestFirst, 2));
76-
var lease = await limiter.WaitAsync().DefaultTimeout();
75+
var limiter = new ConcurrencyLimiter(new ConcurrencyLimiterOptions(2, QueueProcessingOrder.NewestFirst, 3));
76+
var lease = await limiter.WaitAsync(2).DefaultTimeout();
7777

7878
Assert.True(lease.IsAcquired);
79-
var wait1 = limiter.WaitAsync();
79+
var wait1 = limiter.WaitAsync(2);
8080
var wait2 = limiter.WaitAsync();
8181
Assert.False(wait1.IsCompleted);
8282
Assert.False(wait2.IsCompleted);

src/libraries/System.Threading.RateLimiting/tests/TokenBucketRateLimiterTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ public override async Task CanAcquireResourceAsync_QueuesAndGrabsOldest()
8383
[Fact]
8484
public override async Task CanAcquireResourceAsync_QueuesAndGrabsNewest()
8585
{
86-
var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(1, QueueProcessingOrder.NewestFirst, 2,
86+
var limiter = new TokenBucketRateLimiter(new TokenBucketRateLimiterOptions(2, QueueProcessingOrder.NewestFirst, 3,
8787
TimeSpan.FromMinutes(0), 1, autoReplenishment: false));
88-
var lease = await limiter.WaitAsync().DefaultTimeout();
8988

89+
var lease = await limiter.WaitAsync(2).DefaultTimeout();
9090
Assert.True(lease.IsAcquired);
91-
var wait1 = limiter.WaitAsync();
91+
92+
var wait1 = limiter.WaitAsync(2);
9293
var wait2 = limiter.WaitAsync();
9394
Assert.False(wait1.IsCompleted);
9495
Assert.False(wait2.IsCompleted);
@@ -104,6 +105,7 @@ public override async Task CanAcquireResourceAsync_QueuesAndGrabsNewest()
104105
lease.Dispose();
105106
Assert.Equal(0, limiter.GetAvailablePermits());
106107
Assert.True(limiter.TryReplenish());
108+
Assert.True(limiter.TryReplenish());
107109

108110
lease = await wait1.DefaultTimeout();
109111
Assert.True(lease.IsAcquired);

0 commit comments

Comments
 (0)