[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[mono.git] / mcs / class / corlib / System.Runtime.Serialization / SerializationCallbacks.cs
index 7df6ec80b41258fe6e021c695f22faf254d4a28d..49fbb1270a182e59e237c5fd1275166a0575d776 100644 (file)
@@ -26,8 +26,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System;
 using System.Collections;
 using System.Reflection;
@@ -137,20 +135,26 @@ namespace System.Runtime.Serialization {
                }
 
                static Hashtable cache = new Hashtable ();
-
+               static object cache_lock = new object ();
+               
                public static SerializationCallbacks GetSerializationCallbacks (Type t)
                {
-                       lock (cache.SyncRoot) {
+                       SerializationCallbacks sc = (SerializationCallbacks) cache [t];
+                       if (sc != null)
+                               return sc;
 
-                               SerializationCallbacks sc = (SerializationCallbacks)  cache [t];
+                       // Slow path, new entry, we need to copy
+                       lock (cache_lock){
+                               sc = (SerializationCallbacks)  cache [t];
                                if (sc == null) {
+                                       Hashtable copy = (Hashtable) cache.Clone ();
+                               
                                        sc = new SerializationCallbacks (t);
-                                       cache [t] = sc;
+                                       copy [t] = sc;
+                                       cache = copy;
                                }
                                return sc;
                        }
                }
        }
 }
-
-#endif