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