示例#1
0
                public WeakDictionary()
                {
                    if (typeof(K) == typeof(WeakReference))
                    {
                        throw new InvalidOperationException();
                    }

                    this.cleanupSyncObject = new object();
                    storage = new Dictionary <WeakDictionary <K, V> .WeakKey, V>();
                    // kick off the cleanup process...
                    WeakDictionaryCleanupToken.CreateCleanupToken(this);
                }
示例#2
0
                public void Cleanup()
                {
                    List <WeakKey> toClean = null;

                    // First determine what items have died and can be removed...
                    lock (cleanupSyncObject)
                    {
                        foreach (var key in storage.Keys)
                        {
                            bool isAlive, needsCleanup;
                            key.GetValue(out isAlive, out needsCleanup);
                            if (!isAlive)
                            {
                                if (toClean == null)
                                {
                                    toClean = new List <WeakKey>();
                                }
                                toClean.Add(key);
                            }
                        }
                    }

                    if (toClean != null)
                    {
                        // Second go and remove them...
                        lock (cleanupSyncObject)
                        {
                            // If toClean is > some % of the total size it is probably better
                            // just to create a new dictionary and migrate some set of items over
                            // wholesale? It is unclear whether or not this is true.
                            foreach (var key in toClean)
                            {
                                storage.Remove(key);
                            }
                        }
                    }

                    WeakDictionaryCleanupToken.CreateCleanupToken(this);
                }
示例#3
0
 public static void CreateCleanupToken(WeakDictionary <K, V> storage)
 {
     // Create one of these, the work is done when it is finalized which
     //  will be at some indeterminate point in the future...
     WeakDictionaryCleanupToken token = new WeakDictionaryCleanupToken(storage);
 }