Skip to content

Commit 90b3766

Browse files
committed
refactor: use lock file instead of named mutex since the second one may not work on other platforms
Signed-off-by: leo <[email protected]>
1 parent 9ebde19 commit 90b3766

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/Models/IpcChannel.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@ public IpcChannel()
1919
{
2020
try
2121
{
22-
_singletonMutex = new Mutex(false, "SourceGit_2994509B-4906-4A48-9A45-55C1836A8208", out _isFirstInstance);
23-
24-
if (_isFirstInstance)
25-
{
26-
_server = new NamedPipeServerStream(
27-
"SourceGitIPCChannel",
28-
PipeDirection.In,
29-
-1,
30-
PipeTransmissionMode.Byte,
31-
PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);
32-
_cancellationTokenSource = new CancellationTokenSource();
33-
Task.Run(StartServer);
34-
}
22+
_singletoneLock = File.Open(Path.Combine(Native.OS.DataDir, "process.lock"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
23+
_isFirstInstance = true;
24+
_server = new NamedPipeServerStream(
25+
"SourceGitIPCChannel",
26+
PipeDirection.In,
27+
-1,
28+
PipeTransmissionMode.Byte,
29+
PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);
30+
_cancellationTokenSource = new CancellationTokenSource();
31+
Task.Run(StartServer);
3532
}
3633
catch
3734
{
38-
// IGNORE
35+
_isFirstInstance = false;
3936
}
4037
}
4138

@@ -70,7 +67,7 @@ public void SendToFirstInstance(string cmd)
7067
public void Dispose()
7168
{
7269
_cancellationTokenSource?.Cancel();
73-
_singletonMutex.Dispose();
70+
_singletoneLock?.Dispose();
7471
}
7572

7673
private async void StartServer()
@@ -99,7 +96,7 @@ private async void StartServer()
9996
}
10097
}
10198

102-
private Mutex _singletonMutex = null;
99+
private FileStream _singletoneLock = null;
103100
private bool _isFirstInstance = false;
104101
private NamedPipeServerStream _server = null;
105102
private CancellationTokenSource _cancellationTokenSource = null;

0 commit comments

Comments
 (0)