1 | /* $Id: HGCMThread.h 98278 2023-01-24 11:55:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HGCMThread - Host-Guest Communication Manager worker threads header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef MAIN_INCLUDED_HGCMThread_h
|
---|
29 | #define MAIN_INCLUDED_HGCMThread_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <VBox/types.h>
|
---|
35 |
|
---|
36 | #include "HGCMObjects.h"
|
---|
37 |
|
---|
38 | /* Forward declaration of the worker thread class. */
|
---|
39 | class HGCMThread;
|
---|
40 |
|
---|
41 | /** A handle for HGCM message. */
|
---|
42 | typedef uint32_t HGCMMSGHANDLE;
|
---|
43 |
|
---|
44 | /* Forward declaration of message core class. */
|
---|
45 | class HGCMMsgCore;
|
---|
46 |
|
---|
47 | /** @todo comment */
|
---|
48 |
|
---|
49 | typedef HGCMMsgCore *FNHGCMNEWMSGALLOC(uint32_t u32MsgId);
|
---|
50 | typedef FNHGCMNEWMSGALLOC *PFNHGCMNEWMSGALLOC;
|
---|
51 |
|
---|
52 | /** Function that is called after message processing by worker thread,
|
---|
53 | * or if an error occurred during message handling after successfully
|
---|
54 | * posting (hgcmMsgPost) the message to worker thread.
|
---|
55 | *
|
---|
56 | * @param result Return code either from the service which actually processed the message
|
---|
57 | * or from HGCM.
|
---|
58 | * @param pMsgCore Pointer to just processed message.
|
---|
59 | *
|
---|
60 | * @return Restricted set of VBox status codes when guest call message:
|
---|
61 | * @retval VINF_SUCCESS on success
|
---|
62 | * @retval VERR_CANCELLED if the request was cancelled.
|
---|
63 | * @retval VERR_ALREADY_RESET if the VM is resetting.
|
---|
64 | * @retval VERR_NOT_AVAILABLE if HGCM has been disconnected from the VMMDev
|
---|
65 | * (shouldn't happen).
|
---|
66 | */
|
---|
67 | typedef DECLCALLBACKTYPE(int, FNHGCMMSGCALLBACK,(int32_t result, HGCMMsgCore *pMsgCore));
|
---|
68 | /** Pointer to a message completeion callback function. */
|
---|
69 | typedef FNHGCMMSGCALLBACK *PFNHGCMMSGCALLBACK;
|
---|
70 |
|
---|
71 |
|
---|
72 | /** HGCM core message. */
|
---|
73 | class HGCMMsgCore : public HGCMReferencedObject
|
---|
74 | {
|
---|
75 | private:
|
---|
76 | friend class HGCMThread;
|
---|
77 |
|
---|
78 | /** Version of message header. */
|
---|
79 | uint32_t m_u32Version;
|
---|
80 |
|
---|
81 | /** Message number/identifier. */
|
---|
82 | uint32_t m_u32Msg;
|
---|
83 |
|
---|
84 | /** Thread the message belongs to, referenced by the message. */
|
---|
85 | HGCMThread *m_pThread;
|
---|
86 |
|
---|
87 | /** Callback function pointer. */
|
---|
88 | PFNHGCMMSGCALLBACK m_pfnCallback;
|
---|
89 |
|
---|
90 | /** Next element in a message queue. */
|
---|
91 | HGCMMsgCore *m_pNext;
|
---|
92 | /** Previous element in a message queue.
|
---|
93 | * @todo seems not necessary. */
|
---|
94 | HGCMMsgCore *m_pPrev;
|
---|
95 |
|
---|
96 | /** Various internal flags. */
|
---|
97 | uint32_t m_fu32Flags;
|
---|
98 |
|
---|
99 | /** Result code for a Send */
|
---|
100 | int32_t m_vrcSend;
|
---|
101 |
|
---|
102 | protected:
|
---|
103 | void InitializeCore(uint32_t u32MsgId, HGCMThread *pThread);
|
---|
104 |
|
---|
105 | virtual ~HGCMMsgCore();
|
---|
106 |
|
---|
107 | public:
|
---|
108 | HGCMMsgCore() : HGCMReferencedObject(HGCMOBJ_MSG) {};
|
---|
109 |
|
---|
110 | uint32_t MsgId(void) { return m_u32Msg; };
|
---|
111 |
|
---|
112 | HGCMThread *Thread(void) { return m_pThread; };
|
---|
113 |
|
---|
114 | /** Initialize message after it was allocated. */
|
---|
115 | virtual void Initialize(void) {};
|
---|
116 |
|
---|
117 | /** Uninitialize message. */
|
---|
118 | virtual void Uninitialize(void) {};
|
---|
119 | };
|
---|
120 |
|
---|
121 |
|
---|
122 | /** HGCM worker thread function.
|
---|
123 | *
|
---|
124 | * @param pThread The HGCM thread instance.
|
---|
125 | * @param pvUser User specified thread parameter.
|
---|
126 | */
|
---|
127 | typedef DECLCALLBACKTYPE(void, FNHGCMTHREAD,(HGCMThread *pThread, void *pvUser));
|
---|
128 | typedef FNHGCMTHREAD *PFNHGCMTHREAD;
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Thread API.
|
---|
133 | * Based on thread handles. Internals of a thread are not exposed to users.
|
---|
134 | */
|
---|
135 |
|
---|
136 | /** Initialize threads.
|
---|
137 | *
|
---|
138 | * @return VBox status code.
|
---|
139 | */
|
---|
140 | int hgcmThreadInit(void);
|
---|
141 | void hgcmThreadUninit(void);
|
---|
142 |
|
---|
143 |
|
---|
144 | /** Create a HGCM worker thread.
|
---|
145 | *
|
---|
146 | * @param ppThread Where to return the pointer to the worker thread.
|
---|
147 | * @param pszThreadName Name of the thread, needed by runtime.
|
---|
148 | * @param pfnThread The worker thread function.
|
---|
149 | * @param pvUser A pointer passed to worker thread.
|
---|
150 | * @param pszStatsSubDir The "sub-directory" under "/HGCM/" where thread
|
---|
151 | * statistics should be registered. The caller,
|
---|
152 | * HGCMService, will deregister them. NULL if no stats.
|
---|
153 | * @param pUVM The user mode VM handle to register statistics with.
|
---|
154 | * NULL if no stats.
|
---|
155 | * @param pVMM The VMM vtable for statistics registration. NULL if
|
---|
156 | * no stats.
|
---|
157 | *
|
---|
158 | * @return VBox status code.
|
---|
159 | */
|
---|
160 | int hgcmThreadCreate(HGCMThread **ppThread, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser,
|
---|
161 | const char *pszStatsSubDir, PUVM pUVM, PCVMMR3VTABLE pVMM);
|
---|
162 |
|
---|
163 | /** Wait for termination of a HGCM worker thread.
|
---|
164 | *
|
---|
165 | * @param pThread The HGCM thread. The passed in reference is always
|
---|
166 | * consumed.
|
---|
167 | *
|
---|
168 | * @return VBox status code.
|
---|
169 | */
|
---|
170 | int hgcmThreadWait(HGCMThread *pThread);
|
---|
171 |
|
---|
172 | /** Allocate a message to be posted to HGCM worker thread.
|
---|
173 | *
|
---|
174 | * @param pThread The HGCM worker thread.
|
---|
175 | * @param ppHandle Where to store the pointer to the new message.
|
---|
176 | * @param u32MsgId Message identifier.
|
---|
177 | * @param pfnNewMessage New message allocation callback.
|
---|
178 | *
|
---|
179 | * @return VBox status code.
|
---|
180 | */
|
---|
181 | int hgcmMsgAlloc(HGCMThread *pThread, HGCMMsgCore **ppHandle, uint32_t u32MsgId, PFNHGCMNEWMSGALLOC pfnNewMessage);
|
---|
182 |
|
---|
183 | /** Post a message to HGCM worker thread.
|
---|
184 | *
|
---|
185 | * @param pMsg The message. Reference will be consumed!
|
---|
186 | * @param pfnCallback Message completion callback.
|
---|
187 | *
|
---|
188 | * @return VBox status code.
|
---|
189 | * @retval VINF_HGCM_ASYNC_EXECUTE on success.
|
---|
190 | *
|
---|
191 | * @thread any
|
---|
192 | */
|
---|
193 | int hgcmMsgPost(HGCMMsgCore *pMsg, PFNHGCMMSGCALLBACK pfnCallback);
|
---|
194 |
|
---|
195 | /** Send a message to HGCM worker thread.
|
---|
196 | *
|
---|
197 | * The function will return after message is processed by thread.
|
---|
198 | *
|
---|
199 | * @param pMsg The message. Reference will be consumed!
|
---|
200 | *
|
---|
201 | * @return VBox status code.
|
---|
202 | *
|
---|
203 | * @thread any
|
---|
204 | */
|
---|
205 | int hgcmMsgSend(HGCMMsgCore *pMsg);
|
---|
206 |
|
---|
207 |
|
---|
208 | /* Wait for and get a message.
|
---|
209 | *
|
---|
210 | * @param pThread The HGCM worker thread.
|
---|
211 | * @param ppMsg Where to store returned message pointer.
|
---|
212 | *
|
---|
213 | * @return VBox status code.
|
---|
214 | *
|
---|
215 | * @thread worker thread
|
---|
216 | */
|
---|
217 | int hgcmMsgGet(HGCMThread *pThread, HGCMMsgCore **ppMsg);
|
---|
218 |
|
---|
219 |
|
---|
220 | /** Worker thread has processed a message previously obtained with hgcmMsgGet.
|
---|
221 | *
|
---|
222 | * @param pMsg Processed message pointer.
|
---|
223 | * @param result Result code, VBox status code.
|
---|
224 | *
|
---|
225 | * @return Restricted set of VBox status codes when guest call message:
|
---|
226 | * @retval VINF_SUCCESS on success
|
---|
227 | * @retval VERR_CANCELLED if the request was cancelled.
|
---|
228 | * @retval VERR_ALREADY_RESET if the VM is resetting.
|
---|
229 | * @retval VERR_NOT_AVAILABLE if HGCM has been disconnected from the VMMDev
|
---|
230 | * (shouldn't happen).
|
---|
231 | *
|
---|
232 | * @thread worker thread
|
---|
233 | */
|
---|
234 | int hgcmMsgComplete(HGCMMsgCore *pMsg, int32_t result);
|
---|
235 |
|
---|
236 |
|
---|
237 | #endif /* !MAIN_INCLUDED_HGCMThread_h */
|
---|
238 |
|
---|