VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/xclient/thread.cpp@ 7023

Last change on this file since 7023 was 6897, checked in by vboxsync, 17 years ago

Additions/x11: VBoxClient cleanups and sanity fixes

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