crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/SerializedOperations/SerializedOperation.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Collections.Generic;
       
    19 using System.Text;
       
    20 using System.Threading;
       
    21 using System.ComponentModel;
       
    22 
       
    23 namespace SymbianUtils.SerializedOperations
       
    24 {
       
    25     public abstract class SerializedOperation : DisposableObject, IComparable<SerializedOperation>
       
    26     {
       
    27         #region Delegates & events
       
    28         public delegate void EventHandler( SerializedOperation aOperation );
       
    29         public event EventHandler Started;
       
    30         public event EventHandler Completed;
       
    31         #endregion
       
    32 
       
    33         #region Constructors
       
    34         protected SerializedOperation()
       
    35             : this( true )
       
    36         {
       
    37         }
       
    38 
       
    39         protected SerializedOperation( bool aEnabled )
       
    40         {
       
    41             Enabled = aEnabled;
       
    42             //
       
    43             iWorker.DoWork += new DoWorkEventHandler( DoWork );
       
    44         }
       
    45         #endregion
       
    46 
       
    47         #region Framework API
       
    48         protected abstract void PerformOperation();
       
    49 
       
    50         public virtual long Priority
       
    51         {
       
    52             get { return 0; }
       
    53         }
       
    54         #endregion
       
    55 
       
    56         #region API
       
    57         public void EnableAndWait()
       
    58         {
       
    59             Trace( "[{0}] EnableAndWait() - START - enabled: {1}, busy: {2}", this.GetType().Name, Enabled, IsBusy );
       
    60             System.Diagnostics.Debug.Assert( Enabled == false );
       
    61             System.Diagnostics.Debug.Assert( !IsBusy );
       
    62 
       
    63             if ( IsBusy )
       
    64             {
       
    65                 while ( IsBusy )
       
    66                 {
       
    67                     Trace( "[{0}] EnableAndWait() - still busy?: {1}", this.GetType().Name, IsBusy );
       
    68                     Thread.Sleep( 10 );
       
    69                 }
       
    70             }
       
    71             else
       
    72             {
       
    73                 SymbianUtils.SymDebug.SymDebugger.Assert( iWaitForCompletion == null );
       
    74                 using ( iWaitForCompletion = new AutoResetEvent( false ) )
       
    75                 {
       
    76                     // Enable the operation
       
    77                     Trace( "[{0}] EnableAndWait() - enabling item...", this.GetType().Name );
       
    78                     Enabled = true;
       
    79 
       
    80                     // And now block until it completes
       
    81                     Trace( "[{0}] EnableAndWait() - sleeping...", this.GetType().Name );
       
    82                     iWaitForCompletion.WaitOne();
       
    83                     Trace( "[{0}] EnableAndWait() - slept!", this.GetType().Name );
       
    84                 }
       
    85             }
       
    86 
       
    87             Trace( "[{0}] EnableAndWait() - END", this.GetType().Name );
       
    88         }
       
    89         #endregion
       
    90 
       
    91         #region Properties
       
    92         public object Tag
       
    93         {
       
    94             get { return iTag; }
       
    95             set { iTag = value; }
       
    96         }
       
    97 
       
    98         public bool IsBusy
       
    99         {
       
   100             get { return iWorker.IsBusy; }
       
   101         }
       
   102 
       
   103         public bool Enabled
       
   104         {
       
   105             get { return iEnabled; }
       
   106             set
       
   107             {
       
   108                 lock ( iEnabledLock )
       
   109                 {
       
   110                     if ( value != iEnabled )
       
   111                     {
       
   112                         iEnabled = value;
       
   113                         if ( OperationManager != null )
       
   114                         {
       
   115                             OperationManager.OnOperationEnabledStatusChanged( this );
       
   116                         }
       
   117                     }
       
   118                 }
       
   119             }
       
   120         }
       
   121 
       
   122         internal SerializedOperationManager OperationManager
       
   123         {
       
   124             get { return iManager; }
       
   125             set { iManager = value; }
       
   126         }
       
   127         #endregion
       
   128 
       
   129         #region Event handlers
       
   130         private void DoWork( object aSender, DoWorkEventArgs aArgs )
       
   131         {
       
   132             System.Diagnostics.Debug.Assert( OperationManager != null );
       
   133             NotifyStarted();
       
   134             //
       
   135             try
       
   136             {
       
   137                 PerformOperation();
       
   138             }
       
   139             catch ( Exception opException )
       
   140             {
       
   141                 OperationManager.Trace( string.Format( "OPERATION EXCEPTION MSG - op: {0} ({1}) - msg: {2}", this.ToString(), this.GetType(), opException.Message ) );
       
   142                 OperationManager.Trace( string.Format( "OPERATION EXCEPTION STK       {0}", opException.StackTrace ) );
       
   143             }
       
   144             //
       
   145             NotifyCompleted();
       
   146         }
       
   147 
       
   148         private void WorkerCompleted()
       
   149         {
       
   150             Trace( "[{0}] WorkerCompleted() - START", this.GetType().Name );
       
   151             //
       
   152             System.Diagnostics.Debug.Assert( OperationManager != null );
       
   153             OperationManager.OperationCompleted( this );
       
   154             //
       
   155             if ( iWaitForCompletion != null )
       
   156             {
       
   157                 Trace( "[{0}] WorkerCompleted() - unblocking main thread...", this.GetType().Name );
       
   158                 iWaitForCompletion.Set();
       
   159             }
       
   160             //
       
   161             Trace( "[{0}] WorkerCompleted() - END", this.GetType().Name );
       
   162         }
       
   163         #endregion
       
   164 
       
   165         #region Internal methods
       
   166         protected void Trace( string aMessage )
       
   167         {
       
   168             iManager.Trace( aMessage );
       
   169         }
       
   170 
       
   171         protected void Trace( string aFormat, params object[] aParams )
       
   172         {
       
   173             iManager.Trace( aFormat, aParams );
       
   174         }
       
   175 
       
   176         internal void Start()
       
   177         {
       
   178             Start( null );
       
   179         }
       
   180 
       
   181         internal void Start( object aParameter )
       
   182         {
       
   183             System.Diagnostics.Debug.Assert( Enabled );
       
   184             iWorker.RunWorkerAsync( aParameter );
       
   185         }
       
   186 
       
   187         private void NotifyStarted()
       
   188         {
       
   189             try
       
   190             {
       
   191                 if ( Started != null )
       
   192                 {
       
   193                     Started( this );
       
   194                 }
       
   195             }
       
   196             catch ( Exception )
       
   197             {
       
   198             }
       
   199         }
       
   200 
       
   201         private void NotifyCompleted()
       
   202         {
       
   203             try
       
   204             {
       
   205                 if ( Completed != null )
       
   206                 {
       
   207                     Completed( this );
       
   208                 }
       
   209                 try
       
   210                 {
       
   211                     WorkerCompleted();
       
   212                 }
       
   213                 catch ( Exception )
       
   214                 {
       
   215                 }
       
   216             }
       
   217             catch ( Exception )
       
   218             {
       
   219             }
       
   220         }
       
   221         #endregion
       
   222 
       
   223         #region From IComparable<SerializedOperation>
       
   224         public int CompareTo( SerializedOperation aOther )
       
   225         {
       
   226             int ret = 1;
       
   227             //
       
   228             if ( aOther != null )
       
   229             {
       
   230                 ret = this.Priority.CompareTo( aOther.Priority );
       
   231             }
       
   232             //
       
   233             return ret;
       
   234         }
       
   235         #endregion
       
   236 
       
   237         #region From DisposableObject
       
   238         protected override void CleanupManagedResources()
       
   239         {
       
   240             try
       
   241             {
       
   242                 base.CleanupManagedResources();
       
   243             }
       
   244             finally
       
   245             {
       
   246                 if ( iWaitForCompletion != null )
       
   247                 {
       
   248                     iWaitForCompletion.Close();
       
   249                     iWaitForCompletion = null;
       
   250                 }
       
   251                 if ( iWorker != null )
       
   252                 {
       
   253                     iWorker.Dispose();
       
   254                     iWorker = null;
       
   255                 }
       
   256             }
       
   257         }
       
   258         #endregion
       
   259 
       
   260         #region From System.Object
       
   261         public override string ToString()
       
   262         {
       
   263             return this.GetType().Name;
       
   264         }
       
   265         #endregion
       
   266 
       
   267         #region Data members
       
   268         private object iTag = null;
       
   269         private object iEnabledLock = new object();
       
   270         private bool iEnabled = true;
       
   271         private SerializedOperationManager iManager = null;
       
   272         private BackgroundWorker iWorker = new BackgroundWorker();
       
   273         private AutoResetEvent iWaitForCompletion = null;
       
   274         #endregion
       
   275     }
       
   276 }