예제 #1
0
        /// <summary>
        /// Enter in Shared lock mode.
        /// </summary>
        public LockControl Read()
        {
            // if read or write
            if (_thread.IsReadLockHeld || _thread.IsWriteLockHeld)
            {
                return(new LockControl(false, () => { }));
            }

            // try enter in read mode
            if (!_thread.TryEnterReadLock(_timeout))
            {
                throw LiteException.LockTimeout(_timeout);
            }

            _log.Write(Logger.LOCK, "entered in read lock mode in thread #{0}", this.ThreadId);

            // lock disk in shared mode
            var position = _disk.Lock(LockState.Read, _timeout);

            var changed = this.DetectDatabaseChanges();

            return(new LockControl(changed, () =>
            {
                // exit disk lock mode
                _disk.Unlock(LockState.Read, position);

                // exit thread lock mode
                _thread.ExitReadLock();

                _log.Write(Logger.LOCK, "exited read lock mode in thread #{0}", this.ThreadId);
            }));
        }
예제 #2
0
        /// <summary>
        /// Try execute some action while has lock exception
        /// </summary>
        public static void TryExec(Action action, TimeSpan timeout)
        {
            var timer = DateTime.UtcNow.Add(timeout);

            do
            {
                try
                {
                    action();
                    return;
                }
                catch (IOException ex)
                {
                    ex.WaitIfLocked(25);
                }
            }while (DateTime.UtcNow < timer);

            throw LiteException.LockTimeout(timeout);
        }
예제 #3
0
        /// <summary>
        /// Enter in Exclusive lock mode
        /// </summary>
        public LockControl Write()
        {
            // if already in exclusive, do nothing
            if (_thread.IsWriteLockHeld)
            {
                return(new LockControl(false, () => { }));
            }

            // let's test if is not in read lock
            if (_thread.IsReadLockHeld)
            {
                throw new NotSupportedException("Not support Write lock inside a Read lock");
            }

            // try enter in write mode (thread)
            if (!_thread.TryEnterWriteLock(_timeout))
            {
                throw LiteException.LockTimeout(_timeout);
            }

            _log.Write(Logger.LOCK, "entered in write lock mode in thread #{0}", this.ThreadId);

            // try enter in exclusive mode in disk
            var position = _disk.Lock(LockState.Write, _timeout);

            // call avoid dirty only if not came from a shared mode
            var changed = this.DetectDatabaseChanges();

            return(new LockControl(changed, () =>
            {
                // release disk write
                _disk.Unlock(LockState.Write, position);

                // release thread write
                _thread.ExitWriteLock();

                _log.Write(Logger.LOCK, "exited write lock mode in thread #{0}", this.ThreadId);
            }));
        }