Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/30/10 02:13:02 (14 years ago)
Author:
swagner
Message:

Stored execution contexts locally for each thread (#1333)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ParallelEngine/HeuristicLab.Parameters/3.3/Parameter.cs

    r5178 r5183  
    2222using System;
    2323using System.Drawing;
     24using System.Threading;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using System.Collections.Generic;
    28 using System.Threading;
    2928
    3029namespace HeuristicLab.Parameters {
     
    5554      get { return dataType; }
    5655    }
    57     protected Dictionary<int, IItem> cachedActualValues;
     56
     57    private Lazy<ThreadLocal<IItem>> cachedActualValues;
    5858    public IItem ActualValue {
    5959      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;
    7362      }
    7463      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;
    8065        SetActualValue(value);
    8166      }
    8267    }
    83     private Dictionary<int, IExecutionContext> executionContexts;
     68    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
    8469    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; }
    9371      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;
    10875        }
    10976      }
     
    11178
    11279    [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);
    11684    }
    11785    protected Parameter(Parameter original, Cloner cloner)
    11886      : base(original, cloner) {
    11987      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);
    12590    }
    12691    protected Parameter()
    12792      : base("Anonymous") {
    12893      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);
    13196    }
    13297    protected Parameter(string name, Type dataType)
     
    13499      if (dataType == null) throw new ArgumentNullException();
    135100      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);
    138103    }
    139104    protected Parameter(string name, string description, Type dataType)
     
    141106      if (dataType == null) throw new ArgumentNullException();
    142107      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);
    145110    }
    146111
Note: See TracChangeset for help on using the changeset viewer.