1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox additions client application: thread class.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 <iostream> /* For std::exception */
|
---|
19 |
|
---|
20 | #include "thread.h"
|
---|
21 |
|
---|
22 | /** Stop the thread using its stop method and get the exit value.
|
---|
23 | * @returns iprt status code
|
---|
24 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
25 | * an indefinite wait. Only relevant if the thread is
|
---|
26 | * waitable.
|
---|
27 | * @param prc Where to store the return code of the thread. Optional.
|
---|
28 | */
|
---|
29 | int VBoxGuestThread::stop(unsigned cMillies, int *prc)
|
---|
30 | {
|
---|
31 | int rc = VINF_SUCCESS;
|
---|
32 |
|
---|
33 | if (NIL_RTTHREAD == mSelf) /* Assertion */
|
---|
34 | {
|
---|
35 | LogRelThisFunc(("Attempted to stop thread %s which is not running!\n", mName));
|
---|
36 | return VERR_INTERNAL_ERROR;
|
---|
37 | }
|
---|
38 | mExit = true;
|
---|
39 | mFunction->stop();
|
---|
40 | if (0 != (mFlags & RTTHREADFLAGS_WAITABLE))
|
---|
41 | {
|
---|
42 | rc = RTThreadWait(mSelf, cMillies, prc);
|
---|
43 | if (RT_SUCCESS(rc))
|
---|
44 | {
|
---|
45 | mSelf = NIL_RTTHREAD;
|
---|
46 | }
|
---|
47 | else
|
---|
48 | {
|
---|
49 | LogRelThisFunc(("Failed to stop thread %s!\n", mName));
|
---|
50 | }
|
---|
51 | }
|
---|
52 | return rc;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Destroy the class, stopping the thread if necessary. */
|
---|
56 | VBoxGuestThread::~VBoxGuestThread(void)
|
---|
57 | {
|
---|
58 | if (NIL_RTTHREAD != mSelf)
|
---|
59 | {
|
---|
60 | LogRelThisFunc(("Warning! Stopping thread %s, as it is still running!\n", mName));
|
---|
61 | stop(1000, 0);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Start the thread. */
|
---|
66 | int VBoxGuestThread::start(void)
|
---|
67 | {
|
---|
68 | if (NIL_RTTHREAD != mSelf) /* Assertion */
|
---|
69 | {
|
---|
70 | LogRelThisFunc(("Attempted to start thead %s twice!\n", mName));
|
---|
71 | return VERR_INTERNAL_ERROR;
|
---|
72 | }
|
---|
73 | mExit = false;
|
---|
74 | return RTThreadCreate(&mSelf, threadFunction, reinterpret_cast<void *>(this),
|
---|
75 | mStack, mType, mFlags, mName);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Yield the CPU */
|
---|
79 | bool VBoxGuestThread::yield(void)
|
---|
80 | {
|
---|
81 | return RTThreadYield();
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** The "real" thread function for the VBox runtime. */
|
---|
85 | int VBoxGuestThread::threadFunction(RTTHREAD self, void *pvUser)
|
---|
86 | {
|
---|
87 | int rc = VINF_SUCCESS;
|
---|
88 | PSELF pSelf = reinterpret_cast<PSELF>(pvUser);
|
---|
89 | pSelf->mRunning = true;
|
---|
90 | try
|
---|
91 | {
|
---|
92 | rc = pSelf->mFunction->threadFunction(pSelf);
|
---|
93 | }
|
---|
94 | catch (const std::exception &e)
|
---|
95 | {
|
---|
96 | LogRelFunc(("Caught exception in thread: %s\n", e.what()));
|
---|
97 | rc = VERR_UNRESOLVED_ERROR;
|
---|
98 | }
|
---|
99 | catch (...)
|
---|
100 | {
|
---|
101 | LogRelFunc(("Caught unknown exception in thread.\n"));
|
---|
102 | rc = VERR_UNRESOLVED_ERROR;
|
---|
103 | }
|
---|
104 | pSelf->mRunning = false;
|
---|
105 | return rc;
|
---|
106 | }
|
---|