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 | #ifndef __Additions_client_thread_h
|
---|
23 | # define __Additions_client_thread_h
|
---|
24 |
|
---|
25 | #include <iprt/thread.h>
|
---|
26 | #include <iprt/err.h>
|
---|
27 |
|
---|
28 | #include <VBox/log.h>
|
---|
29 |
|
---|
30 | class VBoxGuestThread;
|
---|
31 |
|
---|
32 | /** Virtual parent class for thread functions for the VBoxGuestThread class. */
|
---|
33 | class VBoxGuestThreadFunction
|
---|
34 | {
|
---|
35 | public:
|
---|
36 | // VBoxGuestThreadFunction(void);
|
---|
37 | virtual ~VBoxGuestThreadFunction(void) {}
|
---|
38 | /**
|
---|
39 | * The actual thread function.
|
---|
40 | *
|
---|
41 | * @returns iprt status code as thread return value
|
---|
42 | * @param pParent the VBoxGuestThread running this thread function
|
---|
43 | */
|
---|
44 | virtual int threadFunction(VBoxGuestThread *pPThread) = 0;
|
---|
45 | /**
|
---|
46 | * Send a signal to the thread function that it should exit. This should not block.
|
---|
47 | */
|
---|
48 | virtual void stop(void) = 0;
|
---|
49 | };
|
---|
50 |
|
---|
51 | /** C++ wrapper for VBox runtime threads. */
|
---|
52 | class VBoxGuestThread
|
---|
53 | {
|
---|
54 | private:
|
---|
55 | // Private member variables
|
---|
56 | /** The thread function for this thread */
|
---|
57 | VBoxGuestThreadFunction *mFunction;
|
---|
58 | /** The size of the stack for the new thread. Use 0 for the default stack size. */
|
---|
59 | size_t mStack;
|
---|
60 | /** The thread type. Used for deciding scheduling attributes of the thread. */
|
---|
61 | RTTHREADTYPE mType;
|
---|
62 | /** Flags of the RTTHREADFLAGS type (ORed together). */
|
---|
63 | unsigned mFlags;
|
---|
64 | /** Thread name */
|
---|
65 | const char *mName;
|
---|
66 | /** The VBox runtime thread handle. */
|
---|
67 | RTTHREAD mSelf;
|
---|
68 | /** Is the thread currently running? */
|
---|
69 | volatile bool mRunning;
|
---|
70 | /** Should the thread be stopped? */
|
---|
71 | volatile bool mExit;
|
---|
72 |
|
---|
73 | // Typedefs
|
---|
74 | /** Ourselves, for use in the thread function. */
|
---|
75 | typedef VBoxGuestThread *PSELF;
|
---|
76 | public:
|
---|
77 | /**
|
---|
78 | * Initialise the class.
|
---|
79 | * @param pFunction the thread function for this thread
|
---|
80 | * @param cbStack The size of the stack for the new thread.
|
---|
81 | * Use 0 for the default stack size.
|
---|
82 | * @param enmType The thread type. Used for deciding scheduling attributes
|
---|
83 | * of the thread.
|
---|
84 | * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
|
---|
85 | * @param pszName Thread name.
|
---|
86 | */
|
---|
87 | VBoxGuestThread(VBoxGuestThreadFunction *pFunction, size_t cbStack, RTTHREADTYPE enmType,
|
---|
88 | unsigned fFlags, const char *pszName)
|
---|
89 | {
|
---|
90 | mFunction = pFunction;
|
---|
91 | mStack = cbStack;
|
---|
92 | mType = enmType;
|
---|
93 | mFlags = fFlags;
|
---|
94 | mName = pszName;
|
---|
95 | mSelf = NIL_RTTHREAD;
|
---|
96 | mRunning = false;
|
---|
97 | mExit = false;
|
---|
98 | }
|
---|
99 | /** Stop the thread using its stop method and get the exit value.
|
---|
100 | * @returns iprt status code
|
---|
101 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
102 | * an indefinite wait. Only relevant if the thread is
|
---|
103 | * waitable.
|
---|
104 | * @param prc Where to store the return code of the thread. Optional.
|
---|
105 | */
|
---|
106 | int stop(unsigned cMillies, int *prc);
|
---|
107 |
|
---|
108 | /** Destroy the class, stopping the thread if necessary. */
|
---|
109 | ~VBoxGuestThread(void);
|
---|
110 |
|
---|
111 | /** Return the VBox runtime thread handle. */
|
---|
112 | RTTHREAD getSelf(void) { return mSelf; }
|
---|
113 |
|
---|
114 | /** Start the thread. */
|
---|
115 | int start(void);
|
---|
116 |
|
---|
117 | /** Yield the CPU */
|
---|
118 | bool yield(void);
|
---|
119 |
|
---|
120 | /** Is the thread running? */
|
---|
121 | bool isRunning(void) { return mRunning; }
|
---|
122 |
|
---|
123 | /** Should the thread function exit? */
|
---|
124 | bool isStopping(void) { return mExit; }
|
---|
125 | private:
|
---|
126 | // Copying or assigning a thread object is not sensible
|
---|
127 | VBoxGuestThread(const VBoxGuestThread&);
|
---|
128 | VBoxGuestThread& operator=(const VBoxGuestThread&);
|
---|
129 |
|
---|
130 | // Member functions
|
---|
131 | /** The "real" thread function for the VBox runtime. */
|
---|
132 | static DECLCALLBACK(int) threadFunction(RTTHREAD self, void *pvUser);
|
---|
133 | };
|
---|
134 |
|
---|
135 | #endif /* __Additions_client_thread_h not defined */
|
---|