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