Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/03/11 00:46:55 (14 years ago)
Author:
swagner
Message:

Merged ParallelEngine branch back into trunk (#1333)

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Operators/3.3/Operator.cs

    r4722 r5193  
    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    }
    56 
    57     /// <summary>
    58     /// Flag whether the current instance has been canceled.
    59     /// </summary>
    60     private bool canceled;
    61     /// <inheritdoc/>
    62     protected bool Canceled {
    63       get { return canceled; }
    64       private set {
    65         if (value != canceled) {
    66           canceled = value;
    67           OnCanceledChanged();
    68         }
    69       }
     55    private CancellationToken cancellationToken;
     56    protected CancellationToken CancellationToken {
     57      get { return cancellationToken; }
    7058    }
    7159
    7260    [Storable]
    7361    private bool breakpoint;
    74     /// <inheritdoc/>
    75     /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
    7662    public bool Breakpoint {
    7763      get { return breakpoint; }
     
    8672
    8773    [StorableConstructor]
    88     protected Operator(bool deserializing) : base(deserializing) { }
     74    protected Operator(bool deserializing)
     75      : base(deserializing) {
     76      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     77    }
    8978    protected Operator(Operator original, Cloner cloner)
    9079      : base(original, cloner) {
    91       this.canceled = original.canceled;
     80      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    9281      this.breakpoint = original.breakpoint;
    93       this.executionContext = cloner.Clone<IExecutionContext>(original.executionContext);
    9482    }
    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>
    9983    protected Operator()
    10084      : base() {
    101       canceled = false;
     85      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    10286      breakpoint = false;
    10387    }
    10488    protected Operator(string name)
    10589      : base(name) {
    106       canceled = false;
     90      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    10791      breakpoint = false;
    10892    }
    10993    protected Operator(string name, ParameterCollection parameters)
    11094      : base(name, parameters) {
    111       canceled = false;
     95      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    11296      breakpoint = false;
    11397    }
    11498    protected Operator(string name, string description)
    11599      : base(name, description) {
    116       canceled = false;
     100      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    117101      breakpoint = false;
    118102    }
    119103    protected Operator(string name, string description, ParameterCollection parameters)
    120104      : base(name, description, parameters) {
    121       canceled = false;
     105      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    122106      breakpoint = false;
    123107    }
    124108
    125     /// <inheritdoc/>
    126     public virtual IOperation Execute(IExecutionContext context) {
     109    public virtual IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
    127110      try {
    128         Canceled = false;
    129111        ExecutionContext = context;
     112        this.cancellationToken = cancellationToken;
    130113        foreach (IParameter param in Parameters)
    131114          param.ExecutionContext = context;
     
    140123      }
    141124    }
    142     /// <inheritdoc/>
    143     /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
    144     public void Abort() {
    145       Canceled = true;
    146     }
    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>
    152125    public abstract IOperation Apply();
    153126
    154     protected virtual void OnExecutionContextChanged() { }
    155     protected virtual void OnCanceledChanged() { }
    156     /// <inheritdoc/>
    157127    public event EventHandler BreakpointChanged;
    158     /// <summary>
    159     /// Fires a new <c>BreakpointChanged</c> event.
    160     /// </summary>
    161128    protected virtual void OnBreakpointChanged() {
    162129      if (BreakpointChanged != null) {
     
    164131      }
    165132    }
    166     /// <inheritdoc/>
    167133    public event EventHandler Executed;
    168     /// <summary>
    169     /// Fires a new <c>Executed</c> event.
    170     /// </summary>
    171134    protected virtual void OnExecuted() {
    172135      if (Executed != null) {
Note: See TracChangeset for help on using the changeset viewer.