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