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 | * The function takes ownership of "this" instance (object
|
---|
24 | * instance which calls this function).
|
---|
25 | * And the function is responsible for deletion of "this"
|
---|
26 | * pointer in all cases.
|
---|
27 | * Possible way of usage:
|
---|
28 | *
|
---|
29 | * int vrc = VINF_SUCCESS;
|
---|
30 | * HRESULT hr = S_OK;
|
---|
31 | *
|
---|
32 | * SomeTaskInheritedFromThreadTask* pTask = NULL;
|
---|
33 | * try
|
---|
34 | * {
|
---|
35 | * pTask = new SomeTaskInheritedFromThreadTask(this);
|
---|
36 | * if (!pTask->Init())//some init procedure
|
---|
37 | * {
|
---|
38 | * delete pTask;
|
---|
39 | * throw E_FAIL;
|
---|
40 | * }
|
---|
41 | * //this function delete pTask in case of exceptions, so
|
---|
42 | * there is no need the call of delete operator
|
---|
43 | *
|
---|
44 | * hr = pTask->createThread();
|
---|
45 | * }
|
---|
46 | * catch(...)
|
---|
47 | * {
|
---|
48 | * vrc = E_FAIL;
|
---|
49 | * }
|
---|
50 | */
|
---|
51 | HRESULT ThreadTask::createThread(PRTTHREAD pThread, RTTHREADTYPE enmType)
|
---|
52 | {
|
---|
53 | HRESULT rc = S_OK;
|
---|
54 |
|
---|
55 | m_pThread = pThread;
|
---|
56 | int vrc = RTThreadCreate(m_pThread,
|
---|
57 | taskHandler,
|
---|
58 | (void *)this,
|
---|
59 | 0,
|
---|
60 | enmType,
|
---|
61 | 0,
|
---|
62 | this->getTaskName().c_str());
|
---|
63 |
|
---|
64 | if (RT_FAILURE(vrc))
|
---|
65 | {
|
---|
66 | delete this;
|
---|
67 | return E_FAIL;
|
---|
68 | }
|
---|
69 |
|
---|
70 | return rc;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Static method that can get passed to RTThreadCreate to have a
|
---|
75 | * thread started for a Task.
|
---|
76 | */
|
---|
77 | /* static */ DECLCALLBACK(int) ThreadTask::taskHandler(RTTHREAD /* thread */, void *pvUser)
|
---|
78 | {
|
---|
79 | if (pvUser == NULL)
|
---|
80 | return VERR_INVALID_POINTER;
|
---|
81 |
|
---|
82 | ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * handler shall catch and process all possible cases as errors and exceptions.
|
---|
86 | */
|
---|
87 | pTask->handler();
|
---|
88 |
|
---|
89 | delete pTask;
|
---|
90 |
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | }
|
---|