[PATCH xserver 20/25] os: Generalize poll fd management code

Keith Packard keithp at keithp.com
Wed May 25 05:38:57 UTC 2016


We want to use this in the input thread too

Signed-off-by: Keith Packard <keithp at keithp.com>
---
 os/WaitFor.c    |  10 ++---
 os/connection.c | 123 ++++++--------------------------------------------------
 os/osdep.h      |  29 +++++++------
 os/utils.c      |  89 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 121 insertions(+), 130 deletions(-)

diff --git a/os/WaitFor.c b/os/WaitFor.c
index a709859..21d9d97 100644
--- a/os/WaitFor.c
+++ b/os/WaitFor.c
@@ -211,7 +211,7 @@ WaitForSomething(Bool clients_are_ready)
 
             if (wt)
                 poll_timeout = wt->tv_sec * 1000 + wt->tv_usec / 1000;
-            i = poll(poll_fds, poll_fds_num, poll_timeout);
+            i = poll(server_poll.fds, server_poll.num, poll_timeout);
         }
         pollerr = GetErrno();
         WakeupHandler(i);
@@ -279,11 +279,11 @@ WaitForSomething(Bool clients_are_ready)
                     }
                 }
             }
-            for (p = 0; p < poll_fds_num; p++) {
-                short   revents = poll_fds[p].revents;
+            for (p = 0; p < server_poll.num; p++) {
+                short   revents = server_poll.fds[p].revents;
 
                 if (revents) {
-                    int client_index = ConnectionTranslation[poll_fds[p].fd];
+                    int client_index = ConnectionTranslation[server_poll.fds[p].fd];
 
                     if (client_index) {
                         if (revents & ~(POLLIN|POLLOUT))
@@ -298,7 +298,7 @@ WaitForSomething(Bool clients_are_ready)
                             }
                         }
                     } else {
-                        HandleNotifyFd(poll_fds[p].fd, revents);
+                        HandleNotifyFd(server_poll.fds[p].fd, revents);
                     }
                 }
             }
diff --git a/os/connection.c b/os/connection.c
index 6b74c76..c9e56b7 100644
--- a/os/connection.c
+++ b/os/connection.c
@@ -121,8 +121,7 @@ SOFTWARE.
 
 static int lastfdesc;           /* maximum file descriptor */
 
-struct pollfd   *poll_fds;
-int             poll_fds_num;
+struct ospoll   server_poll;
 
 int MaxClients = 0;
 int NumNotifyWriteFd;           /* Number of NotifyFd members with write set */
@@ -159,10 +158,7 @@ int *ConnectionTranslation = NULL;
  * list.
  */
 
-#undef MAXSOCKS
 #define MAXSOCKS 512
