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.Operators/3.3/Operator.cs

    r5178 r5183  
    2222using System;
    2323using System.Drawing;
     24using System.Threading;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    2829namespace HeuristicLab.Operators {
    2930  /// <summary>
    30   /// The base class for all operators.
     31  /// Base class for operators.
    3132  /// </summary>
    3233  [Item("Operator", "Base class for operators.")]
     
    4344    }
    4445
    45     [Storable]
    46     private IExecutionContext executionContext;
     46    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
    4747    protected IExecutionContext ExecutionContext {
    48       get { return executionContext; }
     48      get { return executionContexts.Value.Value; }
    4949      private set {
    50         if (value != executionContext) {
    51           executionContext = value;
    52           OnExecutionContextChanged();
     50        if (value != executionContexts.Value.Value) {
     51          executionContexts.Value.Value = value;
    5352        }
    5453      }
    5554    }
    5655
    57     /// <summary>
    58     /// Flag whether the current instance has been canceled.
    59     /// </summary>
    6056    private bool canceled;
    61     /// <inheritdoc/>
    6257    protected bool Canceled {
    6358      get { return canceled; }
     
    7267    [Storable]
    7368    private bool breakpoint;
    74     /// <inheritdoc/>
    75     /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
    7669    public bool Breakpoint {
    7770      get { return breakpoint; }
     
    8679
    8780    [StorableConstructor]
    88     protected Operator(bool deserializing) : base(deserializing) { }
     81    protected Operator(bool deserializing)
     82      : base(deserializing) {
     83      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     84      canceled = false;
     85    }
    8986    protected Operator(Operator original, Cloner cloner)
    9087      : base(original, cloner) {
     88      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    9189      this.canceled = original.canceled;
    9290      this.breakpoint = original.breakpoint;
    93       this.executionContext = cloner.Clone<IExecutionContext>(original.executionContext);
    9491    }
    95     /// <summary>
    96     /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
    97     /// the canceled flag to <c>false</c> and the name of the operator to the type name.
    98     /// </summary>
    9992    protected Operator()
    10093      : base() {
     94      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    10195      canceled = false;
    10296      breakpoint = false;
     
    10498    protected Operator(string name)
    10599      : base(name) {
     100      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    106101      canceled = false;
    107102      breakpoint = false;
     
    109104    protected Operator(string name, ParameterCollection parameters)
    110105      : base(name, parameters) {
     106      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    111107      canceled = false;
    112108      breakpoint = false;
     
    114110    protected Operator(string name, string description)
    115111      : base(name, description) {
     112      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    116113      canceled = false;
    117114      breakpoint = false;
     
    119116    protected Operator(string name, string description, ParameterCollection parameters)
    120117      : base(name, description, parameters) {
     118      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    121119      canceled = false;
    122120      breakpoint = false;
    123121    }
    124122
    125     /// <inheritdoc/>
    126123    public virtual IOperation Execute(IExecutionContext context) {
    127124      try {
     
    140137      }
    141138    }
    142     /// <inheritdoc/>
    143     /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
    144139    public void Abort() {
    145140      Canceled = true;
    146141    }
    147     /// <summary>
    148     /// Performs the current operator on the specified <paramref name="scope"/>.
    149     /// </summary>
    150     /// <param name="scope">The scope where to execute the operator</param>
    151     /// <returns><c>null</c>.</returns>
    152142    public abstract IOperation Apply();
    153143
    154     protected virtual void OnExecutionContextChanged() { }
    155144    protected virtual void OnCanceledChanged() { }
    156     /// <inheritdoc/>
    157145    public event EventHandler BreakpointChanged;
    158     /// <summary>
    159     /// Fires a new <c>BreakpointChanged</c> event.
    160     /// </summary>
    161146    protected virtual void OnBreakpointChanged() {
    162147      if (BreakpointChanged != null) {
     
    164149      }
    165150    }
    166     /// <inheritdoc/>
    167151    public event EventHandler Executed;
    168     /// <summary>
    169     /// Fires a new <c>Executed</c> event.
    170     /// </summary>
    171152    protected virtual void OnExecuted() {
    172153      if (Executed != null) {
Note: See TracChangeset for help on using the changeset viewer.