Esempio n. 1
0
        public IReference <TValue> GetOrCreate(TKey key, Func <TKey, TValue> factory)
        {
            lock (_lock)
            {
                if (_sources.TryGetValue(key, out var source) == false)
                {
                    source = new ReferenceSource <TValue>(factory(key), s =>
                    {
                        lock (_lock)
                        {
                            _sources.Remove(key);
                        }
                    });
                    _sources.Add(key, source);
                }

                if (!source.TryCreateReference(out var reference))
                {
                    //source is disposed so we can remove it
                    _sources.Remove(key);
                    return(GetOrCreate(key, factory));
                }

                return(reference);
            }
        }
Esempio n. 2
0
 public Reference(ReferenceSource <T> source)
 {
     _source = source;
     Interlocked.Increment(ref _source._refCounter);
 }