From 9bbf937ed7cc0dc678c04d1ff6211e4b9c125b33 Mon Sep 17 00:00:00 2001 From: Fernando Carrijo Date: Thu, 26 Aug 2010 23:36:33 -0300 Subject: [PATCH 3/4] inputthread: Use our own count for the highest fd among threaded input devices MaxInputDevices was nothing but a short-lived subterfuge for us, lazy developers, who for some time refrained from maintaining our accurate count for the highest fd among the fds of all threaded input devices. Worse than that: it leaked an input thread concern into a few other subsystems of the server, which represents a poor design, at best. Signed-off-by: Fernando Carrijo --- include/opaque.h | 4 ---- os/connection.c | 8 -------- os/inputthread.c | 16 ++++++++++++++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/opaque.h b/include/opaque.h index 2d6807f..b3c7c70 100644 --- a/include/opaque.h +++ b/include/opaque.h @@ -39,10 +39,6 @@ extern _X_EXPORT int MaxClients; extern _X_EXPORT volatile char isItTimeToYield; extern _X_EXPORT volatile char dispatchException; -#ifdef INPUT_THREAD -extern int MaxInputDevices; -#endif - /* bit values for dispatchException */ #define DE_RESET 1 #define DE_TERMINATE 2 diff --git a/os/connection.c b/os/connection.c index 81340b6..77910be 100644 --- a/os/connection.c +++ b/os/connection.c @@ -145,10 +145,6 @@ int MaxClients = 0; Bool NewOutputPending; /* not yet attempted to write some new output */ Bool AnyClientsWriteBlocked; /* true if some client blocked on write */ -#ifdef INPUT_THREAD -int MaxInputDevices = 0; -#endif - static Bool RunFromSmartParent; /* send SIGUSR1 to parent process */ Bool PartialNetwork; /* continue even if unable to bind all addrs */ static Pid_t ParentProcess; @@ -306,10 +302,6 @@ InitConnectionLimits(void) if (lastfdesc > MAXSELECT) lastfdesc = MAXSELECT; -#ifdef INPUT_THREAD - MaxInputDevices = lastfdesc; -#endif - if (lastfdesc > MAXCLIENTS) { lastfdesc = MAXCLIENTS; diff --git a/os/inputthread.c b/os/inputthread.c index 8ad52c4..081a8fb 100644 --- a/os/inputthread.c +++ b/os/inputthread.c @@ -59,6 +59,7 @@ typedef struct { pthread_t thread; threaded_input_device *devs; fd_set fds; + int max_fd; int read_pipe; int write_pipe; } threaded_input_info; @@ -156,6 +157,8 @@ threaded_input_register_device(int fd, threaded_input->devs = new; FD_SET(fd, &threaded_input->fds); + if (fd > threaded_input->max_fd) + threaded_input->max_fd = fd; threaded_input_fill_pipe(hotplugPipeWrite); DebugF("threaded-input: registered device %d\n", fd); @@ -173,9 +176,9 @@ threaded_input_register_device(int fd, int threaded_input_unregister_device(int fd) { + int max_fd = -1; threaded_input_device *prev, *dev; - /* return silently if input thread is already finished (e.g., at * DisableDevice time, evdev tries to call this function again through * xf86RemoveEnabledDevice */ @@ -201,12 +204,19 @@ threaded_input_unregister_device(int fd) prev->next = dev->next; FD_CLR(fd, &threaded_input->fds); + dev->read_input_proc = NULL; dev->read_input_args = NULL; dev->fd = 0; dev = 0; free(dev); + /* Once the device is unlinked from the list, we update max_fd */ + for (dev = threaded_input->devs; dev != NULL; dev = dev->next) + if (dev->fd > max_fd) + max_fd = dev->fd; + threaded_input->max_fd = max_fd; + threaded_input_fill_pipe(hotplugPipeWrite); DebugF("threaded-input: unregistered device: %d\n", fd); @@ -242,7 +252,7 @@ threaded_input_do_work(void *arg) DebugF("threaded-input: do_work waiting for devices\n"); - if (Select(MaxInputDevices, &ready_fds, NULL, NULL, NULL) < 0) + if (Select(threaded_input->max_fd + 1, &ready_fds, NULL, NULL, NULL) < 0) { if (errno == EINVAL) { @@ -289,6 +299,7 @@ threaded_input_pre_init(void) threaded_input->thread = 0; threaded_input->devs = NULL; FD_ZERO(&threaded_input->fds); + threaded_input->max_fd = -1; /* By making read head non-blocking, we ensure that while the main thread * is busy servicing client requests, the dedicated input thread can work @@ -359,6 +370,7 @@ threaded_input_fini(void) } threaded_input->devs = NULL; FD_ZERO(&threaded_input->fds); + threaded_input->max_fd = -1; RemoveGeneralSocket(threaded_input->read_pipe); close(threaded_input->read_pipe); -- 1.7.0.4