VirtualBox

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

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

Darwin port (hope I didn't mess up anything, am very Zzzzz...)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
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#include <sys/select.h>
33
34/**
35 * Globals
36 */
37
38
39#ifdef VBOX_WITH_XPCOM
40
41/** global flag indicating that the event queue thread should terminate */
42bool volatile g_fTerminateXPCOMQueueThread = false;
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#ifdef __DARWIN__
67 /** @todo figure out how this works here! */
68 RTThreadSleep(100);
69 int n = 1;
70#else
71 fd_set fdset;
72 FD_ZERO(&fdset);
73 FD_SET(eqFD, &fdset);
74 int n = select(eqFD + 1, &fdset, NULL, NULL, NULL);
75#endif
76
77
78 /* are there any events to process? */
79 if ((n > 0) && !g_fTerminateXPCOMQueueThread)
80 {
81 /*
82 * Post the event and wait for it to be processed. If we don't wait,
83 * we'll flood the queue on SMP systems and when the main thread is busy.
84 * In the event of a push error, we'll yield the timeslice and retry.
85 */
86 SDL_Event event = {0};
87 event.type = SDL_USEREVENT;
88 event.user.type = SDL_USER_EVENT_XPCOM_EVENTQUEUE;
89 rc = SDL_PushEvent(&event);
90 if (!rc)
91 {
92 RTSemEventWait(g_EventSemXPCOMQueueThread, 100);
93 cErrors = 0;
94 }
95 else
96 {
97 cErrors++;
98 if (!RTThreadYield())
99 RTThreadSleep(2);
100 if (cErrors >= 10)
101 RTSemEventWait(g_EventSemXPCOMQueueThread, RT_MIN(cErrors - 8, 50));
102 }
103 }
104 } while (!g_fTerminateXPCOMQueueThread);
105 return VINF_SUCCESS;
106}
107
108/**
109 * Creates the XPCOM event thread
110 *
111 * @returns VBOX status code
112 * @param eqFD XPCOM event queue file descriptor
113 */
114int startXPCOMEventQueueThread(int eqFD)
115{
116 int rc = RTSemEventCreate(&g_EventSemXPCOMQueueThread);
117 if (VBOX_SUCCESS(rc))
118 {
119 RTTHREAD Thread;
120 rc = RTThreadCreate(&Thread, xpcomEventThread, (void *)eqFD, 0, RTTHREADTYPE_MSG_PUMP, 0, "XPCOMEvent");
121 }
122 AssertRC(rc);
123 return rc;
124}
125
126/**
127 * Signal to the XPCOM even queue thread that it should select for more events.
128 */
129void signalXPCOMEventQueueThread(void)
130{
131 int rc = RTSemEventSignal(g_EventSemXPCOMQueueThread);
132 AssertRC(rc);
133}
134
135/**
136 * Indicates to the XPCOM thread that it should terminate now.
137 */
138void terminateXPCOMQueueThread(void)
139{
140 g_fTerminateXPCOMQueueThread = true;
141 if (g_EventSemXPCOMQueueThread)
142 {
143 RTSemEventSignal(g_EventSemXPCOMQueueThread);
144 RTThreadYield();
145 }
146}
147
148
149
150#endif /* VBOX_WITH_XPCOM */
151
Note: See TracBrowser for help on using the repository browser.

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