public async Task <ILock> LockAsync(string ck, int ttl = 5000, int retry = 2, int retryDelay = 1000)
    {
        var lck = new Lock(this, ck, ttl);

        retry = retry < 0 ? 0 : retry;

        for (var i = 0; i <= retry; i++)
        {
            var tmp = kvs.GetOrAdd(ck, lck);
            if (!tmp.IsAvailable)
            {
                tmp.Dispose();
                i--;
            }
            else if (!ReferenceEquals(tmp, lck))
            {
                await Task.Delay(retryDelay).ConfigureAwait(false);
            }
            else
            {
                lck.SetTimeout();
                return(lck);
            }
        }

        lck.Dispose();
        return(lck);
    }
Exemplo n.º 2
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         DelegateLock.Dispose();
         OuterInstance.Dir.OpenLocks.Remove(Name);
     }
 }
Exemplo n.º 3
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         delegateLock.Dispose();
         outerInstance.dir.openLocks.Remove(name);
     }
 }
 public void Dispose()
 {
     if (_lockHandle != null && _lockHandle.LockTaken)
     {
         _lockHandle.Dispose();
         _repository._isFileSystemWatcherSuppressed = false;
     }
 }
        /// <summary>
        /// Sends contending to the remote storage if the item is modified.
        /// If Auto-locking is enabled, automatically locks the file if not locked. Unlocks the file after the update if auto-locked.
        /// </summary>
        public async Task UpdateAsync()
        {
            Lock fileLock = null;

            try
            {
                if (!PlaceholderItem.GetItem(userFileSystemPath).GetInSync())
                {
                    // Lock file if auto-locking is enabled.
                    if (Config.Settings.AutoLock)
                    {
                        // Get existing lock or create a new lock.
                        fileLock = await LockAsync(FileMode.OpenOrCreate, LockMode.Auto);
                    }

                    ServerLockInfo lockInfo = fileLock != null ? await fileLock.GetLockInfoAsync() : null;

                    // Update item in remote storage.
                    logger.LogMessage("Sending to remote storage", userFileSystemPath);
                    await CreateOrUpdateAsync(FileMode.Open, lockInfo);

                    //await new UserFileSystemRawItem(userFileSystemPath).ClearStateAsync();
                    logger.LogMessage("Sent to remote storage succesefully", userFileSystemPath);
                }

                // Unlock if auto-locked.
                if (await Lock.GetLockModeAsync(userFileSystemPath) == LockMode.Auto)
                {
                    if (fileLock == null)
                    {
                        // Get existing lock.
                        fileLock = await Lock.LockAsync(userFileSystemPath, FileMode.Open, LockMode.None, logger);
                    }
                    await UnlockAsync(fileLock);
                }
            }
            catch (ClientLockFailedException ex)
            {
                // Failed to lock file. Possibly blocked from another thread. This is a normal behaviour.
                logger.LogMessage(ex.Message, userFileSystemPath);
            }
            catch (Exception ex)
            {
                // Some error when locking, updating or unlocking occured.
                await new UserFileSystemRawItem(userFileSystemPath).SetUploadErrorStateAsync(ex);

                // Rethrow the exception preserving stack trace of the original exception.
                System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw();
            }
            finally
            {
                if (fileLock != null)
                {
                    fileLock.Dispose();
                }
            }
        }
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             Lock?.Dispose();
             Lock = null;
             Timer?.Dispose();
             Timer = null;
         }
         disposedValue = true;
     }
 }
Exemplo n.º 7
0
        protected virtual async ValueTask DisposeAsync(bool disposing)
        {
            await _taskTimerLock.TryLockAsync(
                () =>
            {
                if (_taskTimer != null)
                {
                    _taskTimer.Dispose();
                    _taskTimer = null;
                }
            },
                5000);

            _taskTimerLock.Dispose();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (!_isDisposed)
                    {
                        bool disposeLock = false;
                        ComposablePartCatalogCollection?catalogs = null;

                        try
                        {
                            using (new WriteLock(_thisLock))
                            {
                                if (!_isDisposed)
                                {
                                    disposeLock        = true;
                                    catalogs           = _catalogCollection;
                                    _catalogCollection = null !;
                                    _assemblyCatalogs  = null !;
                                    _isDisposed        = true;
                                }
                            }
                        }
                        finally
                        {
                            if (catalogs != null)
                            {
                                catalogs.Dispose();
                            }

                            if (disposeLock)
                            {
                                _thisLock.Dispose();
                            }
                        }
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Exemplo n.º 9
0
        protected virtual void Dispose(bool disposing)
        {
            if (Disposed)
            {
                return;
            }

            Disposed = true;

            if (!disposing)
            {
                return;
            }

            Writer.Flush();
            Writer.Close();
            Writer.Dispose();

            Lock.Dispose();
        }
Exemplo n.º 10
0
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!_isDisposed)
                {
                    bool disposeLock = false;
                    IEnumerable <ComposablePartCatalog>?catalogs = null;
                    try
                    {
                        using (new WriteLock(_lock))
                        {
                            if (!_isDisposed)
                            {
                                disposeLock = true;

                                catalogs  = _catalogs;
                                _catalogs = null !;

                                _isDisposed = true;
                            }
                        }
                    }
                    finally
                    {
                        if (catalogs != null)
                        {
                            UnsubscribeFromCatalogNotifications(catalogs);
                            catalogs.ForEach(catalog => catalog.Dispose());
                        }

                        if (disposeLock)
                        {
                            _lock.Dispose();
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
        public async ValueTask DisposeAsync()
        {
            try
            {
                await StopWorkerAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                // Don't throw inside dispose
                _options.DiagnosticLogger?.LogError(
                    "Error stopping worker during dispose.",
                    ex
                    );
            }

            _workerSignal.Dispose();
            _workerCts.Dispose();
            _worker.Dispose();
            _cacheDirectoryLock.Dispose();

            (_innerTransport as IDisposable)?.Dispose();
        }
Exemplo n.º 12
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    Lock disposeLock = null;

                    if (!this._isDisposed)
                    {
                        using (new WriteLock(this._lock))
                        {
                            if (!this._isDisposed)
                            {
                                this._isDisposed       = true;
                                disposeLock            = this._lock;
                                this._lock             = null;
                                this._packages         = null;
                                this._loadedAssemblies = null;
                                this._parts            = null;
                                this._partsQuery       = null;
                            }
                        }
                    }

                    if (disposeLock != null)
                    {
                        disposeLock.Dispose();
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Exemplo n.º 13
0
 public void Dispose()
 {
     Lock.Dispose();
 }
Exemplo n.º 14
0
 public void Dispose()
 {
     Lock.Dispose();
     Lock = null;
     ObjectRenewalLogic = null;
 }
Exemplo n.º 15
0
 /// <summary>
 /// 释放锁节点
 /// </summary>
 /// <returns></returns>
 internal override Node RunTask()
 {
     Lock.Dispose();
     return(LinkNext);
 }
Exemplo n.º 16
0
 /// <summary>
 /// 释放锁节点
 /// </summary>
 public override void RunTask()
 {
     Lock.Dispose();
 }
Exemplo n.º 17
0
 /// <summary>
 /// 释放锁
 /// </summary>
 public void Dispose()
 {
     _lock.Dispose();
 }