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