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