VirtualBox

Changeset 27831 in vbox


Ignore:
Timestamp:
Mar 30, 2010 2:55:27 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
59506
Message:

Main/Medium: auto_ptr is evil, removed its use with the task objects

Location:
trunk/src/VBox/Main
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/MediumImpl.cpp

    r27824 r27831  
    407407 * Thread function for time-consuming medium tasks.
    408408 *
    409  * @param pvUser    Pointer to the std::auto_ptr<Medium::Task> instance.
     409 * @param pvUser    Pointer to the Medium::Task instance.
    410410 */
    411411/* static */
     
    413413{
    414414    LogFlowFuncEnter();
    415     /* pvUser is a pointer to a std::auto_ptr<Medium::Task>, which is so
    416      * hard to understand that we just clear this situation now by copying
    417      * it. This means the old object loses ownership, and task owns it. */
    418415    AssertReturn(pvUser, (int)E_INVALIDARG);
    419     std::auto_ptr<Medium::Task> *pTask =
    420         static_cast<std::auto_ptr<Medium::Task> *>(pvUser);
    421     std::auto_ptr<Medium::Task> task(pTask->release());
    422     AssertReturn(task.get(), (int)E_INVALIDARG);
    423 
    424     task->mThread = aThread;
    425 
    426     HRESULT rc = task->handler();
     416    Medium::Task *pTask = static_cast<Medium::Task *>(pvUser);
     417
     418    pTask->mThread = aThread;
     419
     420    HRESULT rc = pTask->handler();
    427421
    428422    /* complete the progress if run asynchronously */
    429     if (task->isAsync())
    430     {
    431         if (!task->mProgress.isNull())
    432             task->mProgress->notifyComplete(rc);
    433     }
     423    if (pTask->isAsync())
     424    {
     425        if (!pTask->mProgress.isNull())
     426            pTask->mProgress->notifyComplete(rc);
     427    }
     428
     429    /* pTask is no longer needed, delete it. */
     430    delete pTask;
    434431
    435432    LogFlowFunc(("rc=%Rhrc\n", rc));
     
    24692466
    24702467    /* setup task object to carry out the operation asynchronously */
    2471     std::auto_ptr<Medium::Task> task(new CreateBaseTask(this, progress,
    2472                                                         aLogicalSize,
    2473                                                         aVariant));
    2474     AssertComRCReturnRC(task->rc());
    2475 
    2476     rc = startThread(task);
     2468    Medium::Task *pTask(new Medium::CreateBaseTask(this, progress,
     2469                                                   aLogicalSize, aVariant));
     2470    rc = pTask->rc();
     2471    if (FAILED(rc))
     2472    {
     2473        AssertComRC(rc);
     2474        delete pTask;
     2475        return rc;
     2476    }
     2477
     2478    rc = startThread(pTask);
    24772479    if (FAILED(rc)) return rc;
    24782480
     
    24802482    m->state = MediumState_Creating;
    24812483
     2484    progress.queryInterfaceTo(aProgress);
     2485
    24822486    return S_OK;
    24832487}
     
    24942498    HRESULT rc = deleteStorageNoWait(progress);
    24952499    if (SUCCEEDED(rc))
    2496     {
    2497         /* return progress to the caller */
    24982500        progress.queryInterfaceTo(aProgress);
    2499     }
    25002501
    25012502    return rc;
     
    25362537    }
    25372538    else
    2538     {
    2539         /* return progress to the caller */
    25402539        progress.queryInterfaceTo(aProgress);
    2541     }
    25422540
    25432541    return rc;
     
    26322630
    26332631        /* setup task object to carry out the operation asynchronously */
    2634         std::auto_ptr<Medium::Task> task(new CloneTask(this, progress,
    2635                                                        target, parent,
    2636                                                        sourceChain.release(),
    2637                                                        parentChain.release(),
    2638                                                        aVariant));
    2639         AssertComRCReturnRC(task->rc());
    2640 
    2641         rc = startThread(task);
     2632        Medium::Task *pTask(new Medium::CloneTask(this, progress, target,
     2633                                                  parent,
     2634                                                  sourceChain.release(),
     2635                                                  parentChain.release(),
     2636                                                  aVariant));
     2637        rc = pTask->rc();
     2638        if (FAILED(rc))
     2639        {
     2640            AssertComRC(rc);
     2641            delete pTask;
     2642            throw rc;
     2643        }
     2644
     2645        rc = startThread(pTask);
    26422646        if (FAILED(rc)) throw rc;
    26432647
     
    26542658
    26552659    if (SUCCEEDED(rc))
    2656         /* return progress to the caller */
    26572660        progress.queryInterfaceTo(aProgress);
    26582661
     
    27022705
    27032706        /* setup task object to carry out the operation asynchronously */
    2704         std::auto_ptr<Medium::Task> task(new CompactTask(this, progress,
    2705                                                          imgChain.release()));
    2706         AssertComRCReturnRC(task->rc());
    2707 
    2708         rc = startThread(task);
     2707        Medium::Task *pTask(new Medium::CompactTask(this, progress,
     2708                                                    imgChain.release()));
     2709        rc = pTask->rc();
     2710        if (FAILED(rc))
     2711        {
     2712            AssertComRC(rc);
     2713            delete pTask;
     2714            throw rc;
     2715        }
     2716
     2717        rc = startThread(pTask);
    27092718        if (FAILED(rc)) throw rc;
    27102719    }
     
    27152724
    27162725    if (SUCCEEDED(rc))
    2717     {
    2718         /* return progress to the caller */
    27192726        progress.queryInterfaceTo(aProgress);
    2720     }
    27212727
    27222728    return rc;
     
    27722778
    27732779        /* setup task object to carry out the operation asynchronously */
    2774         std::auto_ptr<Medium::Task> task(new ResetTask(this, progress));
    2775         AssertComRCReturnRC(task->rc());
    2776 
    2777         rc = startThread(task);
     2780        Medium::Task *pTask(new Medium::ResetTask(this, progress));
     2781        rc = pTask->rc();
     2782        if (FAILED(rc))
     2783        {
     2784            AssertComRC(rc);
     2785            delete pTask;
     2786            throw rc;
     2787        }
     2788
     2789        rc = startThread(pTask);
    27782790        if (FAILED(rc)) throw rc;
    27792791    }
     
    27902802    }
    27912803    else
    2792     {
    2793         /* return progress to the caller */
    27942804        progress.queryInterfaceTo(aProgress);
    2795     }
    27962805
    27972806    LogFlowThisFunc(("LEAVE, rc=%Rhrc\n", rc));
     
    43314340
    43324341    /* setup task object to carry out the operation asynchronously */
    4333     std::auto_ptr<Medium::Task> task(new DeleteTask(this, progress));
    4334     AssertComRCReturnRC(task->rc());
     4342    Medium::Task *pTask(new Medium::DeleteTask(this, progress));
     4343    rc = pTask->rc();
     4344    if (FAILED(rc))
     4345    {
     4346        AssertComRC(rc);
     4347        delete pTask;
     4348        return rc;
     4349    }
    43354350
    43364351    if (aWait)
     
    43394354        m->state = MediumState_Deleting;
    43404355
    4341         rc = runNow(task, NULL /* pfNeedsSaveSettings*/ );        // there is no save settings to do in taskThreadDelete()
     4356        rc = runNow(pTask, NULL /* pfNeedsSaveSettings*/ );        // there is no save settings to do in taskThreadDelete()
     4357        if (FAILED(rc)) return rc;
    43424358    }
    43434359    else
    43444360    {
    4345         rc = startThread(task);
     4361        rc = startThread(pTask);
    43464362        if (FAILED(rc)) return rc;
    43474363
     
    43514367
    43524368    if (aProgress != NULL)
    4353     {
    4354         /* return progress to the caller */
    43554369        *aProgress = progress;
    4356     }
    43574370
    43584371    return rc;
     
    44714484
    44724485    /* setup task object to carry out the operation asynchronously */
    4473     std::auto_ptr<Medium::Task> task(new CreateDiffTask(this, progress,
    4474                                                         aTarget, aVariant));
    4475     AssertComRCReturnRC(task->rc());
     4486    Medium::Task *pTask(new Medium::CreateDiffTask(this, progress, aTarget,
     4487                                                   aVariant));
     4488    rc = pTask->rc();
     4489    if (FAILED(rc))
     4490    {
     4491        AssertComRC(rc);
     4492        delete pTask;
     4493        return rc;
     4494    }
    44764495
    44774496    /* register a task (it will deregister itself when done) */
     
    44894508        alock.release();
    44904509
    4491         rc = runNow(task, pfNeedsSaveSettings);
     4510        rc = runNow(pTask, pfNeedsSaveSettings);
     4511        if (FAILED(rc)) return rc;
    44924512    }
    44934513    else
    44944514    {
    4495         rc = startThread(task);
     4515        rc = startThread(pTask);
    44964516        if (FAILED(rc)) return rc;
    44974517
     
    45014521
    45024522    if (aProgress != NULL)
    4503     {
    4504         /* return progress to the caller */
    45054523        *aProgress = progress;
    4506     }
    45074524
    45084525    return rc;
     
    47224739
    47234740    /* setup task object to carry out the operation asynchronously */
    4724     std::auto_ptr<Medium::Task> task(new MergeTask(this, progress, aChain));
    4725     AssertComRCReturnRC(task->rc());
     4741    Medium::Task *pTask(new Medium::MergeTask(this, progress, aChain));
     4742    rc = pTask->rc();
     4743    if (FAILED(rc))
     4744    {
     4745        AssertComRC(rc);
     4746        delete pTask;
     4747        return rc;
     4748    }
    47264749
    47274750    /* Note: task owns aChain (will delete it when not needed) in all cases
     
    47324755    if (aWait)
    47334756    {
    4734         rc = runNow(task, pfNeedsSaveSettings);
     4757        rc = runNow(pTask, pfNeedsSaveSettings);
     4758        if (FAILED(rc)) return rc;
    47354759    }
    47364760    else
    47374761    {
    4738         rc = startThread(task);
     4762        rc = startThread(pTask);
    47394763        if (FAILED(rc)) return rc;
    47404764    }
    47414765
    47424766    if (aProgress != NULL)
    4743     {
    4744         /* return progress to the caller */
    47454767        *aProgress = progress;
    4746     }
    47474768
    47484769    return rc;
     
    50195040 * Starts a new thread driven by the appropriate Medium::Task::handler() method.
    50205041 *
    5021  * @note if this method returns success, this Medium::Task object becomes owned
    5022  *       by the started thread and will be automatically deleted when the
    5023  *       thread terminates.
    5024  *
    50255042 * @note When the task is executed by this method, IProgress::notifyComplete()
    50265043 *       is automatically called for the progress object associated with this
     
    50285045 *       other threads asynchronously waiting for it.
    50295046 */
    5030 HRESULT Medium::startThread(std::auto_ptr<Medium::Task> task)
     5047HRESULT Medium::startThread(Medium::Task *pTask)
    50315048{
    50325049    /// @todo use a more descriptive task name
    5033     int vrc = RTThreadCreate(NULL, Medium::Task::fntMediumTask, &task,
     5050    int vrc = RTThreadCreate(NULL, Medium::Task::fntMediumTask, pTask,
    50345051                             0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
    50355052                             "Medium::Task");
    5036     ComAssertMsgRCRet(vrc,
    5037                       ("Could not create Medium::Task thread (%Rrc)\n", vrc),
    5038                       E_FAIL);
     5053    if (RT_FAILURE(vrc))
     5054    {
     5055        delete pTask;
     5056        ComAssertMsgRCRet(vrc,
     5057                          ("Could not create Medium::Task thread (%Rrc)\n",
     5058                           vrc),
     5059                          E_FAIL);
     5060    }
    50395061
    50405062    return S_OK;
     
    50495071 * operations are potentially lengthy and will block the calling thread in this
    50505072 * case.
    5051  *
    5052  * @note This Medium::Task object will be deleted when this method returns.
    50535073 *
    50545074 * @note When the task is executed by this method, IProgress::notifyComplete()
     
    50585078 *       complete the progress object in this case.
    50595079 */
    5060 HRESULT Medium::runNow(std::auto_ptr<Medium::Task> task,
     5080HRESULT Medium::runNow(Medium::Task *pTask,
    50615081                       bool *pfNeedsSaveSettings)
    50625082{
    5063     task->m_pfNeedsSaveSettings = pfNeedsSaveSettings;
     5083    pTask->m_pfNeedsSaveSettings = pfNeedsSaveSettings;
    50645084
    50655085    /* NIL_RTTHREAD indicates synchronous call. */
    5066     return (HRESULT)Medium::Task::fntMediumTask(NIL_RTTHREAD, &task);
     5086    return (HRESULT)Medium::Task::fntMediumTask(NIL_RTTHREAD, pTask);
    50675087}
    50685088
     
    50775097 * @return
    50785098 */
    5079 HRESULT Medium::taskThreadCreateBase(CreateBaseTask &task)
     5099HRESULT Medium::taskThreadCreateBase(Medium::CreateBaseTask &task)
    50805100{
    50815101    HRESULT rc = S_OK;
     
    52025222 * @return
    52035223 */
    5204 HRESULT Medium::taskThreadCreateDiff(CreateDiffTask &task)
     5224HRESULT Medium::taskThreadCreateDiff(Medium::CreateDiffTask &task)
    52055225{
    52065226    HRESULT rc = S_OK;
     
    53855405 * @return
    53865406 */
    5387 HRESULT Medium::taskThreadMerge(MergeTask &task)
     5407HRESULT Medium::taskThreadMerge(Medium::MergeTask &task)
    53885408{
    53895409    HRESULT rc = S_OK;
     
    56575677 * @return
    56585678 */
    5659 HRESULT Medium::taskThreadClone(CloneTask &task)
     5679HRESULT Medium::taskThreadClone(Medium::CloneTask &task)
    56605680{
    56615681    HRESULT rc = S_OK;
     
    58755895 * @return
    58765896 */
    5877 HRESULT Medium::taskThreadDelete(DeleteTask &task)
    5878 {
     5897HRESULT Medium::taskThreadDelete(Medium::DeleteTask &task)
     5898{
     5899    NOREF(task);
    58795900    HRESULT rc = S_OK;
    58805901
     
    59405961 * @return
    59415962 */
    5942 HRESULT Medium::taskThreadReset(ResetTask &task)
     5963HRESULT Medium::taskThreadReset(Medium::ResetTask &task)
    59435964{
    59445965    HRESULT rc = S_OK;
     
    60506071 * @return
    60516072 */
    6052 HRESULT Medium::taskThreadCompact(CompactTask &task)
     6073HRESULT Medium::taskThreadCompact(Medium::CompactTask &task)
    60536074{
    60546075    HRESULT rc = S_OK;
  • trunk/src/VBox/Main/include/MediumImpl.h

    r27803 r27831  
    318318    friend class MergeTask;
    319319
    320     HRESULT startThread(std::auto_ptr<Task> task);
    321     HRESULT runNow(std::auto_ptr<Task> task, bool *pfNeedsSaveSettings);
    322 
    323     HRESULT taskThreadCreateBase(CreateBaseTask &task);
    324     HRESULT taskThreadCreateDiff(CreateDiffTask &task);
    325     HRESULT taskThreadMerge(MergeTask &task);
    326     HRESULT taskThreadClone(CloneTask &task);
    327     HRESULT taskThreadDelete(DeleteTask &task);
    328     HRESULT taskThreadReset(ResetTask &task);
    329     HRESULT taskThreadCompact(CompactTask &task);
     320    HRESULT startThread(Medium::Task *pTask);
     321    HRESULT runNow(Medium::Task *pTask, bool *pfNeedsSaveSettings);
     322
     323    HRESULT taskThreadCreateBase(Medium::CreateBaseTask &task);
     324    HRESULT taskThreadCreateDiff(Medium::CreateDiffTask &task);
     325    HRESULT taskThreadMerge(Medium::MergeTask &task);
     326    HRESULT taskThreadClone(Medium::CloneTask &task);
     327    HRESULT taskThreadDelete(Medium::DeleteTask &task);
     328    HRESULT taskThreadReset(Medium::ResetTask &task);
     329    HRESULT taskThreadCompact(Medium::CompactTask &task);
    330330
    331331    struct Data;            // opaque data struct, defined in MediumImpl.cpp
Note: See TracChangeset for help on using the changeset viewer.

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