VirtualBox

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

Last change on this file since 70506 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 KB
Line 
1/** @file
2 * Implementation of ThreadTask
3 */
4
5/*
6 * Copyright (C) 2015-2017 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
50 */
51HRESULT ThreadTask::createThread(void)
52{
53 return createThreadInternal(RTTHREADTYPE_MAIN_WORKER);
54}
55
56
57/**
58 * Same ThreadTask::createThread(), except it takes a thread type parameter.
59 *
60 * @param enmType The thread type.
61 */
62HRESULT ThreadTask::createThreadWithType(RTTHREADTYPE enmType)
63{
64 return createThreadInternal(enmType);
65}
66
67
68/**
69 * Internal worker for ThreadTask::createThread,
70 * ThreadTask::createThreadWithType.
71 *
72 * @note Always consumes @a this!
73 */
74HRESULT ThreadTask::createThreadInternal(RTTHREADTYPE enmType)
75{
76 mAsync = true;
77 int vrc = RTThreadCreate(NULL,
78 taskHandlerThreadProc,
79 (void *)this,
80 0,
81 enmType,
82 0,
83 this->getTaskName().c_str());
84 if (RT_SUCCESS(vrc))
85 return S_OK;
86
87 mAsync = false;
88 delete this;
89 return E_FAIL;
90}
91
92
93/**
94 * Static method that can get passed to RTThreadCreate to have a
95 * thread started for a Task.
96 */
97/* static */ DECLCALLBACK(int) ThreadTask::taskHandlerThreadProc(RTTHREAD /* thread */, void *pvUser)
98{
99 if (pvUser == NULL)
100 return VERR_INVALID_POINTER; /* nobody cares */
101
102 ThreadTask *pTask = static_cast<ThreadTask *>(pvUser);
103
104 /*
105 * handler shall catch and process all possible cases as errors and exceptions.
106 */
107 pTask->handler();
108
109 delete pTask;
110 return VINF_SUCCESS;
111}
112
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