-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix file locking problem on Chorus Notes (LT-20807)
* In https://jira.sil.org/browse/LT-20807 the problem seems to come up when multiple processes are trying to write the file +semver:minor
- Loading branch information
1 parent
6f2d87e
commit bc62c4a
Showing
4 changed files
with
160 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Threading; | ||
using SIL.PlatformUtilities; | ||
|
||
namespace Chorus.notes | ||
{ | ||
/// <summary> | ||
/// This factory class creates a cross process mutex for locking Annotation (ChorusNotes) write operations | ||
/// </summary> | ||
internal class AnnotationMutexFactory | ||
{ | ||
public static IAnnotationMutex Create(string annotationFilePath) | ||
{ | ||
var mutexName = $"ChorusAnnotationRepositoryMutex_{annotationFilePath.GetHashCode()}"; | ||
if (Platform.IsWindows) | ||
{ | ||
return new WinMutex(false, mutexName); | ||
} | ||
return new LinuxMutex(mutexName); | ||
} | ||
|
||
/// <summary> | ||
/// This class will create a named mutex which locks across processes | ||
/// </summary> | ||
private class WinMutex : IAnnotationMutex | ||
{ | ||
private readonly Mutex _mutex; | ||
|
||
public WinMutex(bool initiallyOwned, string mutexName) | ||
{ | ||
_mutex = new Mutex(initiallyOwned, mutexName); | ||
} | ||
|
||
public void WaitOne() | ||
{ | ||
_mutex.WaitOne(); | ||
} | ||
|
||
public void ReleaseMutex() | ||
{ | ||
_mutex.ReleaseMutex(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Currently just a no-op interface implementation since named mutexes are not implemented in Mono | ||
/// Enhance: implement a platform appropriate cross process file locking mechanism | ||
/// </summary> | ||
private class LinuxMutex: IAnnotationMutex | ||
{ | ||
public LinuxMutex(string mutexName) | ||
{ | ||
} | ||
|
||
public void WaitOne() | ||
{ | ||
} | ||
|
||
public void ReleaseMutex() | ||
{ | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Chorus.notes | ||
{ | ||
/// <summary> | ||
/// This interface wraps a named mutex for locking annotation files so we can have platform specific implementations | ||
/// </summary> | ||
public interface IAnnotationMutex | ||
{ | ||
void WaitOne(); | ||
void ReleaseMutex(); | ||
} | ||
} |
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