Skip to content

Commit d9a56c5

Browse files
committed
Change the location of locking
1 parent 91ef31a commit d9a56c5

File tree

3 files changed

+42
-68
lines changed

3 files changed

+42
-68
lines changed

AsyncToSyncConverter.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,6 @@ static class AsyncToSyncConverter
6363

6464
public static async Task AsyncFileUpdated(string fullName, Context context)
6565
{
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.CodeFileOperationLocks.LockAsync(filenames[0], context.Token))
78-
using (await Global.CodeFileOperationLocks.LockAsync(filenames[1], context.Token))
7966
//using (await Global.FileOperationAsyncLock.LockAsync())
8067
{
8168
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7

Program.cs

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

4242

4343

44-
internal static readonly AsyncLockQueueDictionary BinaryFileOperationLocks = new AsyncLockQueueDictionary();
45-
internal static readonly AsyncLockQueueDictionary CodeFileOperationLocks = new AsyncLockQueueDictionary();
44+
internal static readonly AsyncLockQueueDictionary FileOperationLocks = new AsyncLockQueueDictionary();
4645
//internal static readonly AsyncLock FileOperationAsyncLock = new AsyncLock();
4746
}
4847
#pragma warning restore S2223
@@ -432,32 +431,46 @@ public static async Task FileUpdated(string fullName, Context context)
432431
&& NeedsUpdate(fullName) //NB!
433432
)
434433
{
435-
if (
436-
fullName.EndsWith("." + Global.WatchedCodeExtension)
437-
|| Global.WatchedCodeExtension == "*"
438-
)
434+
var otherFullName = GetOtherFullName(fullName);
435+
var filenames = new List<string>()
436+
{
437+
fullName,
438+
otherFullName
439+
};
440+
441+
//NB! in order to avoid deadlocks, always take the locks in deterministic order
442+
filenames.Sort(StringComparer.InvariantCultureIgnoreCase);
443+
444+
using (await Global.FileOperationLocks.LockAsync(filenames[0], context.Token))
445+
using (await Global.FileOperationLocks.LockAsync(filenames[1], context.Token))
439446
{
440-
if (fullName.ToUpperInvariant().StartsWith(Global.AsyncPath.ToUpperInvariant()))
441-
{
442-
await AsyncToSyncConverter.AsyncFileUpdated(fullName, context);
443-
}
444-
else if (fullName.ToUpperInvariant().StartsWith(Global.SyncPath.ToUpperInvariant())) //NB!
447+
if (
448+
fullName.EndsWith("." + Global.WatchedCodeExtension)
449+
|| Global.WatchedCodeExtension == "*"
450+
)
445451
{
446-
await SyncToAsyncConverter.SyncFileUpdated(fullName, context);
452+
if (fullName.ToUpperInvariant().StartsWith(Global.AsyncPath.ToUpperInvariant()))
453+
{
454+
await AsyncToSyncConverter.AsyncFileUpdated(fullName, context);
455+
}
456+
else if (fullName.ToUpperInvariant().StartsWith(Global.SyncPath.ToUpperInvariant())) //NB!
457+
{
458+
await SyncToAsyncConverter.SyncFileUpdated(fullName, context);
459+
}
460+
else
461+
{
462+
throw new ArgumentException("fullName");
463+
}
447464
}
448-
else
465+
else //Assume ResX file
449466
{
450-
throw new ArgumentException("fullName");
451-
}
452-
}
453-
else //Assume ResX file
454-
{
455-
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
456-
var fileData = await FileExtensions.ReadAllBytesAsync(@"\\?\" + fullName, context.Token);
457-
var originalData = fileData;
467+
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
468+
var fileData = await FileExtensions.ReadAllBytesAsync(@"\\?\" + fullName, context.Token);
469+
var originalData = fileData;
458470

459-
//save without transformations
460-
await ConsoleWatch.SaveFileModifications(fullName, fileData, originalData, context);
471+
//save without transformations
472+
await ConsoleWatch.SaveFileModifications(fullName, fileData, originalData, context);
473+
}
461474
}
462475
}
463476
}
@@ -773,28 +786,15 @@ public static async Task SaveFileModifications(string fullName, byte[] fileData,
773786
|| !FileExtensions.BinaryEqual(otherFileData, fileData)
774787
)
775788
{
776-
var filenames = new List<string>()
777-
{
778-
fullName,
779-
otherFullName
780-
};
781-
782-
//NB! in order to avoid deadlocks, always take the locks in deterministic order
783-
filenames.Sort(StringComparer.InvariantCultureIgnoreCase);
784-
785-
using (await Global.BinaryFileOperationLocks.LockAsync(filenames[0], context.Token))
786-
using (await Global.BinaryFileOperationLocks.LockAsync(filenames[1], context.Token))
787-
{
788-
await DeleteFile(otherFullName, context);
789+
await DeleteFile(otherFullName, context);
789790

790-
Directory.CreateDirectory(Path.GetDirectoryName(otherFullName));
791+
Directory.CreateDirectory(Path.GetDirectoryName(otherFullName));
791792

792-
//@"\\?\" prefix is needed for writing to long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
793-
await FileExtensions.WriteAllBytesAsync(@"\\?\" + otherFullName, fileData, context.Token);
793+
//@"\\?\" prefix is needed for writing to long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7
794+
await FileExtensions.WriteAllBytesAsync(@"\\?\" + otherFullName, fileData, context.Token);
794795

795-
var now = DateTime.UtcNow; //NB! compute now after saving the file
796-
ConverterSavedFileDates[otherFullName] = now;
797-
}
796+
var now = DateTime.UtcNow; //NB! compute now after saving the file
797+
ConverterSavedFileDates[otherFullName] = now;
798798

799799

800800
await AddMessage(ConsoleColor.Magenta, $"Synchronised updates from file {fullName}", context);

SyncToAsyncConverter.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,6 @@ static SyncToAsyncConverter()
6161

6262
public static async Task SyncFileUpdated(string fullName, Context context)
6363
{
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.CodeFileOperationLocks.LockAsync(filenames[0], context.Token))
76-
using (await Global.CodeFileOperationLocks.LockAsync(filenames[1], context.Token))
7764
//using (await Global.FileOperationAsyncLock.LockAsync())
7865
{
7966
//@"\\?\" prefix is needed for reading from long paths: https://stackoverflow.com/questions/44888844/directorynotfoundexception-when-using-long-paths-in-net-4-7

0 commit comments

Comments
 (0)