Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
CloseableThreadLocal-old.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Util
21 {
22 
23  /// <summary>Java's builtin ThreadLocal has a serious flaw:
24  /// it can take an arbitrarily long amount of time to
25  /// dereference the things you had stored in it, even once the
26  /// ThreadLocal instance itself is no longer referenced.
27  /// This is because there is single, master map stored for
28  /// each thread, which all ThreadLocals share, and that
29  /// master map only periodically purges "stale" entries.
30  ///
31  /// While not technically a memory leak, because eventually
32  /// the memory will be reclaimed, it can take a long time
33  /// and you can easily hit OutOfMemoryError because from the
34  /// GC's standpoint the stale entries are not reclaimaible.
35  ///
36  /// This class works around that, by only enrolling
37  /// WeakReference values into the ThreadLocal, and
38  /// separately holding a hard reference to each stored
39  /// value. When you call {@link #close}, these hard
40  /// references are cleared and then GC is freely able to
41  /// reclaim space by objects stored in it.
42  /// </summary>
43 
44  public class CloseableThreadLocal
45  {
46 
47  [ThreadStatic]
48  static SupportClass.WeakHashTable slots;
49 
50  public /*protected internal*/ virtual System.Object InitialValue()
51  {
52  return null;
53  }
54 
55  public virtual System.Object Get()
56  {
57  object value;
58 
59  if (slots == null)
60  {
61  value = InitialValue();
62  if (value != null)
63  Set(value);
64 
65  return value;
66  }
67 
68  if (slots.ContainsKey(this))
69  {
70  return slots[this];
71  }
72  else
73  {
74  value = InitialValue();
75  slots[this] = value;
76  return value;
77  }
78  }
79 
80  public virtual void Set(System.Object object_Renamed)
81  {
82  //+-- For Debuging
83  if (SupportClass.CloseableThreadLocalProfiler.EnableCloseableThreadLocalProfiler == true)
84  {
85  lock (SupportClass.CloseableThreadLocalProfiler.Instances)
86  {
87  SupportClass.CloseableThreadLocalProfiler.Instances.Add(new WeakReference(object_Renamed));
88  }
89  }
90  //+--
91 
92  if (slots == null)
93  slots = new SupportClass.WeakHashTable();
94 
95  slots[this] = object_Renamed;
96  }
97 
98  public virtual void Close()
99  {
100  if(slots != null)
101  slots.Remove(this);
102  }
103  }
104 }