1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: VBoxSDL (simple frontend based on SDL):
|
---|
4 | * Miscellaneous helpers
|
---|
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 LOG_GROUP_GUI
|
---|
24 | #include <VBox/err.h>
|
---|
25 | #include <VBox/log.h>
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/thread.h>
|
---|
28 | #include <iprt/semaphore.h>
|
---|
29 | #include "VBoxSDL.h"
|
---|
30 | #include "Helper.h"
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Globals
|
---|
34 | */
|
---|
35 |
|
---|
36 |
|
---|
37 | #ifdef __LINUX__
|
---|
38 |
|
---|
39 | /** global flag indicating that the event queue thread should terminate */
|
---|
40 | bool volatile g_fTerminateXPCOMQueueThread = false;
|
---|
41 |
|
---|
42 | /** Semaphore the XPCOM event thread will sleep on while it waits for the main thread to process pending requests. */
|
---|
43 | RTSEMEVENT g_EventSemXPCOMQueueThread = NULL;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Thread method to wait for XPCOM events and notify the SDL thread.
|
---|
47 | *
|
---|
48 | * @returns Error code
|
---|
49 | * @param thread Thread ID
|
---|
50 | * @param pvUser User specific parameter, the file descriptor
|
---|
51 | * of the event queue socket
|
---|
52 | */
|
---|
53 | DECLCALLBACK(int) xpcomEventThread(RTTHREAD thread, void *pvUser)
|
---|
54 | {
|
---|
55 | int eqFD = (intptr_t)pvUser;
|
---|
56 | unsigned cErrors = 0;
|
---|
57 | int rc;
|
---|
58 |
|
---|
59 | /* Wait with the processing till the main thread needs it. */
|
---|
60 | RTSemEventWait(g_EventSemXPCOMQueueThread, 2500);
|
---|
61 |
|
---|
62 | do
|
---|
63 | {
|
---|
64 | fd_set fdset;
|
---|
65 | FD_ZERO(&fdset);
|
---|
66 | FD_SET(eqFD, &fdset);
|
---|
67 | int n = select(eqFD + 1, &fdset, NULL, NULL, NULL);
|
---|
68 |
|
---|
69 | /* are there any events to process? */
|
---|
70 | if ((n > 0) && !g_fTerminateXPCOMQueueThread)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Post the event and wait for it to be processed. If we don't wait,
|
---|
74 | * we'll flood the queue on SMP systems and when the main thread is busy.
|
---|
75 | * In the event of a push error, we'll yield the timeslice and retry.
|
---|
76 | */
|
---|
77 | SDL_Event event = {0};
|
---|
78 | event.type = SDL_USEREVENT;
|
---|
79 | event.user.type = SDL_USER_EVENT_XPCOM_EVENTQUEUE;
|
---|
80 | rc = SDL_PushEvent(&event);
|
---|
81 | if (!rc)
|
---|
82 | {
|
---|
83 | RTSemEventWait(g_EventSemXPCOMQueueThread, 100);
|
---|
84 | cErrors = 0;
|
---|
85 | }
|
---|
86 | else
|
---|
87 | {
|
---|
88 | cErrors++;
|
---|
89 | if (!RTThreadYield())
|
---|
90 | RTThreadSleep(2);
|
---|
91 | if (cErrors >= 10)
|
---|
92 | RTSemEventWait(g_EventSemXPCOMQueueThread, RT_MIN(cErrors - 8, 50));
|
---|
93 | }
|
---|
94 | }
|
---|
95 | } while (!g_fTerminateXPCOMQueueThread);
|
---|
96 | return VINF_SUCCESS;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Creates the XPCOM event thread
|
---|
101 | *
|
---|
102 | * @returns VBOX status code
|
---|
103 | * @param eqFD XPCOM event queue file descriptor
|
---|
104 | */
|
---|
105 | int startXPCOMEventQueueThread(int eqFD)
|
---|
106 | {
|
---|
107 | int rc = RTSemEventCreate(&g_EventSemXPCOMQueueThread);
|
---|
108 | if (VBOX_SUCCESS(rc))
|
---|
109 | {
|
---|
110 | RTTHREAD Thread;
|
---|
111 | rc = RTThreadCreate(&Thread, xpcomEventThread, (void *)eqFD, 0, RTTHREADTYPE_MSG_PUMP, 0, "XPCOMEvent");
|
---|
112 | }
|
---|
113 | AssertRC(rc);
|
---|
114 | return rc;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Signal to the XPCOM even queue thread that it should select for more events.
|
---|
119 | */
|
---|
120 | void signalXPCOMEventQueueThread(void)
|
---|
121 | {
|
---|
122 | int rc = RTSemEventSignal(g_EventSemXPCOMQueueThread);
|
---|
123 | AssertRC(rc);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Indicates to the XPCOM thread that it should terminate now.
|
---|
128 | */
|
---|
129 | void terminateXPCOMQueueThread(void)
|
---|
130 | {
|
---|
131 | g_fTerminateXPCOMQueueThread = true;
|
---|
132 | if (g_EventSemXPCOMQueueThread)
|
---|
133 | {
|
---|
134 | RTSemEventSignal(g_EventSemXPCOMQueueThread);
|
---|
135 | RTThreadYield();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|
141 | #endif /* __LINUX__ */
|
---|
142 |
|
---|