VirtualBox

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

Last change on this file since 78406 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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