VirtualBox

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

Last change on this file since 99771 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1/* $Id: ThreadTask.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * Implementation of ThreadTask
4 */
5
6/*
7 * Copyright (C) 2015-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#include <iprt/errcore.h>
29#include <iprt/thread.h>
30
31#include "VirtualBoxBase.h"
32#include "ThreadTask.h"
33
34#define LOG_GROUP LOG_GROUP_MAIN_THREAD_TASK
35#include "LoggingNew.h"
36
37/**
38 * Starts the task (on separate thread), consuming @a this.
39 *
40 * The function takes ownership of "this" instance (object instance which calls
41 * this function). And the function is responsible for deletion of "this"
42 * pointer in all cases.
43 *
44 * Possible way of usage:
45 * @code{.cpp}
46 HRESULT hr;
47 SomeTaskInheritedFromThreadTask *pTask = NULL;
48 try
49 {
50 pTask = new SomeTaskInheritedFromThreadTask(this);
51 if (!pTask->Init()) // some init procedure
52 throw E_FAIL;
53 }
54 catch (...)
55 {
56 if (pTask);
57 delete pTask;
58 return E_FAIL;
59 }
60 return pTask->createThread(); // pTask is always consumed
61 @endcode
62 *
63 * @sa createThreadWithType
64 *
65 * @note Always consumes @a this!
66 */
67HRESULT ThreadTask::createThread(void)
68{
69 return createThreadInternal(RTTHREADTYPE_MAIN_WORKER);
70}
71
72
73/**
74 * Same ThreadTask::createThread(), except it takes a thread type parameter.
75 *
76 * @param enmType The thread type.
77 *
78 * @note Always consumes @a this!
79 */
80HRESULT ThreadTask::createThreadWithType(RTTHREADTYPE enmType)
81{
82 return createThreadInternal(enmType);
83}
84
85
86/**
87 * Internal worker for ThreadTask::createThread,
88 * ThreadTask::createThreadWithType.
89 *
90 * @note Always consumes @a this!
91 */
92HRESULT ThreadTask::createThreadInternal(RTTHREADTYPE enmType)
93{
94 LogThisFunc(("Created \"%s\"\n", m_strTaskName.c_str()));
95
96 mAsync = true;
97 int vrc = RTThreadCreate(NULL,
98 taskHandlerThreadProc,
99 (void *)this,
100 0,
101 enmType,
102 0,
103 m_strTaskName.c_str());
104 if (RT_SUCCESS(vrc))
105 return S_OK;
106
107 mAsync = false;
108 delete this;
109 return E_FAIL;
110}
111
112
113/**
114 * Static method that can get passed to RTThreadCreate to have a
115 * thread started for a Task.
116 */
117/* static */ DECLCALLBACK(int) ThreadTask::taskHandlerThreadProc(RTTHREAD /* thread */, void *pvUser)
118{
119 if (pvUser == NULL)
120 return VERR_INVALID_POINTER; /* nobody cares */
121
122 ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);
123
124 LogFunc(("Started \"%s\"\n", pTask->m_strTaskName.c_str()));
125
126 /*
127 * handler shall catch and process all possible cases as errors and exceptions.
128 */
129 pTask->handler();
130
131 LogFunc(("Ended \"%s\"\n", pTask->m_strTaskName.c_str()));
132
133 delete pTask;
134 return VINF_SUCCESS;
135}
136
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