-#undef MAXSELECT
-#define MAXSELECT 512
 
 struct _ct_node {
     struct _ct_node *next;
@@ -292,9 +288,6 @@ InitConnectionLimits(void)
     if (lastfdesc < 0)
         lastfdesc = MAXSOCKS;
 
-    if (lastfdesc > MAXSELECT)
-        lastfdesc = MAXSELECT;
-
     if (lastfdesc > MAXCLIENTS) {
         lastfdesc = MAXCLIENTS;
         if (debug_conns)
@@ -386,7 +379,7 @@ CreateWellKnownSockets(void)
     int i;
     int partial;
 
-    poll_fd_init();
+    ospoll_fd_init(&server_poll);
 
 #if !defined(WIN32)
     for (i = 0; i < MaxClients; i++)
@@ -776,7 +769,7 @@ AllocNewConnection(XtransConnInfo trans_conn, int fd, CARD32 conn_time)
 #else
     SetConnectionTranslation(fd, client->index);
 #endif
-    poll_fd_add(fd);
+    ospoll_fd_add(&server_poll, fd);
     set_poll_client(client);
 
 #ifdef DEBUG
@@ -926,7 +919,7 @@ CloseDownFileDescriptor(OsCommPtr oc)
 #else
     SetConnectionTranslation(connection, 0);
 #endif
-    poll_fd_remove(connection);
+    ospoll_fd_remove(&server_poll, connection);
 }
 
 /*****************
@@ -1044,24 +1037,24 @@ SetNotifyFd(int fd, NotifyFdProcPtr notify, int mask, void *data)
 
     if (changes & X_NOTIFY_READ) {
         if (mask & X_NOTIFY_READ) {
-            poll_fd_add(fd);
-            poll_fd_listen(fd, POLLIN);
+            ospoll_fd_add(&server_poll, fd);
+            ospoll_fd_listen(&server_poll, fd, POLLIN);
         } else {
-            poll_fd_mute(fd, POLLIN);
+            ospoll_fd_mute(&server_poll, fd, POLLIN);
         }
     }
 
     if (changes & X_NOTIFY_WRITE) {
         if (mask & X_NOTIFY_WRITE) {
-            poll_fd_add(fd);
-            poll_fd_listen(fd, POLLOUT);
+            ospoll_fd_add(&server_poll, fd);
+            ospoll_fd_listen(&server_poll, fd, POLLOUT);
         } else {
-            poll_fd_mute(fd, POLLOUT);
+            ospoll_fd_mute(&server_poll, fd, POLLOUT);
         }
     }
 
     if (mask == 0) {
-        poll_fd_remove(fd);
+        ospoll_fd_remove(&server_poll, fd);
         xorg_list_del(&n->list);
         free(n);
     } else {
@@ -1293,8 +1286,6 @@ AddClientOnOpenFD(int fd)
     return TRUE;
 }
 
-static int      poll_fds_size;
-
 Bool
 listen_to_client(ClientPtr client)
 {
@@ -1321,9 +1312,9 @@ set_poll_client(ClientPtr client)
     OsCommPtr oc = (OsCommPtr) client->osPrivate;
 
     if (listen_to_client(client))
-        poll_fd_listen(oc->fd, POLLIN);
+        ospoll_fd_listen(&server_poll, oc->fd, POLLIN);
     else
-        poll_fd_mute(oc->fd, POLLIN);
+        ospoll_fd_mute(&server_poll, oc->fd, POLLIN);
 }
 
 static void
@@ -1338,91 +1329,3 @@ set_poll_clients(void)
     }
 }
 
-static int
-poll_fd_find(int fd)
-{
-	int lo = 0;
-	int hi = poll_fds_num - 1;
-
-	while (lo <= hi) {
-		int m = (lo + hi) >> 1;
-		int t = poll_fds[m].fd;
-
-		if (t < fd)
-			lo = m + 1;
-		else if (t > fd)
-			hi = m - 1;
-		else
-			return m;
-	}
-	return -(lo + 1);
-}
-
-void
-poll_fd_init(void)
-{
-    free(poll_fds);
-    poll_fds = NULL;
-    poll_fds_size = poll_fds_num = 0;
-}
-
-Bool
-poll_fd_add(int fd)
-{
-    int pos = poll_fd_find(fd);
-    if (pos >= 0)
-        return TRUE;
-
-    if (poll_fds_num == poll_fds_size) {
-        struct pollfd   *new_poll_fds;
-        int             new_poll_fds_size = poll_fds_size ? poll_fds_size * 2 : MAXCLIENTS * 2;
-
-        new_poll_fds = realloc(poll_fds, new_poll_fds_size * sizeof (struct pollfd));
-        if (!new_poll_fds)
-            return FALSE;
-        poll_fds = new_poll_fds;
-        poll_fds_size = new_poll_fds_size;
-    }
-    pos = -pos - 1;
-    memmove(&poll_fds[pos + 1], &poll_fds[pos], (poll_fds_num - pos) * sizeof (struct pollfd));
-    poll_fds_num++;
-
-    poll_fds[pos].fd = fd;
-    poll_fds[pos].events = 0;
-    poll_fds[pos].revents = 0;
-    return TRUE;
-}
-
-void
-poll_fd_remove(int fd)
-{
-    int pos = poll_fd_find(fd);
-
-    if (pos < 0)
-        return;
-
-    poll_fds_num--;
-    memmove(&poll_fds[pos], &poll_fds[pos+1], (poll_fds_num - pos) * sizeof (struct pollfd));
-}
-
-void
-poll_fd_listen(int fd, short events)
-{
-    int pos = poll_fd_find(fd);
-
-    if (pos < 0)
-        return;
-
-    poll_fds[pos].events |= events;
-}
-
-void
-poll_fd_mute(int fd, short events)
-{
-    int pos = poll_fd_find(fd);
-
-    if (pos < 0)
-        return;
-
-    poll_fds[pos].events &= ~events;
-}
diff --git a/os/osdep.h b/os/osdep.h
index 63df590..528cf8a 100644
--- a/os/osdep.h
+++ b/os/osdep.h
@@ -91,14 +91,8 @@ SOFTWARE.
  * like sysconf(_SC_OPEN_MAX) is not supported.
  */
 
-#if OPEN_MAX <= 512
-#define MAXSOCKS (OPEN_MAX - 1)
-#else
-#define MAXSOCKS 512
-#endif
-
-/* MAXSELECT is the number of fds that select() can handle */
-#define MAXSELECT (sizeof(fd_set) * NBBY)
+#define MAXSOCKS OPEN_MAX
+#define MAXSELECT MAXSOCKS
 
 #include <stddef.h>
 
@@ -174,26 +168,31 @@ extern void HandleNotifyFds(void);
 
 #include "dix.h"
 
-extern struct pollfd   *poll_fds;
-extern int             poll_fds_num;
+struct ospoll {
+    struct pollfd       *fds;
+    int                 num;
+    int                 size;
+};
+
+extern struct ospoll    server_poll;
 
 Bool
 listen_to_client(ClientPtr client);
 
 void
-poll_fd_init(void);
+ospoll_fd_init(struct ospoll *ospoll);
 
 Bool
-poll_fd_add(int fd);
+ospoll_fd_add(struct ospoll *ospoll, int fd);
 
 void
-poll_fd_remove(int fd);
+ospoll_fd_remove(struct ospoll *ospoll, int fd);
 
 void
-poll_fd_listen(int fd, short events);
+ospoll_fd_listen(struct ospoll *ospoll, int fd, short events);
 
 void
-poll_fd_mute(int fd, short events);
+ospoll_fd_mute(struct ospoll *ospoll, int fd, short events);
 
 #if !defined(WIN32) || defined(__CYGWIN__)
 extern int *ConnectionTranslation;
diff --git a/os/utils.c b/os/utils.c
index b3e5926..b5195d9 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -2131,3 +2131,92 @@ os_move_fd(int fd)
     return newfd;
 }
 #endif
