-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move static field
GlobalLocks
to primary ctor param `globalLocked…
…` to fix different instance sharing the same type param `TKey` will also share this static field * rename field `_localLocks` to `_localLocked` * rename method `AcquireLocks()` to `Acquire()` @ SaverLocks.cs + static field `GlobalLocked*` & field `_*saverLocks` for primary ctor param `saverLocksFactory` @ class `UserSaver`, `ReplySignatureSaver` & `AuthorRevisionSaver` @ c#/crawler
- Loading branch information
Showing
4 changed files
with
34 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
namespace tbm.Crawler.Tieba.Crawl.Saver; | ||
|
||
public sealed class SaverLocks<TKey> : IDisposable | ||
public sealed class SaverLocks<TKey>(ISet<TKey> globalLocked) : IDisposable | ||
{ | ||
private static readonly HashSet<TKey> GlobalLocks = []; | ||
private readonly List<TKey> _localLocks = []; | ||
public delegate SaverLocks<TKey> New(ISet<TKey> globalLocked); | ||
|
||
private readonly List<TKey> _localLocked = []; | ||
|
||
public void Dispose() | ||
{ | ||
lock (GlobalLocks) GlobalLocks.ExceptWith(_localLocks); | ||
lock (_localLocks) _localLocks.Clear(); | ||
lock (globalLocked) globalLocked.ExceptWith(_localLocked); | ||
lock (_localLocked) _localLocked.Clear(); | ||
} | ||
|
||
public IReadOnlyCollection<TKey> AcquireLocks(IReadOnlyCollection<TKey> pendingLocking) | ||
public IReadOnlyCollection<TKey> Acquire(IReadOnlyCollection<TKey> pendingLocking) | ||
{ | ||
if (pendingLocking.Count == 0) return []; | ||
var newlyLocked = new List<TKey>(pendingLocking.Count); | ||
lock (GlobalLocks) | ||
lock (globalLocked) | ||
{ | ||
newlyLocked.AddRange(pendingLocking.Except(GlobalLocks)); | ||
GlobalLocks.UnionWith(newlyLocked); | ||
newlyLocked.AddRange(pendingLocking.Except(globalLocked)); | ||
globalLocked.UnionWith(newlyLocked); | ||
} | ||
lock (_localLocks) _localLocks.AddRange(newlyLocked); | ||
lock (_localLocked) _localLocked.AddRange(newlyLocked); | ||
return newlyLocked; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters