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