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 | */
|
---|
51 | HRESULT 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 | */
|
---|
62 | HRESULT 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 | */
|
---|
76 | HRESULT 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 | */
|
---|
88 | HRESULT ThreadTask::createThreadInternal(RTTHREADTYPE enmType, PRTTHREAD phThread)
|
---|
89 | {
|
---|
90 | RTTHREAD hThread;
|
---|
91 | int vrc = RTThreadCreate(&hThread,
|
---|
92 | taskHandlerThreadProc,
|
---|
93 | (void *)this,
|
---|
94 | 0,
|
---|
95 | enmType,
|
---|
96 | 0,
|
---|
97 | this->getTaskName().c_str());
|
---|
98 | if (RT_SUCCESS(vrc))
|
---|
99 | {
|
---|
100 | mAsync = true;
|
---|
101 | if (phThread)
|
---|
102 | *phThread = hThread;
|
---|
103 | return S_OK;
|
---|
104 | }
|
---|
105 |
|
---|
106 | delete this;
|
---|
107 | return E_FAIL;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Static method that can get passed to RTThreadCreate to have a
|
---|
113 | * thread started for a Task.
|
---|
114 | */
|
---|
115 | /* static */ DECLCALLBACK(int) ThreadTask::taskHandlerThreadProc(RTTHREAD /* thread */, void *pvUser)
|
---|
116 | {
|
---|
117 | if (pvUser == NULL)
|
---|
118 | return VERR_INVALID_POINTER; /* nobody cares */
|
---|
119 |
|
---|
120 | ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * handler shall catch and process all possible cases as errors and exceptions.
|
---|
124 | */
|
---|
125 | pTask->handler();
|
---|
126 |
|
---|
127 | delete pTask;
|
---|
128 | return VINF_SUCCESS;
|
---|
129 | }
|
---|
130 |
|
---|