QuickImage – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Threading;
7 using System.Threading.Tasks;
8  
9 namespace QuickImage.Utilities
10 {
11 public class FileMutex
12 {
13 private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks;
14  
15 public FileMutex()
16 {
17 _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
18 }
19  
20 private bool Create(string fileName)
21 {
22 return _locks.TryAdd(fileName, new SemaphoreSlim(1, 1));
23 }
24  
25 public SemaphoreSlim Lock(string fileName)
26 {
27 Create(fileName);
28  
29 return _locks[fileName];
30 }
31  
32 public SemaphoreSlim this[string fileName] => Lock(fileName);
33 }
34 }