engine/collectionframework/thumbnailcreator/src/glxtntaskmanager.cpp
changeset 71 27f2d7aec52a
parent 69 45459746d5e8
child 72 0a8e959402e5
equal deleted inserted replaced
69:45459746d5e8 71:27f2d7aec52a
     1 /*
       
     2 * Copyright (c) 2008-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:   Task manager for multiple tasks. Allows asynchronosity and 
       
    15 *                tracking/callback for long running tasks.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  * @internal reviewed 31/07/2007 by Simon Brooks
       
    23  */
       
    24 
       
    25 #include "glxtntaskmanager.h"
       
    26 
       
    27 #include <glxtracer.h>
       
    28 
       
    29 #include "glxtnbackgroundgenerationtask.h"
       
    30 #include "glxtntask.h"
       
    31 
       
    32 const TInt KGlxTMMaxTasksToRunInOneRound = 4;
       
    33 // -----------------------------------------------------------------------------
       
    34 // Static Constructor
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CGlxtnTaskManager* CGlxtnTaskManager::NewL()
       
    38     {
       
    39     TRACER("CGlxtnTaskManager::NewL()");
       
    40 	CGlxtnTaskManager* self = new (ELeave) CGlxtnTaskManager();
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	CleanupStack::Pop(self);
       
    44 	return self;
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // Constructor
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CGlxtnTaskManager::CGlxtnTaskManager() 
       
    52 		: CActive(EPriorityStandard)
       
    53     {
       
    54     TRACER("CGlxtnTaskManager::CGlxtnTaskManager()");
       
    55     }
       
    56      
       
    57 // -----------------------------------------------------------------------------
       
    58 // ConstructL
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CGlxtnTaskManager::ConstructL()
       
    62     {
       
    63     TRACER("void CGlxtnTaskManager::ConstructL()");
       
    64     CActiveScheduler::Add(this);
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // Destructor
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CGlxtnTaskManager::~CGlxtnTaskManager()
       
    72     {
       
    73     TRACER("CGlxtnTaskManager::~CGlxtnTaskManager()");
       
    74     Cancel(); 
       
    75     iTasks.ResetAndDestroy();
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // Task by id
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CGlxtnTask* CGlxtnTaskManager::Task(const TGlxtnTaskId& aId) const
       
    83 	{
       
    84     TRACER("CGlxtnTaskManager::Task(const TGlxtnTaskId& aId)");
       
    85 	TInt count = iTasks.Count();
       
    86 	for (TInt i = 0; i < count; i++) 
       
    87 		{
       
    88 		CGlxtnTask* task = iTasks[i];
       
    89 		if (task->Id() == aId) 
       
    90 			{
       
    91 			return task;
       
    92 			}
       
    93 		}
       
    94 		
       
    95 	// Not found
       
    96 	return NULL;
       
    97 	}
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // AddTaskL
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CGlxtnTaskManager::AddTaskL(CGlxtnTask* aTask, TExecutionOrder aExecutionOrder)
       
   104     {
       
   105     TRACER("CGlxtnTaskManager::AddTaskL()");
       
   106     __ASSERT_DEBUG(aTask, Panic(EGlxPanicNullPointer));
       
   107 	__ASSERT_DEBUG(iTasks.Find(aTask) == KErrNotFound, Panic(EGlxPanicIllegalArgument)); // Task already added
       
   108     CleanupStack::PushL(aTask);
       
   109 
       
   110  	// Order manually to allow full control over placement of tasks when 
       
   111  	// priorities are the same
       
   112  	TInt priority = aTask->Priority();
       
   113  	TInt count = iTasks.Count();
       
   114 
       
   115     TInt pos = 0;
       
   116     while ( pos < count )
       
   117         {
       
   118  		TInt compPriority = iTasks[pos]->Priority();
       
   119  		if ( compPriority < priority
       
   120  		    || (compPriority == priority && EFirstOut == aExecutionOrder) )
       
   121  		    {
       
   122  		    break;
       
   123  		    }
       
   124 
       
   125         pos++;
       
   126         }
       
   127 
       
   128     iTasks.InsertL(aTask, pos);
       
   129     CleanupStack::Pop(aTask);
       
   130 
       
   131     aTask->SetManager(this);
       
   132 
       
   133 	// Start processing the task if this active object is not already doing something else
       
   134 	if ( !iCurrentTask ) 
       
   135 		{
       
   136         AsyncRun();
       
   137 		}
       
   138     else if( dynamic_cast<CGlxtnBackgroundGenerationTask*>(iCurrentTask) && (!dynamic_cast<CGlxtnBackgroundGenerationTask*>(aTask)) )
       
   139         {
       
   140         iCurrentTask->Cancel();
       
   141         }
       
   142     }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CancelTasks
       
   146 // Cancel all tasks relating to a given media item.
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 void CGlxtnTaskManager::CancelTasks(const TGlxMediaId& aItemId)
       
   150     {
       
   151     TRACER("CGlxtnTaskManager::CancelTasks()");
       
   152     TInt count = iTasks.Count();
       
   153 
       
   154     if( aItemId == TGlxMediaId(0) )
       
   155         {
       
   156         if( dynamic_cast<CGlxtnBackgroundGenerationTask*>(iCurrentTask) )
       
   157         {
       
   158         iCurrentTask->Cancel();
       
   159         }
       
   160         }
       
   161 
       
   162     for ( TInt i = 0; i < count; i++ )
       
   163         {
       
   164         CGlxtnTask* task = iTasks[i];
       
   165 
       
   166         if ( task->ItemId() == aItemId )
       
   167             {
       
   168             task->Cancel();		
       
   169             }
       
   170         }
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CancelTasks
       
   175 // Cancel all tasks using a given storage.
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 void CGlxtnTaskManager::CancelTasks(MGlxtnThumbnailStorage* aStorage)
       
   179     {
       
   180     TRACER("void CGlxtnTaskManager::CancelTasks()");
       
   181     TInt count = iTasks.Count();
       
   182 
       
   183     for ( TInt i = 0; i < count; i++ )
       
   184         {
       
   185         CGlxtnTask* task = iTasks[i];
       
   186 
       
   187         if ( task->Storage() == aStorage )
       
   188             {
       
   189             task->Cancel();		
       
   190             }
       
   191         }
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // RunL
       
   196 // -----------------------------------------------------------------------------
       
   197 //   
       
   198 void CGlxtnTaskManager::RunL()
       
   199 	{
       
   200     TRACER("void CGlxtnTaskManager::RunL()");
       
   201     TInt taskIndex = 0;
       
   202     TInt tasksRun = 0;
       
   203     while ( taskIndex < iTasks.Count() )
       
   204         {
       
   205     	if ( iCurrentTask )
       
   206     	    {
       
   207             if ( CGlxtnTask::EStarting == iCurrentTask->State() ) 
       
   208                 {
       
   209                 // Start the task
       
   210                 iCurrentTask->StartL(iStatus);
       
   211                 }
       
   212             else if ( CGlxtnTask::ERunning == iCurrentTask->State() )
       
   213                 {
       
   214                 // Let the task run
       
   215                 iCurrentTask->RunL(iStatus);
       
   216                 }
       
   217 
       
   218             if ( CGlxtnTask::ERunning == iCurrentTask->State() )
       
   219                 {
       
   220                 SetActive();
       
   221                 return;
       
   222                 }
       
   223 
       
   224             // Task completed or cancelled, delete the task immediately
       
   225             __ASSERT_DEBUG(iStatus != KRequestPending, Panic(EGlxPanicIllegalState));
       
   226             __ASSERT_DEBUG(CGlxtnTask::EComplete == iCurrentTask->State()
       
   227                         || CGlxtnTask::ECanceled == iCurrentTask->State(), Panic(EGlxPanicIllegalState));
       
   228 
       
   229             // Search for the task again, since the processing of the task might have added new tasks to the queue
       
   230             TInt index = iTasks.Find(iCurrentTask);
       
   231             __ASSERT_DEBUG(index != KErrNotFound, Panic(EGlxPanicLogicError));
       
   232             iTasks.Remove(index);
       
   233             delete iCurrentTask;
       
   234             iCurrentTask = NULL;
       
   235 
       
   236             // Start from top, since the task might have added another task
       
   237             taskIndex = 0;
       
   238     	    }
       
   239     	else
       
   240     	    {
       
   241             CGlxtnTask* task = iTasks[taskIndex];
       
   242             CGlxtnTask::TState state = task->State();
       
   243 
       
   244             switch ( state )
       
   245                 {
       
   246                 case CGlxtnTask::EIdle:
       
   247                     // Task not started. Ignore and try next task
       
   248                     taskIndex++; // Skip
       
   249                     break;
       
   250                 case CGlxtnTask::EStarting:
       
   251                     {
       
   252                     // Start the task
       
   253                     iCurrentTask = task;
       
   254 
       
   255                     // Update active object priority to match task priority
       
   256                     TInt priority = task->Priority();
       
   257                     if ( priority != Priority() )
       
   258                         {
       
   259                         SetPriority(priority);
       
   260                         }
       
   261 
       
   262                     // Check if too many tasks have been run during this call to RunL. 
       
   263                     // This check exist to make sure a blocking loop does not occur, in which
       
   264                     // a task creates another task which completes immediately, which creates
       
   265                     // another task, which also completes immediately. (Task don't have to 
       
   266                     // be asynchronous.)
       
   267                     tasksRun++;
       
   268                     if ( tasksRun >= KGlxTMMaxTasksToRunInOneRound )
       
   269                         {
       
   270                         AsyncRun();
       
   271                         return;
       
   272                         }
       
   273                     }
       
   274                     break;
       
   275                 case CGlxtnTask::ECanceled:
       
   276         			// Task is canceled, remove from queue
       
   277         			delete task;
       
   278         			iTasks.Remove(taskIndex);
       
   279                     break;
       
   280                 default:
       
   281                     Panic(EGlxPanicIllegalState);   // Unknown or wrong state
       
   282                     break;
       
   283                 }
       
   284     	    }
       
   285         }
       
   286 	}
       
   287 	
       
   288 // -----------------------------------------------------------------------------
       
   289 // DoCancel
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CGlxtnTaskManager::DoCancel()
       
   293     {
       
   294     TRACER("CGlxtnTaskManager::DoCancel()");
       
   295     TInt count = iTasks.Count();
       
   296 	for (TInt i = 0; i < count; i++)
       
   297 		{
       
   298 		CGlxtnTask* task = iTasks[i];
       
   299 		CGlxtnTask::TState state = task->State();
       
   300 		if (state == CGlxtnTask::ERunning) 
       
   301 			{
       
   302 			task->Cancel();
       
   303 			}
       
   304 		}
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // RunError
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 TInt CGlxtnTaskManager::RunError(TInt aError)
       
   312     {
       
   313     TRACER("CGlxtnTaskManager::RunError()");
       
   314     if ( iCurrentTask )
       
   315         {
       
   316         iCurrentTask->RunError(aError);
       
   317 
       
   318         if ( CGlxtnTask::ERunning == iCurrentTask->State() )
       
   319             {
       
   320             SetActive();
       
   321             }
       
   322         else
       
   323             {
       
   324             AsyncRun();
       
   325             }
       
   326 
       
   327         return KErrNone;
       
   328         }
       
   329 
       
   330     return aError;
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // AsyncRun
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 void CGlxtnTaskManager::AsyncRun()
       
   338     {
       
   339     TRACER("CGlxtnTaskManager::AsyncRun()");
       
   340     // Issue a self-completing request.
       
   341     iStatus = KRequestPending;
       
   342     SetActive();
       
   343     TRequestStatus* status = &iStatus;
       
   344     User::RequestComplete(status, KErrNone);
       
   345     }