crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/Threading/MultiThreadedProcessor.cs
changeset 0 818e61de6cd1
child 2 0c91f0baec58
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.Text;
       
    19 using System.IO;
       
    20 using System.Collections.Generic;
       
    21 using System.Threading;
       
    22 using System.Reflection;
       
    23 
       
    24 namespace SymbianUtils.Threading
       
    25 {
       
    26     public class MultiThreadedProcessor<T> : DisposableObject
       
    27     {
       
    28         #region Enumerations
       
    29         public enum TEvent
       
    30         {
       
    31             EEventStarting = 0,
       
    32             EEventCompleted
       
    33         }
       
    34         #endregion
       
    35 
       
    36         #region Delegates & events
       
    37         public delegate void ProcessorEventHandler( TEvent aEvent );
       
    38         public event ProcessorEventHandler EventHandler = delegate { };
       
    39         //
       
    40         public delegate void ItemProcessor( T aItem );
       
    41         public event ItemProcessor ProcessItem = null;
       
    42         //
       
    43         public delegate void ExceptionHandler( Exception aException );
       
    44         public event ExceptionHandler Exception;
       
    45         #endregion
       
    46         
       
    47         #region Constructors
       
    48         public MultiThreadedProcessor( IEnumerable<T> aCollection )
       
    49             : this( aCollection, ThreadPriority.Normal )
       
    50         {
       
    51         }
       
    52 
       
    53         public MultiThreadedProcessor( IEnumerable<T> aCollection, ThreadPriority aPriority )
       
    54         {
       
    55             PopulateQueue( aCollection );
       
    56             iThreadPriorities = aPriority;
       
    57             iProcessorCount = System.Environment.ProcessorCount;
       
    58         }
       
    59         #endregion
       
    60 
       
    61         #region Framework API
       
    62         public virtual void Start( TSynchronicity aSynchronicity )
       
    63         {
       
    64             iSynchronicity = aSynchronicity;
       
    65             OnEvent( MultiThreadedProcessor<T>.TEvent.EEventStarting );
       
    66 
       
    67             int count = iQueue.Count;
       
    68             if ( count == 0 )
       
    69             {
       
    70                 // Nothing to do!
       
    71                 OnEvent( MultiThreadedProcessor<T>.TEvent.EEventCompleted );
       
    72             }
       
    73             else
       
    74             {
       
    75                 // For sync mode, we need to block until the operation
       
    76                 // completes.
       
    77                 DestroyBlocker();
       
    78                 if ( aSynchronicity == TSynchronicity.ESynchronous )
       
    79                 {
       
    80                     iSynchronousBlocker = new ManualResetEvent( false );
       
    81                 }
       
    82 
       
    83                 // Create worker threads to process queue items. One per
       
    84                 // processor core.
       
    85                 CreateThreads();
       
    86 
       
    87                 if ( aSynchronicity == TSynchronicity.ESynchronous )
       
    88                 {
       
    89                     System.Diagnostics.Debug.Assert( iSynchronousBlocker != null );
       
    90 
       
    91                     // Now wait.
       
    92                     using ( iSynchronousBlocker )
       
    93                     {
       
    94                         iSynchronousBlocker.WaitOne();
       
    95                     }
       
    96                     iSynchronousBlocker = null;
       
    97 
       
    98                     // See comments in "RunThread" below for details about why
       
    99                     // we do this here - it avoids a race condition.
       
   100                     OperationComplete();
       
   101                 }
       
   102             }
       
   103         }
       
   104 
       
   105         protected virtual bool Process( T aItem )
       
   106         {
       
   107             return false;
       
   108         }
       
   109 
       
   110         protected virtual void OnException( Exception aException )
       
   111         {
       
   112             if ( Exception != null )
       
   113             {
       
   114                 Exception( aException );
       
   115             }
       
   116         }
       
   117         #endregion
       
   118         
       
   119         #region Properties
       
   120         #endregion
       
   121 
       
   122         #region Internal methods
       
   123         protected void PopulateQueue( IEnumerable<T> aCollection )
       
   124         {
       
   125             iQueue.Clear();
       
   126             //
       
   127             foreach ( T item in aCollection )
       
   128             {
       
   129                 iQueue.Enqueue( item );
       
   130             }
       
   131         }
       
   132 
       
   133         private void CreateThreads()
       
   134         {
       
   135             Random r = new Random( DateTime.Now.Millisecond );
       
   136             //
       
   137             int count = System.Environment.ProcessorCount;
       
   138             for ( int i = 0; i < count; i++ )
       
   139             {
       
   140                 string name = string.Format( "Processor Thread {0:d3} {1:d8}", i, r.Next() );
       
   141                 Thread t = new Thread( new ThreadStart( RunThread ) );
       
   142                 t.IsBackground = true;
       
   143                 t.Priority = iThreadPriorities;
       
   144                 iThreads.Add( t );
       
   145                 //
       
   146                 t.Start();
       
   147             }
       
   148         }
       
   149 
       
   150         private void RunThread()
       
   151         {
       
   152             // Process items until none are left.
       
   153             while ( iQueue.Count > 0 )
       
   154             {
       
   155                 T item;
       
   156                 //
       
   157                 bool dequeued = iQueue.TryToDequeue( out item );
       
   158                 if ( dequeued )
       
   159                 {
       
   160                     // First try virtual function call. If that fails then 
       
   161                     // we'll resort to trying an event handler.
       
   162                     try
       
   163                     {
       
   164                         bool processed = Process( item );
       
   165                         if ( processed == false && ProcessItem != null )
       
   166                         {
       
   167                             ProcessItem( item );
       
   168                         }
       
   169                     }
       
   170                     catch ( Exception e )
       
   171                     {
       
   172                         // Let the derived class handle exceptions
       
   173                         OnException( e );
       
   174                     }
       
   175                 }
       
   176             }
       
   177 
       
   178             // If all the threads have finished then the entire
       
   179             // operation is complete.
       
   180             bool finished = false;
       
   181             lock ( iThreads )
       
   182             {
       
   183                 iThreads.Remove( Thread.CurrentThread );
       
   184                 finished = ( iThreads.Count == 0 );
       
   185             }
       
   186 
       
   187             // Check for completion
       
   188             if ( finished )
       
   189             {
       
   190                 // If we're operating synchronously, then let the main
       
   191                 // thread (the one that is currently blocked) report
       
   192                 // completion. This prevents a race condition whereby the worker
       
   193                 // threads (i.e. the thread in which this function is running)
       
   194                 // notifies about completion before the main thread has started
       
   195                 // blocking (waiting). This can cause an exception whereby 
       
   196                 // the client might dispose of this object twice.
       
   197                 if ( iSynchronicity == TSynchronicity.EAsynchronous )
       
   198                 {
       
   199                     OperationComplete();
       
   200                 }
       
   201                 else
       
   202                 {
       
   203                     // Will be done by "Start"
       
   204                 }
       
   205 
       
   206                 // Always release the blocker to "unblock" the main thread
       
   207                 ReleaseBlocker();
       
   208             }
       
   209         }
       
   210 
       
   211         private void OperationComplete()
       
   212         {
       
   213             OnEvent( MultiThreadedProcessor<T>.TEvent.EEventCompleted );
       
   214         }
       
   215 
       
   216         private void DestroyBlocker()
       
   217         {
       
   218             if ( iSynchronousBlocker != null )
       
   219             {
       
   220                 iSynchronousBlocker.Close();
       
   221                 iSynchronousBlocker = null;
       
   222             }
       
   223         }
       
   224 
       
   225         private void ReleaseBlocker()
       
   226         {
       
   227             if ( iSynchronousBlocker != null )
       
   228             {
       
   229                 iSynchronousBlocker.Set();
       
   230             }
       
   231         }
       
   232 
       
   233         protected virtual void OnEvent( TEvent aEvent )
       
   234         {
       
   235             EventHandler( aEvent );
       
   236         }
       
   237         #endregion
       
   238 
       
   239         #region From DisposableObject
       
   240         protected override void CleanupManagedResources()
       
   241         {
       
   242             try
       
   243             {
       
   244                 base.CleanupManagedResources();
       
   245             }
       
   246             finally
       
   247             {
       
   248                 DestroyBlocker();
       
   249             }
       
   250         }
       
   251         #endregion
       
   252 
       
   253         #region Data members
       
   254         private readonly int iProcessorCount;
       
   255         private readonly ThreadPriority iThreadPriorities;
       
   256         private BlockingQueue<T> iQueue = new BlockingQueue<T>();
       
   257         private List<Thread> iThreads = new List<Thread>();
       
   258         private ManualResetEvent iSynchronousBlocker = null;
       
   259         private TSynchronicity iSynchronicity = TSynchronicity.ESynchronous;
       
   260         #endregion
       
   261     }
       
   262 }