Changeset 5183 for branches/ParallelEngine/HeuristicLab.Parameters
- Timestamp:
- 12/30/10 02:13:02 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ParallelEngine/HeuristicLab.Parameters/3.3/Parameter.cs
r5178 r5183 22 22 using System; 23 23 using System.Drawing; 24 using System.Threading; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using System.Collections.Generic;28 using System.Threading;29 28 30 29 namespace HeuristicLab.Parameters { … … 55 54 get { return dataType; } 56 55 } 57 protected Dictionary<int, IItem> cachedActualValues; 56 57 private Lazy<ThreadLocal<IItem>> cachedActualValues; 58 58 public IItem ActualValue { 59 59 get { 60 int id = Thread.CurrentThread.ManagedThreadId; 61 IItem value = null; 62 lock (cachedActualValues) { 63 cachedActualValues.TryGetValue(id, out value); 64 } 65 if (value == null) { 66 value = GetActualValue(); 67 lock (cachedActualValues) { 68 if (cachedActualValues.ContainsKey(id)) cachedActualValues[id] = value; 69 else cachedActualValues.Add(id, value); 70 } 71 } 72 return value; 60 if (cachedActualValues.Value.Value == null) cachedActualValues.Value.Value = GetActualValue(); 61 return cachedActualValues.Value.Value; 73 62 } 74 63 set { 75 int id = Thread.CurrentThread.ManagedThreadId; 76 lock (cachedActualValues) { 77 if (cachedActualValues.ContainsKey(id)) cachedActualValues[id] = value; 78 else cachedActualValues.Add(id, value); 79 } 64 cachedActualValues.Value.Value = value; 80 65 SetActualValue(value); 81 66 } 82 67 } 83 private Dictionary<int, IExecutionContext> executionContexts;68 private Lazy<ThreadLocal<IExecutionContext>> executionContexts; 84 69 public IExecutionContext ExecutionContext { 85 get { 86 int id = Thread.CurrentThread.ManagedThreadId; 87 IExecutionContext context = null; 88 lock (executionContexts) { 89 executionContexts.TryGetValue(id, out context); 90 } 91 return context; 92 } 70 get { return executionContexts.Value.Value; } 93 71 set { 94 IExecutionContext context = null; 95 int id = Thread.CurrentThread.ManagedThreadId; 96 lock (executionContexts) { 97 executionContexts.TryGetValue(id, out context); 98 if (value != context) { 99 if (context == null) executionContexts.Add(id, value); 100 else if (value == null) { 101 executionContexts.Remove(id); 102 lock (cachedActualValues) cachedActualValues.Remove(id); 103 } else { 104 executionContexts[id] = value; 105 lock (cachedActualValues) cachedActualValues.Remove(id); 106 } 107 } 72 if (value != executionContexts.Value.Value) { 73 executionContexts.Value.Value = value; 74 cachedActualValues.Value.Value = null; 108 75 } 109 76 } … … 111 78 112 79 [StorableConstructor] 113 protected Parameter(bool deserializing) : base(deserializing) { 114 cachedActualValues = new Dictionary<int, IItem>(); 115 executionContexts = new Dictionary<int, IExecutionContext>(); 80 protected Parameter(bool deserializing) 81 : base(deserializing) { 82 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 83 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 116 84 } 117 85 protected Parameter(Parameter original, Cloner cloner) 118 86 : base(original, cloner) { 119 87 dataType = original.dataType; 120 cachedActualValues = new Dictionary<int, IItem>(); 121 executionContexts = new Dictionary<int, IExecutionContext>(); 122 foreach (var item in original.executionContexts) { 123 executionContexts.Add(item.Key, cloner.Clone(item.Value)); 124 } 88 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 89 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 125 90 } 126 91 protected Parameter() 127 92 : base("Anonymous") { 128 93 dataType = typeof(IItem); 129 cachedActualValues = new Dictionary<int, IItem>();130 executionContexts = new Dictionary<int, IExecutionContext>();94 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 95 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 131 96 } 132 97 protected Parameter(string name, Type dataType) … … 134 99 if (dataType == null) throw new ArgumentNullException(); 135 100 this.dataType = dataType; 136 cachedActualValues = new Dictionary<int, IItem>();137 executionContexts = new Dictionary<int, IExecutionContext>();101 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 102 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 138 103 } 139 104 protected Parameter(string name, string description, Type dataType) … … 141 106 if (dataType == null) throw new ArgumentNullException(); 142 107 this.dataType = dataType; 143 cachedActualValues = new Dictionary<int, IItem>();144 executionContexts = new Dictionary<int, IExecutionContext>();108 cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 109 executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication); 145 110 } 146 111
Note: See TracChangeset
for help on using the changeset viewer.