1 | /* $Id: ThreadTask.cpp 78742 2019-05-25 13:37:42Z 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 | *
|
---|
55 | * @note Always consumes @a this!
|
---|
56 | */
|
---|
57 | HRESULT ThreadTask::createThread(void)
|
---|
58 | {
|
---|
59 | return createThreadInternal(RTTHREADTYPE_MAIN_WORKER);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Same ThreadTask::createThread(), except it takes a thread type parameter.
|
---|
65 | *
|
---|
66 | * @param enmType The thread type.
|
---|
67 | *
|
---|
68 | * @note Always consumes @a this!
|
---|
69 | */
|
---|
70 | HRESULT ThreadTask::createThreadWithType(RTTHREADTYPE enmType)
|
---|
71 | {
|
---|
72 | return createThreadInternal(enmType);
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Internal worker for ThreadTask::createThread,
|
---|
78 | * ThreadTask::createThreadWithType.
|
---|
79 | *
|
---|
80 | * @note Always consumes @a this!
|
---|
81 | */
|
---|
82 | HRESULT ThreadTask::createThreadInternal(RTTHREADTYPE enmType)
|
---|
83 | {
|
---|
84 | LogThisFunc(("Created \"%s\"\n", m_strTaskName.c_str()));
|
---|
85 |
|
---|
86 | mAsync = true;
|
---|
87 | int vrc = RTThreadCreate(NULL,
|
---|
88 | taskHandlerThreadProc,
|
---|
89 | (void *)this,
|
---|
90 | 0,
|
---|
91 | enmType,
|
---|
92 | 0,
|
---|
93 | m_strTaskName.c_str());
|
---|
94 | if (RT_SUCCESS(vrc))
|
---|
95 | return S_OK;
|
---|
96 |
|
---|
97 | mAsync = false;
|
---|
98 | delete this;
|
---|
99 | return E_FAIL;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Static method that can get passed to RTThreadCreate to have a
|
---|
105 | * thread started for a Task.
|
---|
106 | */
|
---|
107 | /* static */ DECLCALLBACK(int) ThreadTask::taskHandlerThreadProc(RTTHREAD /* thread */, void *pvUser)
|
---|
108 | {
|
---|
109 | if (pvUser == NULL)
|
---|
110 | return VERR_INVALID_POINTER; /* nobody cares */
|
---|
111 |
|
---|
112 | ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);
|
---|
113 |
|
---|
114 | LogFunc(("Started \"%s\"\n", pTask->m_strTaskName.c_str()));
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * handler shall catch and process all possible cases as errors and exceptions.
|
---|
118 | */
|
---|
119 | pTask->handler();
|
---|
120 |
|
---|
121 | LogFunc(("Ended \"%s\"\n", pTask->m_strTaskName.c_str()));
|
---|
122 |
|
---|
123 | delete pTask;
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 |
|
---|