VirtualBox

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

Last change on this file since 75267 was 71287, checked in by vboxsync, 7 years ago

Main/ThreadTask: Added own logging group LOG_GROUP_MAIN_THREAD_TASK and related logging to observe threaded tasks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1/** @file
2 * Implementation of ThreadTask
3 */
4
5/*
6 * Copyright (C) 2015-2018 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#define LOG_GROUP LOG_GROUP_MAIN_THREAD_TASK
23#include "LoggingNew.h"
24
25/**
26 * Starts the task (on separate thread), consuming @a this.
27 *
28 * The function takes ownership of "this" instance (object instance which calls
29 * this function). And the function is responsible for deletion of "this"
30 * pointer in all cases.
31 *
32 * Possible way of usage:
33 * @code{.cpp}
34 HRESULT hr;
35 SomeTaskInheritedFromThreadTask *pTask = NULL;
36 try
37 {
38 pTask = new SomeTaskInheritedFromThreadTask(this);
39 if (!pTask->Init()) // some init procedure
40 throw E_FAIL;
41 }
42 catch (...)
43 {
44 if (pTask);
45 delete pTask;
46 return E_FAIL;
47 }
48 return pTask->createThread(); // pTask is always consumed
49 @endcode
50 *
51 * @sa createThreadWithType
52 */
53HRESULT ThreadTask::createThread(void)
54{
55 return createThreadInternal(RTTHREADTYPE_MAIN_WORKER);
56}
57
58
59/**
60 * Same ThreadTask::createThread(), except it takes a thread type parameter.
61 *
62 * @param enmType The thread type.
63 */
64HRESULT ThreadTask::createThreadWithType(RTTHREADTYPE enmType)
65{
66 return createThreadInternal(enmType);
67}
68
69
70/**
71 * Internal worker for ThreadTask::createThread,
72 * ThreadTask::createThreadWithType.
73 *
74 * @note Always consumes @a this!
75 */
76HRESULT ThreadTask::createThreadInternal(RTTHREADTYPE enmType)
77{
78 LogThisFunc(("Created \"%s\"\n", m_strTaskName.c_str()));
79
80 mAsync = true;
81 int vrc = RTThreadCreate(NULL,
82 taskHandlerThreadProc,
83 (void *)this,
84 0,
85 enmType,
86 0,
87 m_strTaskName.c_str());
88 if (RT_SUCCESS(vrc))
89 return S_OK;
90
91 mAsync = false;
92 delete this;
93 return E_FAIL;
94}
95
96
97/**
98 * Static method that can get passed to RTThreadCreate to have a
99 * thread started for a Task.
100 */
101/* static */ DECLCALLBACK(int) ThreadTask::taskHandlerThreadProc(RTTHREAD /* thread */, void *pvUser)
102{
103 if (pvUser == NULL)
104 return VERR_INVALID_POINTER; /* nobody cares */
105
106 ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);
107
108 LogFunc(("Started \"%s\"\n", pTask->m_strTaskName.c_str()));
109
110 /*
111 * handler shall catch and process all possible cases as errors and exceptions.
112 */
113 pTask->handler();
114
115 LogFunc(("Ended \"%s\"\n", pTask->m_strTaskName.c_str()));
116
117 delete pTask;
118 return VINF_SUCCESS;
119}
120
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