VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxSDL/Helper.cpp@ 44528

Last change on this file since 44528 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1/** @file
2 *
3 * VBox frontends: VBoxSDL (simple frontend based on SDL):
4 * Miscellaneous helpers
5 */
6
7/*
8 * Copyright (C) 2006-2011 Oracle Corporation
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 (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#define LOG_GROUP LOG_GROUP_GUI
20#include <VBox/err.h>
21#include <VBox/log.h>
22#include <iprt/asm.h>
23#include <iprt/assert.h>
24#include <iprt/thread.h>
25#include <iprt/semaphore.h>
26#include "VBoxSDL.h"
27#include "Helper.h"
28
29
30/**
31 * Globals
32 */
33
34
35#ifdef USE_XPCOM_QUEUE_THREAD
36
37/** global flag indicating that the event queue thread should terminate */
38static bool volatile g_fTerminateXPCOMQueueThread = false;
39
40/** How many XPCOM user events are on air. Only allow one pending event to
41 * prevent an overflow of the SDL event queue. */
42static volatile int32_t g_s32XPCOMEventsPending;
43
44/** Semaphore the XPCOM event thread will sleep on while it waits for the main thread to process pending requests. */
45RTSEMEVENT g_EventSemXPCOMQueueThread = NULL;
46
47/**
48 * Thread method to wait for XPCOM events and notify the SDL thread.
49 *
50 * @returns Error code
51 * @param thread Thread ID
52 * @param pvUser User specific parameter, the file descriptor
53 * of the event queue socket
54 */
55DECLCALLBACK(int) xpcomEventThread(RTTHREAD thread, void *pvUser)
56{
57 int eqFD = (intptr_t)pvUser;
58 unsigned cErrors = 0;
59 int rc;
60
61 /* Wait with the processing till the main thread needs it. */
62 RTSemEventWait(g_EventSemXPCOMQueueThread, 2500);
63
64 do
65 {
66 fd_set fdset;
67 FD_ZERO(&fdset);
68 FD_SET(eqFD, &fdset);
69 int n = select(eqFD + 1, &fdset, NULL, NULL, NULL);
70
71 /* are there any events to process? */
72 if ((n > 0) && !g_fTerminateXPCOMQueueThread)
73 {
74 /*
75 * Wait until all XPCOM events are processed. 1s just for sanity.
76 */
77 int iWait = 1000;
78 /*
79 * Don't post an event if there is a pending XPCOM event to prevent an
80 * overflow of the SDL event queue.
81 */
82 if (g_s32XPCOMEventsPending < 1)
83 {
84 /*
85 * Post the event and wait for it to be processed. If we don't wait,
86 * we'll flood the queue on SMP systems and when the main thread is busy.
87 * In the event of a push error, we'll yield the timeslice and retry.
88 */
89 SDL_Event event = {0};
90 event.type = SDL_USEREVENT;
91 event.user.type = SDL_USER_EVENT_XPCOM_EVENTQUEUE;
92 rc = SDL_PushEvent(&event);
93 if (!rc)
94 {
95 /* success */
96 ASMAtomicIncS32(&g_s32XPCOMEventsPending);
97 cErrors = 0;
98 }
99 else
100 {
101 /* failure */
102 cErrors++;
103 if (!RTThreadYield())
104 RTThreadSleep(2);
105 iWait = (cErrors >= 10) ? RT_MIN(cErrors - 8, 50) : 0;
106 }
107 }
108 else
109 Log2(("not enqueueing SDL XPCOM event (%d)\n", g_s32XPCOMEventsPending));
110
111 if (iWait)
112 RTSemEventWait(g_EventSemXPCOMQueueThread, iWait);
113 }
114 } while (!g_fTerminateXPCOMQueueThread);
115 return VINF_SUCCESS;
116}
117
118/**
119 * Creates the XPCOM event thread
120 *
121 * @returns VBOX status code
122 * @param eqFD XPCOM event queue file descriptor
123 */
124int startXPCOMEventQueueThread(int eqFD)
125{
126 int rc = RTSemEventCreate(&g_EventSemXPCOMQueueThread);
127 if (RT_SUCCESS(rc))
128 {
129 RTTHREAD Thread;
130 rc = RTThreadCreate(&Thread, xpcomEventThread, (void *)(intptr_t)eqFD,
131 0, RTTHREADTYPE_MSG_PUMP, 0, "XPCOMEvent");
132 }
133 AssertRC(rc);
134 return rc;
135}
136
137/**
138 * Notify the XPCOM thread that we consumed an XPCOM event.
139 */
140void consumedXPCOMUserEvent(void)
141{
142 ASMAtomicDecS32(&g_s32XPCOMEventsPending);
143}
144
145/**
146 * Signal to the XPCOM even queue thread that it should select for more events.
147 */
148void signalXPCOMEventQueueThread(void)
149{
150 int rc = RTSemEventSignal(g_EventSemXPCOMQueueThread);
151 AssertRC(rc);
152}
153
154/**
155 * Indicates to the XPCOM thread that it should terminate now.
156 */
157void terminateXPCOMQueueThread(void)
158{
159 g_fTerminateXPCOMQueueThread = true;
160 if (g_EventSemXPCOMQueueThread)
161 {
162 RTSemEventSignal(g_EventSemXPCOMQueueThread);
163 RTThreadYield();
164 }
165}
166
167#endif /* USE_XPCOM_QUEUE_THREAD */
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