VirtualBox

Changeset 59454 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jan 25, 2016 11:13:52 AM (9 years ago)
Author:
vboxsync
Message:

bugref:7179. Added information about usage of ThreadTask class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-all/ThreadTask.cpp

    r59318 r59454  
    2020#include "ThreadTask.h"
    2121
    22 /** @todo r=bird: DOCUMENTATION!! 'ING DOCUMENTATION!! THIS 'ING FUNCTION
    23  *        CONSUMES THE this OBJECTED AND YOU AREN'T EXACTLY 'ING TELLING
    24  *        ANYBODY.  THAT IS REALLY NICE OF YOU. */
     22/**
     23 *  The function takes ownership of "this" instance (object
     24 *  instance which calls this function).
     25 *  And the function is responsible for deletion of "this"
     26 *  pointer in all cases.
     27 *  Possible way of usage:
     28 *
     29 *  int vrc = VINF_SUCCESS;
     30 *  HRESULT hr = S_OK;
     31 *
     32 *  SomeTaskInheritedFromThreadTask* pTask = NULL;
     33 *  try
     34 *  {
     35 *      pTask = new SomeTaskInheritedFromThreadTask(this);
     36 *      if (!pTask->Init())//some init procedure
     37 *      {
     38 *          delete pTask;
     39 *          throw E_FAIL;
     40 *      }
     41 *      //this function delete pTask in case of exceptions, so
     42 *      there is no need the call of delete operator
     43 *
     44 *      hr = pTask->createThread();
     45 *  }
     46 *  catch(...)
     47 *  {
     48 *      vrc = E_FAIL;
     49 *  }
     50 */
    2551HRESULT ThreadTask::createThread(PRTTHREAD pThread, RTTHREADTYPE enmType)
    2652{
    2753    HRESULT rc = S_OK;
    28     try
     54
     55    m_pThread = pThread;
     56    int vrc = RTThreadCreate(m_pThread,
     57                             taskHandler,
     58                             (void *)this,
     59                             0,
     60                             enmType,
     61                             0,
     62                             this->getTaskName().c_str());
     63
     64    if (RT_FAILURE(vrc))
    2965    {
    30         m_pThread = pThread;
    31         int vrc = RTThreadCreate(m_pThread,
    32                                  taskHandler,
    33                                  (void *)this,
    34                                  0,
    35                                  enmType,
    36                                  0,
    37                                  this->getTaskName().c_str());
    38         if (RT_FAILURE(vrc))
    39         {
    40             throw E_FAIL;
    41         }
    42     }
    43     catch(HRESULT eRC)
    44     {
    45         /** @todo r=bird: only happens in your above throw call.
    46          *  Makes you wonder why you don't just do if (RT_SUCCESS(vrc)) return S_OK;
    47          *  else {delete pTask; return E_FAIL;}   */
    48         rc = eRC;
    49         delete this;
    50     }
    51     catch(std::exception& )
    52     {
    53         /** @todo r=bird: can't happen, RTThreadCreate+Utf8Str doesn't do this. */
    54         rc = E_FAIL;
    55         delete this;
    56     }
    57     catch(...)
    58     {
    59         /** @todo r=bird: can't happen, RTThreadCreate+Utf8Str doesn't do this. */
    60         rc = E_FAIL;
    61         delete this;
     66        delete this;
     67        return E_FAIL;
    6268    }
    6369
     
    7682
    7783    ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);
    78     try
    79     {
    80         pTask->handler();
    81     }
    82     /** @todo r=bird: This next stuff here is utterly and totally pointless, as
    83      *        the handler() method _must_ and _shall_ do this, otherwise there is
    84      *        no 'ing way we can know about it, since you (a) you always return
    85      *        VINF_SUCCESS (aka '0'), and (b) the thread isn't waitable even if
    86      *        you wanted to return anything.  It is much preferred to _crash_ and
    87      *        _burn_ if we fail to catch stuff than quietly ignore it, most
    88      *        likely the state of the objects involved is butchered by this.  At
    89      *        least AssertLogRel()! Gee. */
    90     catch(HRESULT eRC)
    91     {
    92         rc = eRC;
    93     }
    94     catch(std::exception& )
    95     {
    96         rc = E_FAIL;
    97     }
    98     catch(...)
    99     {
    100         rc = E_FAIL;
    101     }
     84
     85    /*
     86    *  handler shall catch and process all possible cases as errors and exceptions.
     87    */
     88    pTask->handler();
    10289
    10390    delete pTask;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette