VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/ThreadTask.cpp@ 63470

Last change on this file since 63470 was 63183, checked in by vboxsync, 8 years ago

ThreadTask: Dealt with totally misguided m_pThread member. Started cleaning up handler() methods.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1/** @file
2 * Implementation of ThreadTask
3 */
4
5/*
6 * Copyright (C) 2015-2016 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#include <iprt/thread.h>
18
19#include "VirtualBoxBase.h"
20#include "ThreadTask.h"
21
22
23/**
24 * Starts the task (on separate thread), consuming @a this.
25 *
26 * The function takes ownership of "this" instance (object instance which calls
27 * this function). And the function is responsible for deletion of "this"
28 * pointer in all cases.
29 *
30 * Possible way of usage:
31 * @code{.cpp}
32 HRESULT hr;
33 SomeTaskInheritedFromThreadTask *pTask = NULL;
34 try
35 {
36 pTask = new SomeTaskInheritedFromThreadTask(this);
37 if (!pTask->Init()) // some init procedure
38 throw E_FAIL;
39 }
40 catch (...)
41 {
42 if (pTask);
43 delete pTask;
44 return E_FAIL;
45 }
46 return pTask->createThread(); // pTask is always consumed
47 @endcode
48 *
49 * @sa createThreadWithType, createThreadWithRaceCondition
50 */
51HRESULT ThreadTask::createThread(void)
52{
53 return createThreadInternal(RTTHREADTYPE_MAIN_WORKER, NULL /*phThread*/);
54}
55
56
57/**
58 * Same ThreadTask::createThread(), except it takes a thread type parameter.
59 *
60 * @param enmType The thread type.
61 */
62HRESULT ThreadTask::createThreadWithType(RTTHREADTYPE enmType)
63{
64 return createThreadInternal(enmType, NULL /*phThread*/);
65}
66
67
68/**
69 * Same ThreadTask::createThread(), except it returns a thread handle.
70 *
71 * If the task thread is incorrectly mananged, the caller may easily race the
72 * completion and termination of the task thread! Use with care!
73 *
74 * @param phThread Handle of the worker thread.
75 */
76HRESULT ThreadTask::createThreadWithRaceCondition(PRTTHREAD phThread)
77{
78 return createThreadInternal(RTTHREADTYPE_MAIN_WORKER, phThread);
79}
80
81
82/**
83 * Internal worker for ThreadTask::createThread,
84 * ThreadTask::createThreadWithType, ThreadTask::createThreadwithRaceCondition.
85 *
86 * @note Always consumes @a this!
87 */
88HRESULT ThreadTask::createThreadInternal(RTTHREADTYPE enmType, PRTTHREAD phThread)
89{
90 int vrc = RTThreadCreate(&m_hThread,
91 taskHandlerThreadProc,
92 (void *)this,
93 0,
94 enmType,
95 0,
96 this->getTaskName().c_str());
97 if (RT_SUCCESS(vrc))
98 {
99 if (phThread)
100 *phThread = m_hThread;
101 return S_OK;
102 }
103
104 delete this;
105 return E_FAIL;
106}
107
108
109/**
110 * Static method that can get passed to RTThreadCreate to have a
111 * thread started for a Task.
112 */
113/* static */ DECLCALLBACK(int) ThreadTask::taskHandlerThreadProc(RTTHREAD /* thread */, void *pvUser)
114{
115 if (pvUser == NULL)
116 return VERR_INVALID_POINTER; /* nobody cares */
117
118 ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);
119
120 /*
121 * handler shall catch and process all possible cases as errors and exceptions.
122 */
123 pTask->handler();
124
125 pTask->m_hThread = NIL_RTTHREAD; /* unnecessary, but whatever. */
126 delete pTask;
127 return VINF_SUCCESS;
128}
129
Note: See TracBrowser for help on using the repository browser.

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