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