QuickImage – Rev 1

Subversion Repositories:
Rev:
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace QuickImage.Utilities
{
    public class FileMutex
    {
        private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks;

        public FileMutex()
        {
            _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
        }

        private bool Create(string fileName)
        {
            return _locks.TryAdd(fileName, new SemaphoreSlim(1, 1));
        }

        public SemaphoreSlim Lock(string fileName)
        {
            Create(fileName);

            return _locks[fileName];
        }

        public SemaphoreSlim this[string fileName] => Lock(fileName);
    }
}