[PATCH 5/5] input: use a thread for the generation of input events
Adam Jackson
ajax at redhat.com
Thu Dec 16 12:31:40 PST 2010
From: Tiago Vignatti <tiago.vignatti at nokia.com>
If configured, and enabled at runtime, use a separate thread for
handling input devices. Use pipes to communicate plug events from main
thread to input thread, and to communicate event queue updates from
input thread to main thread.
For the xfree86 DDX, change the SIGIO handler to simply raise enough of
a dispatch exception to make the main loop interrupt request processing
and return to input processing. This has no effect when the input
thread is active but is a minor optimization when it's not.
v2:
- Fix memory leak in InputThreadUnregisterDev
- Fix build with --disable-input-thread
- Merge SIGIO strength reduction
Co-authored-by: Fernando Carrijo <fcarrijo at freedesktop.org>
Signed-off-by: Tiago Vignatti <tiago.vignatti at nokia.com>
Signed-off-by: Adam Jackson <ajax at redhat.com>
---
configure.ac | 30 +++
dix/main.c | 3 +
hw/xfree86/common/xf86Events.c | 35 ++--
hw/xfree86/common/xf86Init.c | 6 +-
include/Makefile.am | 1 +
include/dix-config.h.in | 3 +
include/inputthread.h | 36 ++++
include/opaque.h | 2 +
mi/mieq.c | 76 ++++----
os/Makefile.am | 1 +
os/connection.c | 4 +
os/inputthread.c | 404 ++++++++++++++++++++++++++++++++++++++++
12 files changed, 540 insertions(+), 61 deletions(-)
create mode 100644 include/inputthread.h
create mode 100644 os/inputthread.c
diff --git a/configure.ac b/configure.ac
index a80a13f..b516456 100644
--- a/configure.ac
+++ b/configure.ac
@@ -819,6 +819,36 @@ REQUIRED_LIBS="$REQUIRED_LIBS $LIBPIXMAN $LIBXFONT xau"
REQUIRED_MODULES="[fixesproto >= 4.1] [damageproto >= 1.1] [xcmiscproto >= 1.2.0] [xtrans >= 1.2.2] [bigreqsproto >= 1.1.0] $SDK_REQUIRED_MODULES"
+# input thread setup
+case $host_os in
+linux*)
+ THREAD_DEFAULT=yes ;;
+*)
+ THREAD_DEFAULT=no ;;
+esac
+AC_ARG_ENABLE(input-thread, AS_HELP_STRING([--enable-input-thread],
+ [Enable input threads]),
+ [INPUTTHREAD=$enableval], [INPUTTHREAD=$THREAD_DEFAULT])
+if test "x$INPUTTHREAD" = "xyes" ; then
+ case $host_os in
+ linux*|openbsd*|gnu*|k*bsd*-gnu)
+ THREAD_LIB=-lpthread ;;
+ netbsd*)
+ THREAD_CFLAGS="-D_POSIX_THREAD_SAFE_FUNCTIONS"
+ THREAD_LIB="-lpthread" ;;
+ freebsd*)
+ THREAD_CFLAGS="-D_THREAD_SAFE"
+ THREAD_LIB="-pthread" ;;
+ dragonfly*)
+ THREAD_LIB="-pthread" ;;
+ solaris*)
+ THREAD_CFLAGS="-D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS" ;;
+ esac
+ SYS_LIBS="$SYS_LIBS $THREAD_LIB"
+ CFLAGS="$CFLAGS $THREAD_CFLAGS"
+ AC_DEFINE(USE_INPUT_THREAD, 1, [Use pthreads for input processing])
+fi
+
if test "x$CONFIG_UDEV" = xyes &&
{ test "x$CONFIG_DBUS_API" = xyes || test "x$CONFIG_HAL" = xyes; }; then
AC_MSG_ERROR([Hotplugging through both libudev and dbus/hal not allowed])
diff --git a/dix/main.c b/dix/main.c
index 692bec1..8056892 100644
--- a/dix/main.c
+++ b/dix/main.c
@@ -99,6 +99,7 @@ Equipment Corporation.
#include "opaque.h"
#include "servermd.h"
#include "hotplug.h"
+#include "inputthread.h"
#include "site.h"
#include "dixfont.h"
#include "extnsionst.h"
@@ -284,6 +285,8 @@ int main(int argc, char *argv[], char *envp[])
NotifyParentProcess();
+ InputThreadInit();
+
Dispatch();
UndisplayDevices();
diff --git a/hw/xfree86/common/xf86Events.c b/hw/xfree86/common/xf86Events.c
index 6561292..670addb 100644
--- a/hw/xfree86/common/xf86Events.c
+++ b/hw/xfree86/common/xf86Events.c
@@ -71,6 +71,7 @@
#include <X11/extensions/XI.h>
#include <X11/extensions/XIproto.h>
#include "inputstr.h"
+#include "inputthread.h"
#include "xf86Xinput.h"
#include "mi.h"
@@ -285,42 +286,36 @@ xf86Wakeup(pointer blockData, int err, pointer pReadmask)
if (xf86VTSwitchPending()) xf86VTSwitch();
}
-
/*
- * xf86SigioReadInput --
- * signal handler for the SIGIO signal.
+ * The host OS may or may not have working SIGIO handlers. If they don't,
+ * then the time_to_yield callback never gets called, and input will simply
+ * run out of the main loop whenever it gets around to it.
*/
+
static void
-xf86SigioReadInput(int fd, void *closure)
+time_to_yield(int fd, void *closure)
{
- int errno_save = errno;
- InputInfoPtr pInfo = closure;
-
- pInfo->read_input(pInfo);
-
- errno = errno_save;
+ isItTimeToYield = 1;
}
-/*
- * xf86AddEnabledDevice --
- *
- */
void
xf86AddEnabledDevice(InputInfoPtr pInfo)
{
- if (!xf86InstallSIGIOHandler (pInfo->fd, xf86SigioReadInput, pInfo)) {
+ if (!xf86silkenMouseDisableFlag) {
+ InputThreadRegisterDev(pInfo->fd, (void*) pInfo->read_input, pInfo);
+ } else {
+ xf86InstallSIGIOHandler(pInfo->fd, time_to_yield, NULL);
AddEnabledDevice(pInfo->fd);
}
}
-/*
- * xf86RemoveEnabledDevice --
- *
- */
void
xf86RemoveEnabledDevice(InputInfoPtr pInfo)
{
- if (!xf86RemoveSIGIOHandler (pInfo->fd)) {
+ if (!xf86silkenMouseDisableFlag) {
+ InputThreadUnregisterDev(pInfo->fd);
+ } else {
+ xf86RemoveSIGIOHandler(pInfo->fd);
RemoveEnabledDevice(pInfo->fd);
}
}
diff --git a/hw/xfree86/common/xf86Init.c b/hw/xfree86/common/xf86Init.c
index c1596d6..6033053 100644
--- a/hw/xfree86/common/xf86Init.c
+++ b/hw/xfree86/common/xf86Init.c
@@ -86,7 +86,7 @@
#include "dpmsproc.h"
#endif
#include <hotplug.h>
-
+#include "inputthread.h"
#ifdef XF86PM
void (*xf86OSPMClose)(void) = NULL;
@@ -817,6 +817,8 @@ InitInput(int argc, char **argv)
InputInfoPtr* pDev;
DeviceIntPtr dev;
+ InputThreadPreInit();
+
xf86Info.vtRequestsPending = FALSE;
mieqInit();
@@ -842,6 +844,8 @@ void
CloseInput (void)
{
config_fini();
+
+ InputThreadFini();
}
/*
diff --git a/include/Makefile.am b/include/Makefile.am
index 966d215..936aba7 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -66,5 +66,6 @@ AM_CFLAGS = $(DIX_CFLAGS)
EXTRA_DIST = \
dix-config-apple-verbatim.h \
eventconvert.h eventstr.h inpututils.h \
+ inputthread.h \
protocol-versions.h \
xsha1.h
diff --git a/include/dix-config.h.in b/include/dix-config.h.in
index 6a33264..5332688 100644
--- a/include/dix-config.h.in
+++ b/include/dix-config.h.in
@@ -300,6 +300,9 @@
/* Support UNIX socket connections */
#undef UNIXCONN
+/* Use pthreads for input processing */
+#undef USE_INPUT_THREAD
+
/* Define to use byteswap macros from <sys/endian.h> */
#undef USE_SYS_ENDIAN_H
diff --git a/include/inputthread.h b/include/inputthread.h
new file mode 100644
index 0000000..f15fae2
--- /dev/null
+++ b/include/inputthread.h
@@ -0,0 +1,36 @@
+/* inputthread.h -- Threaded generation of input events.
+ *
+ * Copyright © 2007-2008 Tiago Vignatti <vignatti at freedesktop org>
+ * Copyright © 2010 Nokia
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Fernando Carrijo <fcarrijo at freedesktop org>
+ * Tiago Vignatti <vignatti at freedesktop org>
+ */
+
+extern void InputThreadPreInit(void);
+extern void InputThreadInit(void);
+extern void InputThreadFini(void);
+
+extern int InputThreadRegisterDev(int fd,
+ void (*readInputProc) (void *),
+ void *readInputArgs);
+
+extern void InputThreadUnregisterDev(int fd);
diff --git a/include/opaque.h b/include/opaque.h
index 5c70717..9a21839 100644
--- a/include/opaque.h
+++ b/include/opaque.h
@@ -39,6 +39,8 @@ extern _X_EXPORT int MaxClients;
extern _X_EXPORT volatile char isItTimeToYield;
extern _X_EXPORT volatile char dispatchException;
+extern int MaxInputDevices;
+
/* bit values for dispatchException */
#define DE_RESET 1
#define DE_TERMINATE 2
diff --git a/mi/mieq.c b/mi/mieq.c
index 01da52a..4fc3b67 100644
--- a/mi/mieq.c
+++ b/mi/mieq.c
@@ -39,6 +39,7 @@ in this Software without prior written authorization from The Open Group.
# include <X11/X.h>
# include <X11/Xmd.h>
# include <X11/Xproto.h>
+# include <pthread.h>
# include "misc.h"
# include "windowstr.h"
# include "pixmapstr.h"
@@ -79,10 +80,26 @@ typedef struct _EventQueue {
static EventQueueRec miEventQueue;
-#ifdef XQUARTZ
-#include <pthread.h>
+#ifdef USE_INPUT_THREAD
+
static pthread_mutex_t miEventQueueMutex = PTHREAD_MUTEX_INITIALIZER;
+static inline void queue_mutex_lock(void) {
+ pthread_mutex_lock(&miEventQueueMutex);
+}
+
+static inline void queue_mutex_unlock(void) {
+ pthread_mutex_unlock(&miEventQueueMutex);
+}
+
+#else
+
+#define queue_mutex_lock() do {} while (0)
+#define queue_mutex_unlock() do {} while (0)
+
+#endif
+
+#if defined XQUARTZ
extern BOOL serverInitComplete;
extern pthread_mutex_t serverInitCompleteMutex;
extern pthread_cond_t serverInitCompleteCond;
@@ -96,6 +113,9 @@ static inline void wait_for_server_init(void) {
pthread_mutex_unlock(&serverInitCompleteMutex);
}
}
+
+#else
+static inline void wait_for_server_init(void) {}
#endif
Bool
@@ -151,10 +171,8 @@ mieqEnqueue(DeviceIntPtr pDev, InternalEvent *e)
int evlen;
Time time;
-#ifdef XQUARTZ
wait_for_server_init();
- pthread_mutex_lock(&miEventQueueMutex);
-#endif
+ queue_mutex_lock();
CHECKEVENT(e);
@@ -178,9 +196,7 @@ mieqEnqueue(DeviceIntPtr pDev, InternalEvent *e)
xorg_backtrace();
stuck = 1;
}
-#ifdef XQUARTZ
- pthread_mutex_unlock(&miEventQueueMutex);
-#endif
+ queue_mutex_unlock();
return;
}
stuck = 0;
@@ -195,9 +211,7 @@ mieqEnqueue(DeviceIntPtr pDev, InternalEvent *e)
if (!evt->event)
{
ErrorF("[mi] Running out of memory. Tossing event.\n");
-#ifdef XQUARTZ
- pthread_mutex_unlock(&miEventQueueMutex);
-#endif
+ queue_mutex_unlock();
return;
}
}
@@ -217,39 +231,30 @@ mieqEnqueue(DeviceIntPtr pDev, InternalEvent *e)
miEventQueue.lastMotion = isMotion;
miEventQueue.tail = (oldtail + 1) % QUEUE_SIZE;
-#ifdef XQUARTZ
- pthread_mutex_unlock(&miEventQueueMutex);
-#endif
+
+ queue_mutex_unlock();
}
void
mieqSwitchScreen(DeviceIntPtr pDev, ScreenPtr pScreen, Bool fromDIX)
{
-#ifdef XQUARTZ
- pthread_mutex_lock(&miEventQueueMutex);
-#endif
+ queue_mutex_lock();
EnqueueScreen(pDev) = pScreen;
if (fromDIX)
DequeueScreen(pDev) = pScreen;
-#ifdef XQUARTZ
- pthread_mutex_unlock(&miEventQueueMutex);
-#endif
+ queue_mutex_unlock();
}
void
mieqSetHandler(int event, mieqHandler handler)
{
-#ifdef XQUARTZ
- pthread_mutex_lock(&miEventQueueMutex);
-#endif
+ queue_mutex_lock();
if (handler && miEventQueue.handlers[event])
ErrorF("[mi] mieq: warning: overriding existing handler %p with %p for "
"event %d\n", miEventQueue.handlers[event], handler, event);
miEventQueue.handlers[event] = handler;
-#ifdef XQUARTZ
- pthread_mutex_unlock(&miEventQueueMutex);
-#endif
+ queue_mutex_unlock();
}
/**
@@ -436,10 +441,8 @@ mieqProcessInputEvents(void)
DeviceIntPtr dev = NULL,
master = NULL;
-#ifdef XQUARTZ
- pthread_mutex_lock(&miEventQueueMutex);
-#endif
-
+ queue_mutex_lock();
+
while (miEventQueue.head != miEventQueue.tail) {
e = &miEventQueue.events[miEventQueue.head];
@@ -462,9 +465,7 @@ mieqProcessInputEvents(void)
miEventQueue.head = (miEventQueue.head + 1) % QUEUE_SIZE;
-#ifdef XQUARTZ
- pthread_mutex_unlock(&miEventQueueMutex);
-#endif
+ queue_mutex_unlock();
master = (dev && !IsMaster(dev) && dev->u.master) ? dev->u.master : NULL;
@@ -484,12 +485,7 @@ mieqProcessInputEvents(void)
if (event->any.type == ET_Motion && master)
miPointerUpdateSprite(dev);
-#ifdef XQUARTZ
- pthread_mutex_lock(&miEventQueueMutex);
-#endif
+ queue_mutex_lock();
}
-#ifdef XQUARTZ
- pthread_mutex_unlock(&miEventQueueMutex);
-#endif
+ queue_mutex_unlock();
}
-
diff --git a/os/Makefile.am b/os/Makefile.am
index 3e4f2c5..cbbae31 100644
--- a/os/Makefile.am
+++ b/os/Makefile.am
@@ -21,6 +21,7 @@ libos_la_SOURCES = \
auth.c \
backtrace.c \
connection.c \
+ inputthread.c \
io.c \
mitauth.c \
oscolor.c \
diff --git a/os/connection.c b/os/connection.c
index 5452ae1..6c2b5d3 100644
--- a/os/connection.c
+++ b/os/connection.c
@@ -142,6 +142,8 @@ int MaxClients = 0;
Bool NewOutputPending; /* not yet attempted to write some new output */
Bool AnyClientsWriteBlocked; /* true if some client blocked on write */
+int MaxInputDevices = 0;
+
static Bool RunFromSmartParent; /* send SIGUSR1 to parent process */
Bool RunFromSigStopParent; /* send SIGSTOP to our own process; Upstart (or
equivalent) will send SIGCONT back. */
@@ -301,6 +303,8 @@ InitConnectionLimits(void)
if (lastfdesc > MAXSELECT)
lastfdesc = MAXSELECT;
+ MaxInputDevices = lastfdesc;
+
if (lastfdesc > MAXCLIENTS)
{
lastfdesc = MAXCLIENTS;
diff --git a/os/inputthread.c b/os/inputthread.c
new file mode 100644
index 0000000..0bc32a7
--- /dev/null
+++ b/os/inputthread.c
@@ -0,0 +1,404 @@
+/* inputthread.c -- Threaded generation of input events.
+ *
+ * Copyright © 2007-2008 Tiago Vignatti <vignatti at freedesktop org>
+ * Copyright © 2010 Nokia
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Fernando Carrijo <fcarrijo at freedesktop org>
+ * Tiago Vignatti <vignatti at freedesktop org>
+ */
+
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <pthread.h>
+
+#include <X11/Xpoll.h>
+#include "inputthread.h"
+#include "inputstr.h"
+#include "opaque.h"
+
+#ifdef USE_INPUT_THREAD
+
+/**
+ * An input device as seen by the threaded input facility
+ */
+typedef struct _InputThreadDevice {
+ struct _InputThreadDevice *next;
+ void (*readInputProc)(void*);
+ void *readInputArgs;
+ int fd;
+} InputThreadDevice;
+
+/**
+ * The threaded input facility.
+ *
+ * For now, we have one instance for all input devices.
+ */
+typedef struct {
+ pthread_t thread;
+ InputThreadDevice *devs;
+ fd_set fds;
+ int readPipe;
+ int writePipe;
+} InputThreadInfo;
+
+static InputThreadInfo *inputThreadInfo;
+
+static int hotplugPipeRead = -1;
+static int hotplugPipeWrite = -1;
+
+/**
+ * Notify a thread about the availability of new asynchronously enqueued input
+ * events.
+ *
+ * @see WaitForSomething()
+ */
+static void
+InputThreadFillPipe(int writeHead)
+{
+ int ret;
+ char byte = 0;
+ fd_set writePipe;
+
+ FD_ZERO(&writePipe);
+
+ while (1) {
+ ret = write(writeHead, &byte, 1);
+ if (!ret)
+ FatalError("input-thread: write() returned 0");
+ if (ret > 0) {
+ break;
+ }
+ if (errno != EAGAIN)
+ FatalError("input-thread: filling pipe");
+
+ DebugF("input-thread: pipe full\n");
+ FD_SET(writeHead, &writePipe);
+ Select(writeHead + 1, NULL, &writePipe, NULL, NULL);
+ }
+}
+
+/**
+ * Consume eventual notifications left by a thread.
+ *
+ * @see WaitForSomething()
+ * @see InputThreadFillPipe()
+ */
+static int
+InputThreadReadPipe(int readHead)
+{
+ int ret, array[10];
+
+ ret = read(readHead, &array, sizeof(array));
+ if (ret >= 0)
+ return ret;
+
+ if (errno != EAGAIN)
+ FatalError("input-thread: draining pipe (%d)", errno);
+
+ return 1;
+}
+
+/**
+ * Register an input device in the threaded input facility
+ *
+ * @param fd File descriptor which identifies the input device
+ * @param readInputProc Procedure used to read input from the device
+ * @param readInputArgs Arguments to be consumed by the above procedure
+ *
+ * return 1 if success; 0 otherwise.
+ */
+int
+InputThreadRegisterDev(int fd,
+ void (*readInputProc) (void*),
+ void *readInputArgs)
+{
+ InputThreadDevice *new;
+
+ new = malloc(sizeof(InputThreadDevice));
+ if (new == NULL) {
+ DebugF("input-thread: could not register device\n");
+ return 0;
+ }
+
+ new->fd = fd;
+ new->readInputProc = readInputProc;
+ new->readInputArgs = readInputArgs;
+ new->next = inputThreadInfo->devs;
+
+ inputThreadInfo->devs = new;
+ FD_SET(fd, &inputThreadInfo->fds);
+
+ InputThreadFillPipe(hotplugPipeWrite);
+ DebugF("input-thread: registered device %d\n", fd);
+
+ return 1;
+}
+
+/**
+ * Unregister a device in the threaded input facility
+ *
+ * @param fd File descriptor which identifies the input device
+ *
+ * @return 1 if success; 0 otherwise.
+ */
+void
+InputThreadUnregisterDev(int fd)
+{
+ InputThreadDevice *prev, *dev;
+
+ /* return silently if input thread is already finished (e.g., at
+ * DisableDevice time, evdev tries to call this function again through
+ * xf86RemoveEnabledDevice */
+ if (!inputThreadInfo)
+ return;
+
+ prev = NULL;
+ dev = inputThreadInfo->devs;
+ while (dev != NULL) {
+ if (dev->fd == fd)
+ break;
+ prev = dev;
+ dev = dev->next;
+ }
+
+ /* fd didn't match any registered device. */
+ if (dev == NULL)
+ return;
+
+ if (prev == NULL)
+ inputThreadInfo->devs = dev->next;
+ else
+ prev->next = dev->next;
+
+ FD_CLR(fd, &inputThreadInfo->fds);
+ dev->readInputProc = NULL;
+ dev->readInputArgs = NULL;
+ dev->fd = 0;
+ free(dev);
+
+ InputThreadFillPipe(hotplugPipeWrite);
+ DebugF("input-thread: unregistered device: %d\n", fd);
+}
+
+/**
+ * The workhorse of threaded input event generation.
+ *
+ * Or if you prefer: The WaitForSomething for input devices. :)
+ *
+ * Runs in parallel with the server main thread, listening to input devices in
+ * an endless loop. Whenever new input data is made available, calls the
+ * proper device driver's routines which are ultimately responsible for the
+ * generation of input events.
+ *
+ * @see InputThreadPreInit()
+ * @see InputThreadInit()
+ */
+
+static void*
+InputThreadDoWork(void *arg)
+{
+ fd_set readyFds;
+ InputThreadDevice *dev;
+
+ FD_ZERO(&readyFds);
+
+ while (1)
+ {
+ XFD_COPYSET(&inputThreadInfo->fds, &readyFds);
+ FD_SET(hotplugPipeRead, &readyFds);
+
+ DebugF("input-thread: InputThreadDoWork waiting for devices\n");
+
+ if (Select(MaxInputDevices, &readyFds, NULL, NULL, NULL) < 0) {
+ if (errno == EINVAL)
+ FatalError("input-thread: InputThreadDoWork (%s)", strerror(errno));
+ else if (errno != EINTR)
+ ErrorF("input-thread: InputThreadDoWork (%s)\n", strerror(errno));
+ }
+
+ DebugF("input-thread: InputThreadDoWork generating events\n");
+ /* Call the device drivers to generate input events for us */
+ for (dev = inputThreadInfo->devs; dev != NULL; dev = dev->next)
+ if (FD_ISSET(dev->fd, &readyFds) && dev->readInputProc)
+ dev->readInputProc(dev->readInputArgs);
+
+ /* Kick main thread to process the generated input events and drain
+ * events from hotplug pipe */
+ InputThreadFillPipe(inputThreadInfo->writePipe);
+ InputThreadReadPipe(hotplugPipeRead);
+ }
+}
+
+static void
+InputThreadWakeup(pointer blockData, int err, pointer pReadmask)
+{
+ InputThreadReadPipe(inputThreadInfo->readPipe);
+}
+
+/**
+ * Pre-initialize the facility used for threaded generation of input events
+ *
+ */
+void
+InputThreadPreInit(void)
+{
+ int fds[2], hotplugPipe[2];
+
+ if (pipe(fds) < 0)
+ FatalError("input-thread: could not create pipe");
+
+ if (pipe(hotplugPipe) < 0)
+ FatalError("input-thread: could not create pipe");
+
+ inputThreadInfo = malloc(sizeof(InputThreadInfo));
+ if (!inputThreadInfo)
+ FatalError("input-thread: could not allocate memory");
+
+ inputThreadInfo->thread = 0;
+ inputThreadInfo->devs = NULL;
+ FD_ZERO(&inputThreadInfo->fds);
+
+ /* By making read head non-blocking, we ensure that while the main thread
+ * is busy servicing client requests, the dedicated input thread can work
+ * in parallel.
+ */
+ inputThreadInfo->readPipe = fds[0];
+ fcntl(inputThreadInfo->readPipe, F_SETFL, O_NONBLOCK | O_CLOEXEC);
+ AddGeneralSocket(inputThreadInfo->readPipe);
+ RegisterBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
+ InputThreadWakeup, NULL);
+
+ inputThreadInfo->writePipe = fds[1];
+
+ hotplugPipeRead = hotplugPipe[0];
+ fcntl(hotplugPipeRead, F_SETFL, O_NONBLOCK | O_CLOEXEC);
+ hotplugPipeWrite = hotplugPipe[1];
+}
+
+/**
+ * Start the threaded generation of input events. This routine complements what
+ * was previously done by InputThreadPreInit(), being only responsible for
+ * creating the dedicated input thread.
+ *
+ */
+void
+InputThreadInit(void)
+{
+ pthread_attr_t attr;
+
+ pthread_attr_init(&attr);
+
+ /* For OSes that differentiate between processes and threads, the following
+ * lines have sense. Linux uses the 1:1 thread model. The scheduler handles
+ * every thread as a normal process. Therefore this probably has no meaning
+ * if we are under Linux.
+ */
+ if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM) != 0)
+ ErrorF("input-thread: error setting thread scope\n");
+
+ if (pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN) != 0)
+ ErrorF("input-thread: error setting thread stack size\n");
+
+ DebugF("input-thread: creating thread\n");
+ pthread_create(&inputThreadInfo->thread, &attr,
+ &InputThreadDoWork, NULL);
+
+ pthread_attr_destroy (&attr);
+}
+
+/**
+ * Stop the threaded generation of input events
+ *
+ * This function is supposed to be called at server shutdown time only.
+ */
+void
+InputThreadFini(void)
+{
+ InputThreadDevice *dev, *tmp;
+
+ pthread_cancel(inputThreadInfo->thread);
+ pthread_join(inputThreadInfo->thread, NULL);
+
+ for (dev = inputThreadInfo->devs; dev != NULL; dev = tmp) {
+ tmp = dev->next;
+ dev->next = NULL;
+ dev->readInputProc = NULL;
+ dev->readInputArgs = NULL;
+ FD_CLR(dev->fd, &inputThreadInfo->fds);
+ dev->fd = -1;
+ free(dev);
+ }
+ inputThreadInfo->devs = NULL;
+ FD_ZERO(&inputThreadInfo->fds);
+
+ RemoveGeneralSocket(inputThreadInfo->readPipe);
+ RemoveBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
+ InputThreadWakeup, NULL);
+ close(inputThreadInfo->readPipe);
+ close(inputThreadInfo->writePipe);
+ inputThreadInfo->readPipe = -1;
+ inputThreadInfo->writePipe = -1;
+
+ close(hotplugPipeRead);
+ close(hotplugPipeWrite);
+ hotplugPipeRead = -1;
+ hotplugPipeWrite = -1;
+
+ free(inputThreadInfo);
+ inputThreadInfo = NULL;
+}
+
+#else /* no input thread */
+
+void
+InputThreadPreInit(void)
+{
+}
+
+void
+InputThreadInit(void)
+{
+}
+
+void
+InputThreadFini(void)
+{
+}
+
+int
+InputThreadRegisterDev(int fd, void (*readInputProc) (void *),
+ void *readInputArgs)
+{
+ return FALSE;
+}
+
+void
+InputThreadUnregisterDev(int fd)
+{
+}
+
+#endif
--
1.7.3.2
More information about the xorg-devel
mailing list