Skip to content

Commit 38a83f8

Browse files
committed
Removing obsolete global lock since now we are using filename-based locking
1 parent 10a72f1 commit 38a83f8

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

AsyncToSyncConverter.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,20 @@ static class AsyncToSyncConverter
6363

6464
public static async Task AsyncFileUpdated(string fullName, Context context)
6565
{
66-
using (await Global.FileOperationAsyncLock.LockAsync())
66+
var otherFullName = ConsoleWatch.GetOtherFullName(fullName);
67+
68+
var filenames = new List<string>()
69+
{
70+
fullName,
71+
otherFullName
72+
};
73+
74+
//NB! in order to avoid deadlocks, always take the locks in deterministic order
75+
filenames.Sort(StringComparer.InvariantCultureIgnoreCase);
76+
77+
using (await Global.FileOperationLocks.LockAsync(filenames[0], context.Token))
78+
using (await Global.FileOperationLocks.LockAsync(filenames[1], context.Token))
79+
//using (await Global.FileOperationAsyncLock.LockAsync())
6780
{
6881
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
6982
var fileData = await FileExtensions.ReadAllTextAsync(@"\\?\" + fullName, context.Token);

Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ internal static class Global
4141

4242

4343

44-
internal static readonly AsyncLock FileOperationAsyncLock = new AsyncLock();
45-
46-
internal static ConcurrentDictionary<string, DateTime> ConverterSavedFileDates = new ConcurrentDictionary<string, DateTime>();
44+
internal static readonly AsyncLockQueueDictionary FileOperationLocks = new AsyncLockQueueDictionary();
45+
//internal static readonly AsyncLock FileOperationAsyncLock = new AsyncLock();
4746
}
4847
#pragma warning restore S2223
4948

@@ -303,6 +302,7 @@ internal class ConsoleWatch
303302
public static bool DoingInitialSync = false;
304303
#pragma warning restore S2223
305304

305+
private static ConcurrentDictionary<string, DateTime> ConverterSavedFileDates = new ConcurrentDictionary<string, DateTime>();
306306
private static readonly AsyncLockQueueDictionary FileLocks = new AsyncLockQueueDictionary();
307307

308308

@@ -396,7 +396,7 @@ public static async Task DeleteFile(string fullName, Context context)
396396
public static DateTime GetConverterSaveDate(string fullName)
397397
{
398398
DateTime converterSaveDate;
399-
if (!Global.ConverterSavedFileDates.TryGetValue(fullName, out converterSaveDate))
399+
if (!ConverterSavedFileDates.TryGetValue(fullName, out converterSaveDate))
400400
{
401401
converterSaveDate = DateTime.MinValue;
402402
}
@@ -733,7 +733,7 @@ public static async Task SaveFileModifications(string fullName, string fileData,
733733
await FileExtensions.WriteAllTextAsync(@"\\?\" + otherFullName, fileData, context.Token);
734734

735735
var now = DateTime.UtcNow; //NB! compute now after saving the file
736-
Global.ConverterSavedFileDates[otherFullName] = now;
736+
ConverterSavedFileDates[otherFullName] = now;
737737

738738

739739
await AddMessage(ConsoleColor.Magenta, $"Synchronised updates from file {fullName}", context);
@@ -752,7 +752,7 @@ public static async Task SaveFileModifications(string fullName, string fileData,
752752
await ConsoleWatch.WriteException(ex, context);
753753
}
754754

755-
Global.ConverterSavedFileDates[otherFullName] = now;
755+
ConverterSavedFileDates[otherFullName] = now;
756756
}
757757
}
758758

@@ -780,7 +780,7 @@ public static async Task SaveFileModifications(string fullName, byte[] fileData,
780780
await FileExtensions.WriteAllBytesAsync(@"\\?\" + otherFullName, fileData, context.Token);
781781

782782
var now = DateTime.UtcNow; //NB! compute now after saving the file
783-
Global.ConverterSavedFileDates[otherFullName] = now;
783+
ConverterSavedFileDates[otherFullName] = now;
784784

785785

786786
await AddMessage(ConsoleColor.Magenta, $"Synchronised updates from file {fullName}", context);
@@ -799,7 +799,7 @@ public static async Task SaveFileModifications(string fullName, byte[] fileData,
799799
await ConsoleWatch.WriteException(ex, context);
800800
}
801801

802-
Global.ConverterSavedFileDates[otherFullName] = now;
802+
ConverterSavedFileDates[otherFullName] = now;
803803
}
804804
}
805805

SyncToAsyncConverter.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,20 @@ static SyncToAsyncConverter()
6161

6262
public static async Task SyncFileUpdated(string fullName, Context context)
6363
{
64-
using (await Global.FileOperationAsyncLock.LockAsync())
64+
var otherFullName = ConsoleWatch.GetOtherFullName(fullName);
65+
66+
var filenames = new List<string>()
67+
{
68+
fullName,
69+
otherFullName
70+
};
71+
72+
//NB! in order to avoid deadlocks, always take the locks in deterministic order
73+
filenames.Sort(StringComparer.InvariantCultureIgnoreCase);
74+
75+
using (await Global.FileOperationLocks.LockAsync(filenames[0], context.Token))
76+
using (await Global.FileOperationLocks.LockAsync(filenames[1], context.Token))
77+
//using (await Global.FileOperationAsyncLock.LockAsync())
6578
{
6679
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
6780
var fileData = await FileExtensions.ReadAllTextAsync(@"\\?\" + fullName, context.Token);

0 commit comments

Comments
 (0)