1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox additions client application: thread class.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <VBox/log.h>
|
---|
23 | #include <iostream> /* For std::exception */
|
---|
24 |
|
---|
25 | #include "thread.h"
|
---|
26 |
|
---|
27 | /** Stop the thread using its stop method and get the exit value. */
|
---|
28 | int VBoxGuestThread::stop(unsigned cMillies, int *prc)
|
---|
29 | {
|
---|
30 | int rc = VINF_SUCCESS;
|
---|
31 |
|
---|
32 | LogFlowThisFunc(("\n"));
|
---|
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 | LogFlowThisFunc(("returning %Rrc\n", rc));
|
---|
53 | return rc;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** Destroy the class, stopping the thread if necessary. */
|
---|
57 | VBoxGuestThread::~VBoxGuestThread(void)
|
---|
58 | {
|
---|
59 | LogFlowThisFunc(("\n"));
|
---|
60 | if (NIL_RTTHREAD != mSelf)
|
---|
61 | {
|
---|
62 | LogRelThisFunc(("Warning! Stopping thread %s, as it is still running!\n", mName));
|
---|
63 | try
|
---|
64 | {
|
---|
65 | stop(2000, 0);
|
---|
66 | }
|
---|
67 | catch(...) {}
|
---|
68 | }
|
---|
69 | LogFlowThisFunc(("returning\n"));
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Start the thread. */
|
---|
73 | int VBoxGuestThread::start(void)
|
---|
74 | {
|
---|
75 | int rc = VINF_SUCCESS;
|
---|
76 |
|
---|
77 | LogFlowThisFunc(("returning\n"));
|
---|
78 | if (NIL_RTTHREAD != mSelf) /* Assertion */
|
---|
79 | {
|
---|
80 | LogRelThisFunc(("Attempted to start thead %s twice!\n", mName));
|
---|
81 | return VERR_INTERNAL_ERROR;
|
---|
82 | }
|
---|
83 | mExit = false;
|
---|
84 | rc = RTThreadCreate(&mSelf, threadFunction, reinterpret_cast<void *>(this),
|
---|
85 | mStack, mType, mFlags, mName);
|
---|
86 | LogFlowThisFunc(("returning %Rrc\n", rc));
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Yield the CPU */
|
---|
91 | bool VBoxGuestThread::yield(void)
|
---|
92 | {
|
---|
93 | return RTThreadYield();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** The "real" thread function for the VBox runtime. */
|
---|
97 | int VBoxGuestThread::threadFunction(RTTHREAD self, void *pvUser)
|
---|
98 | {
|
---|
99 | int rc = VINF_SUCCESS;
|
---|
100 |
|
---|
101 | LogFlowFunc(("\n"));
|
---|
102 | PSELF pSelf = reinterpret_cast<PSELF>(pvUser);
|
---|
103 | pSelf->mRunning = true;
|
---|
104 | try
|
---|
105 | {
|
---|
106 | rc = pSelf->mFunction->threadFunction(pSelf);
|
---|
107 | }
|
---|
108 | catch (const std::exception &e)
|
---|
109 | {
|
---|
110 | LogRelFunc(("Caught exception in thread: %s\n", e.what()));
|
---|
111 | rc = VERR_UNRESOLVED_ERROR;
|
---|
112 | }
|
---|
113 | catch (...)
|
---|
114 | {
|
---|
115 | LogRelFunc(("Caught unknown exception in thread.\n"));
|
---|
116 | rc = VERR_UNRESOLVED_ERROR;
|
---|
117 | }
|
---|
118 | pSelf->mRunning = false;
|
---|
119 | LogFlowFunc(("returning %Rrc\n", rc));
|
---|
120 | return rc;
|
---|
121 | }
|
---|