Provides locking based on a string key. Locks are local to the LockProvider instance. The class handles disposing of unused locks. Generally used for coordinating writes to files (of which there can be millions). Only keeps key/lock pairs in memory which are in use. Thread-safe.
More...
|
bool | MayBeLocked (string key) |
| Returns true if the given key might be locked. More...
|
|
bool | TryExecute (string key, int timeoutMs, LockCallback success) |
| Attempts to execute the 'success' callback inside a lock based on 'key'. If successful, returns true. If the lock cannot be acquired within 'timoutMs', returns false In a worst-case scenario, it could take up to twice as long as 'timeoutMs' to return false. More...
|
|
|
Dictionary< String, Object > | locks |
| The only objects in this collection should be for open files. More...
|
|
object | createLock = new object() |
| Synchronization object for modifications to the 'locks' dictionary More...
|
|
Provides locking based on a string key. Locks are local to the LockProvider instance. The class handles disposing of unused locks. Generally used for coordinating writes to files (of which there can be millions). Only keeps key/lock pairs in memory which are in use. Thread-safe.
Definition at line 18 of file LockProvider.cs.
bool ImageResizer.Plugins.DiskCache.LockProvider.MayBeLocked |
( |
string |
key | ) |
|
|
inline |
Returns true if the given key might be locked.
- Parameters
-
- Returns
Definition at line 35 of file LockProvider.cs.
39 return locks.ContainsKey(key);
object createLock
Synchronization object for modifications to the 'locks' dictionary
bool ImageResizer.Plugins.DiskCache.LockProvider.TryExecute |
( |
string |
key, |
|
|
int |
timeoutMs, |
|
|
LockCallback |
success |
|
) |
| |
|
inline |
Attempts to execute the 'success' callback inside a lock based on 'key'. If successful, returns true. If the lock cannot be acquired within 'timoutMs', returns false In a worst-case scenario, it could take up to twice as long as 'timeoutMs' to return false.
- Parameters
-
key | |
success | |
failure | |
timeoutMs | |
Definition at line 53 of file LockProvider.cs.
55 DateTime startedAt = DateTime.UtcNow;
58 bool validLock =
true;
60 object itemLock = null;
70 if (!
locks.TryGetValue(key, out itemLock))
71 locks[key] = itemLock =
new Object();
80 if (System.Threading.Monitor.TryEnter(itemLock, timeoutMs)) {
87 object newLock = null;
88 validLock = locks.TryGetValue(key, out newLock);
89 validLock = validLock && newLock == itemLock;
98 System.Threading.Monitor.Exit(itemLock);
106 if (!validLock && Math.Abs(DateTime.UtcNow.Subtract(startedAt).TotalMilliseconds) > timeoutMs) {
113 }
while (!validLock);
126 if (System.Threading.Monitor.TryEnter(itemLock)) {
132 object existingLock = null;
133 if (
locks.TryGetValue(key, out existingLock)
134 && existingLock == itemLock)
138 System.Threading.Monitor.Exit(itemLock);
object createLock
Synchronization object for modifications to the 'locks' dictionary
Dictionary< String, Object > locks
The only objects in this collection should be for open files.
object ImageResizer.Plugins.DiskCache.LockProvider.createLock = new object() |
|
protected |
Synchronization object for modifications to the 'locks' dictionary
Definition at line 28 of file LockProvider.cs.
Dictionary<String, Object> ImageResizer.Plugins.DiskCache.LockProvider.locks |
|
protected |
Initial value:=
new Dictionary<string, object>(StringComparer.Ordinal)
The only objects in this collection should be for open files.
Definition at line 23 of file LockProvider.cs.
The documentation for this class was generated from the following file: