diff --git a/AsyncKeyedLock/AsyncKeyedLock.csproj b/AsyncKeyedLock/AsyncKeyedLock.csproj
index e864bb7..9ee5fa8 100644
--- a/AsyncKeyedLock/AsyncKeyedLock.csproj
+++ b/AsyncKeyedLock/AsyncKeyedLock.csproj
@@ -7,16 +7,16 @@
https://github.com/MarkCiliaVincenti/AsyncKeyedLock.git
https://github.com/MarkCiliaVincenti/AsyncKeyedLock
LICENSE
- 5.0.3
+ 5.0.4
logo.png
- Minor performance enhancements.
+ Added pool initial fill to allow overriding of the initial number of items in the pool (defaults to the pool size).
An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), limiting concurrent threads sharing the same key to a specified number.
© 2022 Mark Cilia Vincenti
async,lock,key,semaphore,dictionary,pooling,duplicate
git
false
- 5.0.3.0
- 5.0.3.0
+ 5.0.4.0
+ 5.0.4.0
README.md
true
true
diff --git a/AsyncKeyedLock/AsyncKeyedLockOptions.cs b/AsyncKeyedLock/AsyncKeyedLockOptions.cs
index 898d911..1f47b73 100644
--- a/AsyncKeyedLock/AsyncKeyedLockOptions.cs
+++ b/AsyncKeyedLock/AsyncKeyedLockOptions.cs
@@ -15,15 +15,22 @@ public sealed class AsyncKeyedLockOptions
///
public int PoolSize { get; set; } = 0;
+ ///
+ /// The number of items to fill the pool with during initialization. Defaults to -1 (fill up to pool size).
+ ///
+ public int PoolInitialFill { get; set; } = -1;
+
///
/// Initializes options for the constructors
///
/// The maximum number of requests for the semaphore that can be granted concurrently. Defaults to 1.
/// The size of the pool to use in order for generated objects to be reused. Defaults to 0 (disabled).
- public AsyncKeyedLockOptions(int maxCount = 1, int poolSize = 0)
+ /// The number of items to fill the pool with during initialization. Defaults to -1 (fill up to pool size).
+ public AsyncKeyedLockOptions(int maxCount = 1, int poolSize = 0, int poolInitialFill = -1)
{
MaxCount = maxCount;
PoolSize = poolSize;
+ PoolInitialFill = poolInitialFill;
}
}
}
\ No newline at end of file
diff --git a/AsyncKeyedLock/AsyncKeyedLockPool.cs b/AsyncKeyedLock/AsyncKeyedLockPool.cs
index 486d003..35a3413 100644
--- a/AsyncKeyedLock/AsyncKeyedLockPool.cs
+++ b/AsyncKeyedLock/AsyncKeyedLockPool.cs
@@ -9,13 +9,23 @@ internal sealed class AsyncKeyedLockPool
private readonly BlockingCollection> _objects;
private readonly Func> _objectGenerator;
- public AsyncKeyedLockPool(Func> objectGenerator, int capacity)
+ public AsyncKeyedLockPool(Func> objectGenerator, int capacity, int initialFill = -1)
{
_objects = new BlockingCollection>(new ConcurrentBag>(), capacity);
_objectGenerator = objectGenerator;
- for (int i = 0; i < capacity; ++i)
+ if (initialFill < 0)
{
- _objects.Add(_objectGenerator(default));
+ for (int i = 0; i < capacity; ++i)
+ {
+ _objects.Add(_objectGenerator(default));
+ }
+ }
+ else
+ {
+ for (int i = 0; i < initialFill; ++i)
+ {
+ _objects.Add(_objectGenerator(default));
+ }
}
}
diff --git a/README.md b/README.md
index 06b91f7..714a1d2 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,12 @@ Setting the pool size can be done via the `AsyncKeyedLockOptions` in one of the
var asyncKeyedLocker = new AsyncKeyedLocker(new AsyncKeyedLockOptions(poolSize: 100));
```
+You can also set the initial pool fill (by default this is set to the pool size):
+
+```csharp
+var asyncKeyedLocker = new AsyncKeyedLocker(new AsyncKeyedLockOptions(poolSize: 100, poolInitialFill: 50));
+```
+
### Locking
```csharp
using (var lockObj = await asyncKeyedLocker.LockAsync(myObject))