Exemple #1
0
        private bool RegisterIfNotDisposed(SyncCoordinator coordinator)
        {
            var acquired = true;
            var disposed = false;

            lock (_held)
            {
                disposed = _isDisposed;
                if (!disposed)
                {
                    _held.Add(coordinator);
                }
            }

            if (disposed)
            {
                coordinator.Release(this);
                acquired = false;
            }

            return(acquired);
        }
Exemple #2
0
        internal SyncCoordinator GetCoordinator(object syncRoot, bool create = true)
        {
            SyncCoordinator coordinator;

            lock (_coordinators)
            {
                if (!_coordinators.TryGetValue(syncRoot, out WeakReference <SyncCoordinator> coordinatorRef))
                {
                    if (!create)
                    {
                        return(null);
                    }

                    coordinator    = new SyncCoordinator(syncRoot);
                    coordinatorRef = new WeakReference <SyncCoordinator>(coordinator);
                    _coordinators.Add(syncRoot, coordinatorRef);
                }
                else if (!coordinatorRef.TryGetTarget(out coordinator))
                {
                    if (create)
                    {
                        coordinator             = new SyncCoordinator(syncRoot);
                        coordinatorRef          = new WeakReference <SyncCoordinator>(coordinator);
                        _coordinators[syncRoot] = coordinatorRef;
                    }
                    else
                    {
                        _coordinators.Remove(syncRoot);
                    }

                    RemoveDeadCoordinators();
                }
            }

            return(coordinator);
        }