VirtualBox

source: vbox/trunk/src/VBox/Main/hgcm/HGCMThread.cpp@ 703

Last change on this file since 703 was 313, checked in by vboxsync, 18 years ago

DECLINLINE doesn't work for C++ anylonger.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.5 KB
Line 
1/** @file
2 *
3 * HGCM (Host-Guest Communication Manager):
4 * HGCMThread - Host-Guest Communication Manager Threads
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23#define LOG_GROUP_MAIN_OVERRIDE LOG_GROUP_HGCM
24#include "Logging.h"
25
26#include "hgcm/HGCMThread.h"
27
28#include <VBox/err.h>
29#include <iprt/semaphore.h>
30#include <iprt/thread.h>
31#include <iprt/string.h>
32
33
34/* HGCM uses worker threads, which process messages from other threads.
35 * A message consists of the message header and message specific data.
36 * Message header is opaque for callers, but message data is defined
37 * and used by them.
38 *
39 * Messages are distinguished by message identifier and worker thread
40 * they are allocated for.
41 *
42 * Messages are allocated for a worker thread and belong to
43 * the thread. A worker thread holds the queue of messages.
44 *
45 * The calling thread creates a message, specifying which worker thread
46 * the message is created for, then, optionally, initializes message
47 * specific data and, also optionally, references the message.
48 *
49 * Message then is posted or sent to worker thread by inserting
50 * it to the worker thread message queue and referencing the message.
51 * Worker thread then again may fetch next message.
52 *
53 * Upon processing the message the worker thread dereferences it.
54 * Dereferencing also automatically deletes message from the thread
55 * queue and frees memory allocated for the message, if no more
56 * references left. If there are references, the message remains
57 * in the queue.
58 *
59 */
60
61/* Version of HGCM message header */
62#define HGCMMSG_VERSION (1)
63
64/* Thread is initializing. */
65#define HGCMMSG_TF_INITIALIZING (0x00000001)
66/* Thread must be terminated. */
67#define HGCMMSG_TF_TERMINATE (0x00000002)
68/* Thread has been terminated. */
69#define HGCMMSG_TF_TERMINATED (0x00000004)
70
71
72static DECLCALLBACK(int) hgcmWorkerThreadFunc (RTTHREAD ThreadSelf, void *pvUser);
73
74class HGCMThread: public HGCMObject
75{
76 private:
77 friend DECLCALLBACK(int) hgcmWorkerThreadFunc (RTTHREAD ThreadSelf, void *pvUser);
78
79 /* Worker thread function. */
80 PFNHGCMTHREAD m_pfnThread;
81
82 /* A user supplied thread parameter. */
83 void *m_pvUser;
84
85 /* The thread runtime handle. */
86 RTTHREAD m_thread;
87
88 /* Event the thread waits for, signalled when a message
89 * to process is posted to the thread.
90 */
91 RTSEMEVENTMULTI m_eventThread;
92
93 /* A caller thread waits for completion of a SENT message on this event. */
94 RTSEMEVENTMULTI m_eventSend;
95
96 /* Critical section for accessing the thread data, mostly for message queues. */
97 RTCRITSECT m_critsect;
98
99 /* thread state/operation flags */
100 uint32_t m_fu32ThreadFlags;
101
102 /* Typical message size for this thread, messages with
103 * this size will be put in the Free list and reused,
104 * instead of reallocating. Should speed up processing
105 * in cases when the message size is fixed.
106 */
107 uint32_t m_cbMsg;
108
109 /* Message queue variables. Messages are inserted at tail of message
110 * queue. They are consumed by worker thread sequently. If a message was
111 * consumed, it is removed from message queue.
112 */
113
114 /* Head of message queue. */
115 HGCMMsgCore *m_pMsgInputQueueHead;
116 /* Message which another message will be inserted after. */
117 HGCMMsgCore *m_pMsgInputQueueTail;
118
119 /* Head of messages being processed queue. */
120 HGCMMsgCore *m_pMsgInProcessHead;
121 /* Message which another message will be inserted after. */
122 HGCMMsgCore *m_pMsgInProcessTail;
123
124 /* Head of free message structures list. */
125 HGCMMsgCore *m_pFreeHead;
126 /* Tail of free message structures list. */
127 HGCMMsgCore *m_pFreeTail;
128
129
130 inline int Enter (void);
131 inline void Leave (void);
132
133 HGCMMsgCore *FetchFreeListHead (void);
134
135 protected:
136 virtual ~HGCMThread (void);
137
138 public:
139
140 HGCMThread ();
141
142 int Initialize (HGCMTHREADHANDLE handle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser, uint32_t cbMsg);
143
144 int MsgAlloc (HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, uint32_t cbMsg, PFNHGCMNEWMSGALLOC pfnNewMessage);
145 int MsgGet (HGCMMsgCore **ppMsg);
146 int MsgPost (HGCMMsgCore *pMsg, PHGCMMSGCALLBACK pfnCallback, bool bWait);
147 void MsgComplete (HGCMMsgCore *pMsg, int32_t result);
148
149 bool MsgReuse (HGCMMsgCore *pMsg);
150};
151
152
153/*
154 * HGCMMsgCore implementation.
155 */
156
157#define HGCM_MSG_F_PROCESSED (0x00000001)
158#define HGCM_MSG_F_WAIT (0x00000002)
159#define HGCM_MSG_F_IN_PROCESS (0x00000004)
160
161void HGCMMsgCore::InitializeCore (uint32_t cbMsg, uint32_t u32MsgId, HGCMThread *pThread)
162{
163 m_cbMsg = cbMsg;
164 m_u32Version = HGCMMSG_VERSION;
165 m_u32Msg = u32MsgId;
166 m_pfnCallback = NULL;
167 m_pNext = NULL;
168 m_pPrev = NULL;
169 m_fu32Flags = 0;
170 m_rcSend = VINF_SUCCESS;
171
172 m_pThread = pThread;
173}
174
175bool HGCMMsgCore::Reuse (void)
176{
177 return m_pThread->MsgReuse (this);
178}
179
180
181/*
182 * HGCMThread implementation.
183 */
184
185static DECLCALLBACK(int) hgcmWorkerThreadFunc (RTTHREAD ThreadSelf, void *pvUser)
186{
187 int rc = VINF_SUCCESS;
188
189 HGCMThread *pThread = (HGCMThread *)pvUser;
190
191 LogFlow(("MAIN::hgcmWorkerThreadFunc: starting HGCM thread %p\n", pThread));
192
193 AssertRelease(pThread);
194
195 pThread->m_thread = ThreadSelf;
196 pThread->m_fu32ThreadFlags &= ~HGCMMSG_TF_INITIALIZING;
197 rc = RTThreadUserSignal (ThreadSelf);
198 AssertRC (rc);
199
200 pThread->m_pfnThread (pThread->Handle (), pThread->m_pvUser);
201
202 pThread->m_fu32ThreadFlags |= HGCMMSG_TF_TERMINATED;
203
204 hgcmObjDeleteHandle (pThread->Handle ());
205
206 LogFlow(("MAIN::hgcmWorkerThreadFunc: completed HGCM thread %p\n", pThread));
207
208 return rc;
209}
210
211HGCMThread::HGCMThread ()
212 :
213 m_pfnThread (NULL),
214 m_pvUser (NULL),
215 m_thread (NIL_RTTHREAD),
216 m_eventThread (0),
217 m_eventSend (0),
218 m_fu32ThreadFlags (0),
219 m_cbMsg (0),
220 m_pMsgInputQueueHead (NULL),
221 m_pMsgInputQueueTail (NULL),
222 m_pMsgInProcessHead (NULL),
223 m_pMsgInProcessTail (NULL),
224 m_pFreeHead (NULL),
225 m_pFreeTail (NULL)
226{
227 memset (&m_critsect, 0, sizeof (m_critsect));
228}
229
230HGCMThread::~HGCMThread ()
231{
232 /*
233 * Free resources allocated for the thread.
234 */
235
236 if (RTCritSectIsInitialized (&m_critsect))
237 {
238 RTCritSectDelete (&m_critsect);
239 }
240
241 if (m_eventSend)
242 {
243 RTSemEventMultiDestroy (m_eventSend);
244 }
245
246 if (m_eventThread)
247 {
248 RTSemEventMultiDestroy (m_eventThread);
249 }
250
251 /*
252 * Wait for the thread to terminate, but let's not wait forever.
253 */
254 if ( m_thread != NIL_RTTHREAD
255 && !(m_fu32ThreadFlags & HGCMMSG_TF_TERMINATED))
256 {
257 int rc = RTThreadWait (m_thread, 5000, NULL);
258 AssertRC (rc);
259 }
260
261 return;
262}
263
264int HGCMThread::Initialize (HGCMTHREADHANDLE handle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser, uint32_t cbMsg)
265{
266 int rc = VINF_SUCCESS;
267
268 rc = RTSemEventMultiCreate (&m_eventThread);
269
270 if (VBOX_SUCCESS(rc))
271 {
272 rc = RTSemEventMultiCreate (&m_eventSend);
273
274 if (VBOX_SUCCESS(rc))
275 {
276 rc = RTCritSectInit (&m_critsect);
277
278 if (VBOX_SUCCESS(rc))
279 {
280 m_pfnThread = pfnThread;
281 m_pvUser = pvUser;
282 m_cbMsg = cbMsg;
283
284 m_fu32ThreadFlags = HGCMMSG_TF_INITIALIZING;
285
286 RTTHREAD thread;
287 rc = RTThreadCreate (&thread, hgcmWorkerThreadFunc, this, 64 * _1K,
288 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE,
289 pszThreadName);
290
291 if (VBOX_SUCCESS(rc))
292 {
293 /* Wait until the thread is ready. */
294 rc = RTThreadUserWait (thread, 30000);
295 AssertRC (rc);
296 Assert (!(m_fu32ThreadFlags & HGCMMSG_TF_INITIALIZING) || VBOX_FAILURE (rc));
297 }
298 else
299 {
300 m_thread = NIL_RTTHREAD;
301 Log(("hgcmThreadCreate: FAILURE: Can't start worker thread.\n"));
302 }
303 }
304 else
305 {
306 Log(("hgcmThreadCreate: FAILURE: Can't init a critical section for a hgcm worker thread.\n"));
307 memset (&m_critsect, 0, sizeof (m_critsect));
308 }
309 }
310 else
311 {
312 Log(("hgcmThreadCreate: FAILURE: Can't create an event semaphore for a sent messages.\n"));
313 m_eventSend = 0;
314 }
315 }
316 else
317 {
318 Log(("hgcmThreadCreate: FAILURE: Can't create an event semaphore for a hgcm worker thread.\n"));
319 m_eventThread = 0;
320 }
321
322 return rc;
323}
324
325inline int HGCMThread::Enter (void)
326{
327 int rc = RTCritSectEnter (&m_critsect);
328
329#ifdef LOG_ENABLED
330 if (VBOX_FAILURE (rc))
331 {
332 Log(("HGCMThread::MsgPost: FAILURE: could not obtain worker thread mutex, rc = %Vrc!!!\n", rc));
333 }
334#endif /* LOG_ENABLED */
335
336 return rc;
337}
338
339inline void HGCMThread::Leave (void)
340{
341 RTCritSectLeave (&m_critsect);
342}
343
344
345int HGCMThread::MsgAlloc (HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, uint32_t cbMsg, PFNHGCMNEWMSGALLOC pfnNewMessage)
346{
347 int rc = VINF_SUCCESS;
348
349 HGCMMsgCore *pmsg = NULL;
350
351 bool fFromFreeList = false;
352
353 if (cbMsg == m_cbMsg)
354 {
355 rc = Enter ();
356
357 if (VBOX_SUCCESS(rc))
358 {
359 /* May be we can reuse a previously allocated message memory block. */
360
361 if (m_pFreeHead)
362 {
363 /* Take existing message object from the head of the free list. */
364 pmsg = m_pFreeHead;
365
366 m_pFreeHead = m_pFreeHead->m_pNext;
367
368 if (m_pFreeHead == NULL)
369 {
370 m_pFreeTail = NULL;
371 }
372
373 fFromFreeList = true;
374 }
375
376 Leave ();
377 }
378 }
379
380 if (!pmsg && VBOX_SUCCESS(rc))
381 {
382 /* We have to allocate a new memory block. */
383 pmsg = pfnNewMessage (u32MsgId);
384
385 if (pmsg == NULL)
386 {
387 rc = VERR_NO_MEMORY;
388 }
389 }
390
391 if (VBOX_SUCCESS(rc))
392 {
393 /* Reference the thread because pMsg contains the pointer to it. */
394 Reference ();
395
396 /* Initialize just allocated or reused message core */
397 pmsg->InitializeCore (cbMsg, u32MsgId, this);
398
399 /* and the message specific data. */
400 pmsg->Initialize ();
401
402 LogFlow(("MAIN::hgcmMsgAlloc: allocated message %p\n", pmsg));
403
404 /** Get handle of the message. The message will be also referenced
405 * until the handle is deleted.
406 */
407 *pHandle = hgcmObjGenerateHandle (pmsg);
408
409 if (fFromFreeList)
410 {
411 /* Message was referenced in the free list, now dereference it. */
412 pmsg->Dereference ();
413 }
414 }
415
416 return rc;
417}
418
419int HGCMThread::MsgPost (HGCMMsgCore *pMsg, PHGCMMSGCALLBACK pfnCallback, bool fWait)
420{
421 int rc = VINF_SUCCESS;
422
423 LogFlow(("HGCMThread::MsgPost: thread = %p, pMsg = %p, pfnCallback = %p\n", this, pMsg, pfnCallback));
424
425 rc = Enter ();
426
427 if (VBOX_SUCCESS(rc))
428 {
429 pMsg->m_pfnCallback = pfnCallback;
430
431 /* Insert the message to the queue tail. */
432 pMsg->m_pNext = NULL;
433 pMsg->m_pPrev = m_pMsgInputQueueTail;
434
435 if (m_pMsgInputQueueTail)
436 {
437 m_pMsgInputQueueTail->m_pNext = pMsg;
438 }
439 else
440 {
441 m_pMsgInputQueueHead = pMsg;
442 }
443
444 m_pMsgInputQueueTail = pMsg;
445
446 Leave ();
447
448 LogFlow(("HGCMThread::MsgPost: going to inform the thread %p about message, fWait = %d\n", this, fWait));
449
450 if (fWait)
451 {
452 pMsg->m_fu32Flags |= HGCM_MSG_F_WAIT;
453 }
454
455 /* Inform the worker thread that there is a message. */
456 RTSemEventMultiSignal (m_eventThread);
457
458 LogFlow(("HGCMThread::MsgPost: event signalled\n"));
459
460 if (fWait)
461 {
462 while ((pMsg->m_fu32Flags & HGCM_MSG_F_PROCESSED) == 0)
463 {
464 RTSemEventMultiWait (m_eventSend, RT_INDEFINITE_WAIT);
465 RTSemEventMultiReset (m_eventSend);
466
467 LogFlow(("HGCMThread::MsgPost: wait completed flags = %08X\n", pMsg->m_fu32Flags));
468 }
469
470 rc = pMsg->m_rcSend;
471 }
472 }
473
474 LogFlow(("HGCMThread::MsgPost: rc = %Vrc\n", rc));
475
476 return rc;
477}
478
479
480int HGCMThread::MsgGet (HGCMMsgCore **ppMsg)
481{
482 int rc = VINF_SUCCESS;
483
484 LogFlow(("HGCMThread::MsgGet: thread = %p, ppMsg = %p\n", this, ppMsg));
485
486 for (;;)
487 {
488 if (m_fu32ThreadFlags & HGCMMSG_TF_TERMINATE)
489 {
490 rc = VERR_INTERRUPTED;
491 break;
492 }
493
494 LogFlow(("MAIN::hgcmMsgGet: m_pMsgInputQueueHead = %p\n", m_pMsgInputQueueHead));
495
496 if (m_pMsgInputQueueHead)
497 {
498 /* Move the message to the m_pMsgInProcessHead list */
499 rc = Enter ();
500
501 if (VBOX_FAILURE (rc))
502 {
503 break;
504 }
505
506 HGCMMsgCore *pMsg = m_pMsgInputQueueHead;
507
508 /* Remove the message from the head of Queue list. */
509 Assert (m_pMsgInputQueueHead->m_pPrev == NULL);
510
511 if (m_pMsgInputQueueHead->m_pNext)
512 {
513 m_pMsgInputQueueHead = m_pMsgInputQueueHead->m_pNext;
514 m_pMsgInputQueueHead->m_pPrev = NULL;
515 }
516 else
517 {
518 Assert (m_pMsgInputQueueHead == m_pMsgInputQueueTail);
519
520 m_pMsgInputQueueHead = NULL;
521 m_pMsgInputQueueTail = NULL;
522 }
523
524 /* Insert the message to the tail of the m_pMsgInProcessHead list. */
525 pMsg->m_pNext = NULL;
526 pMsg->m_pPrev = m_pMsgInProcessTail;
527
528 if (m_pMsgInProcessTail)
529 {
530 m_pMsgInProcessTail->m_pNext = pMsg;
531 }
532 else
533 {
534 m_pMsgInProcessHead = pMsg;
535 }
536
537 m_pMsgInProcessTail = pMsg;
538
539 pMsg->m_fu32Flags |= HGCM_MSG_F_IN_PROCESS;
540
541 Leave ();
542
543 /* Return the message to the caller. */
544 *ppMsg = pMsg;
545
546 LogFlow(("MAIN::hgcmMsgGet: got message %p\n", *ppMsg));
547
548 break;
549 }
550
551 /* Wait for an event. */
552 RTSemEventMultiWait (m_eventThread, RT_INDEFINITE_WAIT);
553 RTSemEventMultiReset (m_eventThread);
554 }
555
556 LogFlow(("HGCMThread::MsgGet: *ppMsg = %p, return rc = %Vrc\n", *ppMsg, rc));
557
558 return rc;
559}
560
561void HGCMThread::MsgComplete (HGCMMsgCore *pMsg, int32_t result)
562{
563 LogFlow(("HGCMThread::MsgComplete: thread = %p, pMsg = %p\n", this, pMsg));
564
565 int rc = VINF_SUCCESS;
566
567 AssertRelease(pMsg->m_pThread == this);
568 AssertRelease((pMsg->m_fu32Flags & HGCM_MSG_F_IN_PROCESS) != 0);
569
570 if (pMsg->m_pfnCallback)
571 {
572 /** @todo call callback with error code in MsgPost in case of errors */
573
574 pMsg->m_pfnCallback (result, pMsg);
575
576 LogFlow(("HGCMThread::MsgComplete: callback executed. pMsg = %p, thread = %p\n", pMsg, this));
577 }
578
579 /* Message processing has been completed. */
580
581 rc = Enter ();
582
583 if (VBOX_SUCCESS(rc))
584 {
585 /* Remove the message from the InProcess queue. */
586
587 if (pMsg->m_pNext)
588 {
589 pMsg->m_pNext->m_pPrev = pMsg->m_pPrev;
590 }
591 else
592 {
593 m_pMsgInProcessTail = pMsg->m_pPrev;
594 }
595
596 if (pMsg->m_pPrev)
597 {
598 pMsg->m_pPrev->m_pNext = pMsg->m_pNext;
599 }
600 else
601 {
602 m_pMsgInProcessHead = pMsg->m_pNext;
603 }
604
605 pMsg->m_pNext = NULL;
606 pMsg->m_pPrev = NULL;
607
608 bool fWaited = ((pMsg->m_fu32Flags & HGCM_MSG_F_WAIT) != 0);
609
610 /* The message is now completed. */
611 pMsg->m_fu32Flags &= ~HGCM_MSG_F_IN_PROCESS;
612 pMsg->m_fu32Flags &= ~HGCM_MSG_F_WAIT;
613 pMsg->m_fu32Flags |= HGCM_MSG_F_PROCESSED;
614
615 Leave ();
616
617 if (fWaited)
618 {
619 pMsg->m_rcSend = result;
620
621 /* Wake up all waiters. so they can decide if their message has been processed. */
622 RTSemEventMultiSignal (m_eventSend);
623 }
624
625 hgcmObjDeleteHandle (pMsg->Handle ());
626 }
627
628 return;
629}
630
631
632bool HGCMThread::MsgReuse (HGCMMsgCore *pMsg)
633{
634 if (pMsg->m_cbMsg != m_cbMsg)
635 {
636 /* Message no longer belong to the thread. */
637 Dereference ();
638
639 return false;
640 }
641
642 int rc = Enter ();
643
644 if (VBOX_SUCCESS (rc))
645 {
646 /* Put the message to the tail of free list. */
647
648 /* Reference message because it will still be in free list. */
649 pMsg->Reference ();
650
651 pMsg->Uninitialize ();
652
653 if (m_pFreeTail)
654 {
655 m_pFreeTail->m_pNext = pMsg;
656 }
657 else
658 {
659 m_pFreeHead = pMsg;
660 }
661
662 m_pFreeTail = pMsg;
663
664 Leave ();
665
666 /* Dereference the thread. */
667 Dereference ();
668 }
669
670 return true;
671}
672
673
674
675/*
676 * Thread API. Public interface.
677 */
678
679int hgcmThreadCreate (HGCMTHREADHANDLE *pHandle, const char *pszThreadName, PFNHGCMTHREAD pfnThread, void *pvUser, uint32_t cbMsg)
680{
681 int rc = VINF_SUCCESS;
682
683 LogFlow(("MAIN::hgcmThreadCreate\n"));
684
685 HGCMTHREADHANDLE handle = 0;
686
687 /* Allocate memory for a new thread object. */
688 HGCMThread *pThread = new HGCMThread ();
689
690 if (pThread)
691 {
692 /* Put just created object to pool and obtain handle for it. */
693 handle = hgcmObjGenerateHandle (pThread);
694
695 /* Initialize the object. */
696 rc = pThread->Initialize (handle, pszThreadName, pfnThread, pvUser, cbMsg);
697 }
698 else
699 {
700 Log(("hgcmThreadCreate: FAILURE: Can't allocate memory for a hgcm worker thread.\n"));
701 rc = VERR_NO_MEMORY;
702 }
703
704 if (VBOX_SUCCESS (rc))
705 {
706 *pHandle = handle;
707 }
708 else
709 {
710 Log(("hgcmThreadCreate: FAILURE: rc = %Vrc.\n", rc));
711
712 if (handle != 0)
713 {
714 /* Delete allocated handle, this will also free the object memory. */
715 hgcmObjDeleteHandle (handle);
716 }
717 }
718
719 LogFlow(("MAIN::hgcmThreadCreate: rc = %Vrc\n", rc));
720
721 return rc;
722}
723
724int hgcmMsgAlloc (HGCMTHREADHANDLE hThread, HGCMMSGHANDLE *pHandle, uint32_t u32MsgId, uint32_t cbMsg, PFNHGCMNEWMSGALLOC pfnNewMessage)
725{
726 LogFlow(("hgcmMsgAlloc: thread handle = %d, pHandle = %p, cbMsg = %d, sizeof (HGCMMsgCore) = %d\n", hThread, pHandle, cbMsg, sizeof (HGCMMsgCore)));
727
728 if (!pHandle || cbMsg < sizeof (HGCMMsgCore))
729 {
730 return VERR_INVALID_PARAMETER;
731 }
732
733 int rc = VINF_SUCCESS;
734
735 HGCMThread *pThread = (HGCMThread *)hgcmObjReference (hThread);
736
737 if (!pThread)
738 {
739 rc = VERR_INVALID_HANDLE;
740 }
741 else
742 {
743 rc = pThread->MsgAlloc (pHandle, u32MsgId, cbMsg, pfnNewMessage);
744
745 hgcmObjDereference (pThread);
746 }
747
748 LogFlow(("MAIN::hgcmMsgAlloc: handle %d, rc = %Vrc\n", *pHandle, rc));
749
750 return rc;
751}
752
753static int hgcmMsgPostInternal (HGCMMSGHANDLE hMsg, PHGCMMSGCALLBACK pfnCallback, bool fWait)
754{
755 LogFlow(("MAIN::hgcmMsgPostInternal: hMsg = %d, pfnCallback = %p, fWait = %d\n", hMsg, pfnCallback, fWait));
756
757 int rc = VINF_SUCCESS;
758
759 HGCMMsgCore *pMsg = (HGCMMsgCore *)hgcmObjReference (hMsg);
760
761 if (!pMsg)
762 {
763 rc = VERR_INVALID_HANDLE;
764 }
765 else
766 {
767 rc = pMsg->Thread()->MsgPost (pMsg, pfnCallback, fWait);
768
769 hgcmObjDereference (pMsg);
770 }
771
772 LogFlow(("MAIN::hgcmMsgPostInternal: hMsg %d, rc = %Vrc\n", hMsg, rc));
773
774 return rc;
775}
776
777
778/* Post message to worker thread with a flag indication if this is a Send or Post.
779 *
780 * @thread any
781 */
782
783int hgcmMsgPost (HGCMMSGHANDLE hMsg, PHGCMMSGCALLBACK pfnCallback)
784{
785 return hgcmMsgPostInternal (hMsg, pfnCallback, false);
786}
787
788/* Send message to worker thread. Sending thread will block until message is processed.
789 *
790 * @thread any
791 */
792
793int hgcmMsgSend (HGCMMSGHANDLE hMsg)
794{
795 return hgcmMsgPostInternal (hMsg, NULL, true);
796}
797
798
799int hgcmMsgGet (HGCMTHREADHANDLE hThread, HGCMMsgCore **ppMsg)
800{
801 LogFlow(("MAIN::hgcmMsgGet: hThread = %d, ppMsg = %p\n", hThread, ppMsg));
802
803 if (!hThread || !ppMsg)
804 {
805 return VERR_INVALID_PARAMETER;
806 }
807
808 int rc = VINF_SUCCESS;
809
810 HGCMThread *pThread = (HGCMThread *)hgcmObjReference (hThread);
811
812 if (!pThread)
813 {
814 rc = VERR_INVALID_HANDLE;
815 }
816 else
817 {
818 rc = pThread->MsgGet (ppMsg);
819
820 hgcmObjDereference (pThread);
821 }
822
823 LogFlow(("MAIN::hgcmMsgGet: *ppMsg = %p, rc = %Vrc\n", *ppMsg, rc));
824
825 return rc;
826}
827
828
829void hgcmMsgComplete (HGCMMsgCore *pMsg, int32_t u32Result)
830{
831 LogFlow(("MAIN::hgcmMsgComplete: pMsg = %p\n", pMsg));
832
833 if (!pMsg)
834 {
835 return;
836 }
837
838 pMsg->Thread()->MsgComplete (pMsg, u32Result);
839
840 LogFlow(("MAIN::hgcmMsgComplete: pMsg = %p, rc = void\n", pMsg));
841
842 return;
843}
844
845
846int hgcmThreadInit (void)
847{
848 int rc = VINF_SUCCESS;
849
850 LogFlow(("MAIN::hgcmThreadInit\n"));
851
852 /** @todo error processing. */
853
854 rc = hgcmObjInit ();
855
856 LogFlow(("MAIN::hgcmThreadInit: rc = %Vrc\n", rc));
857
858 return rc;
859}
860
861void hgcmThreadUninit (void)
862{
863 hgcmObjUninit ();
864}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette