forked from Unity-Technologies/ClusterDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedMutex.cs
More file actions
41 lines (34 loc) · 947 Bytes
/
Copy pathSharedMutex.cs
File metadata and controls
41 lines (34 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Threading;
namespace Unity.ClusterDisplay.Editor.SourceGenerators
{
public class SharedMutex : IDisposable
{
readonly string k_MutexName;
Mutex mutex = null;
public SharedMutex(string mutexName) => this.k_MutexName = mutexName;
public void Lock ()
{
try
{
if (mutex == null)
{
mutex = Mutex.OpenExisting(k_MutexName);
}
while (!mutex.WaitOne()) {}
}
catch
{
mutex = new Mutex(true, k_MutexName, out var isOwned);
while (!isOwned && !mutex.WaitOne()) {}
}
}
public void Release () => mutex.ReleaseMutex();
public void Dispose()
{
mutex.Close();
mutex.Dispose();
mutex = null;
}
}
}