VirtualBox

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

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

not needed after all.

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