+
+static int
+ospoll_fd_find(struct ospoll *ospoll, int fd)
+{
+	int lo = 0;
+	int hi = ospoll->num - 1;
+
+	while (lo <= hi) {
+		int m = (lo + hi) >> 1;
+		int t = ospoll->fds[m].fd;
+
+		if (t < fd)
+			lo = m + 1;
+		else if (t > fd)
+			hi = m - 1;
+		else
+			return m;
+	}
+	return -(lo + 1);
+}
+
+void
+ospoll_fd_init(struct ospoll *ospoll)
+{
+    free(ospoll->fds);
+    ospoll->fds = NULL;
+    ospoll->size = ospoll->num = 0;
+}
+
+Bool
+ospoll_fd_add(struct ospoll *ospoll, int fd)
+{
+    int pos = ospoll_fd_find(ospoll, fd);
+    if (pos >= 0)
+        return TRUE;
+
+    if (ospoll->num == ospoll->size) {
+        struct pollfd   *new_fds;
+        int             new_size = ospoll->size ? ospoll->size * 2 : MAXCLIENTS * 2;
+
+        new_fds = realloc(ospoll->fds, new_size * sizeof (struct pollfd));
+        if (!new_fds)
+            return FALSE;
+        ospoll->fds = new_fds;
+        ospoll->size = new_size;
+    }
+    pos = -pos - 1;
+    memmove(&ospoll->fds[pos + 1], &ospoll->fds[pos], (ospoll->num - pos) * sizeof (struct pollfd));
+    ospoll->num++;
+
+    ospoll->fds[pos].fd = fd;
+    ospoll->fds[pos].events = 0;
+    ospoll->fds[pos].revents = 0;
+    return TRUE;
+}
+
+void
+ospoll_fd_remove(struct ospoll *ospoll, int fd)
+{
+    int pos = ospoll_fd_find(ospoll, fd);
+
+    if (pos < 0)
+        return;
+
+    ospoll->num--;
+    memmove(&ospoll->fds[pos], &ospoll->fds[pos+1], (ospoll->num - pos) * sizeof (struct pollfd));
+}
+
+void
+ospoll_fd_listen(struct ospoll *ospoll, int fd, short events)
+{
+    int pos = ospoll_fd_find(ospoll, fd);
+
+    if (pos < 0)
+        return;
+
+    ospoll->fds[pos].events |= events;
+}
+
+void
+ospoll_fd_mute(struct ospoll *ospoll, int fd, short events)
+{
+    int pos = ospoll_fd_find(ospoll, fd);
+
+    if (pos < 0)
+        return;
+
+    ospoll->fds[pos].events &= ~events;
+}
-- 
2.8.0.rc3



More information about the xorg-devel mailing list