changeset 71889:2385a5c8186c

Fix high cpu load for server sockets. (pfn_WSAEventSelect): New function ptr. (init_winsock): Load it. (sys_listen): Set FILE_LISTEN flag. Set event mask for socket's char_avail event object to FD_ACCEPT. (sys_accept): Check FILE_LISTEN flag. Set event mask on new socket's char_avail event object to FD_READ|FD_CLOSE.
author Kim F. Storm <storm@cua.dk>
date Fri, 14 Jul 2006 09:29:32 +0000
parents f07726615400
children b0584c551b8c
files src/w32.c
diffstat 1 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/w32.c	Fri Jul 14 09:29:22 2006 +0000
+++ b/src/w32.c	Fri Jul 14 09:29:32 2006 +0000
@@ -2700,6 +2700,7 @@
 int (PASCAL *pfn_WSAStartup) (WORD wVersionRequired, LPWSADATA lpWSAData);
 void (PASCAL *pfn_WSASetLastError) (int iError);
 int (PASCAL *pfn_WSAGetLastError) (void);
+int (PASCAL *pfn_WSAEventSelect) (SOCKET s, HANDLE hEventObject, long lNetworkEvents);
 int (PASCAL *pfn_socket) (int af, int type, int protocol);
 int (PASCAL *pfn_bind) (SOCKET s, const struct sockaddr *addr, int namelen);
 int (PASCAL *pfn_connect) (SOCKET s, const struct sockaddr *addr, int namelen);
@@ -2782,6 +2783,7 @@
       LOAD_PROC( WSAStartup );
       LOAD_PROC( WSASetLastError );
       LOAD_PROC( WSAGetLastError );
+      LOAD_PROC( WSAEventSelect );
       LOAD_PROC( socket );
       LOAD_PROC( bind );
       LOAD_PROC( connect );
@@ -3295,6 +3297,11 @@
       int rc = pfn_listen (SOCK_HANDLE (s), backlog);
       if (rc == SOCKET_ERROR)
 	set_errno ();
+      else
+	{
+	  fd_info[s].flags |= FILE_LISTEN;
+	  pfn_WSAEventSelect (SOCK_HANDLE (s), fd_info[s].cp->char_avail, FD_ACCEPT);
+	}
       return rc;
     }
   h_errno = ENOTSOCK;
@@ -3332,11 +3339,16 @@
     }
 
   check_errno ();
-  if (fd_info[s].flags & FILE_SOCKET)
+  if (fd_info[s].flags & FILE_LISTEN)
     {
       SOCKET t = pfn_accept (SOCK_HANDLE (s), addr, addrlen);
       if (t != INVALID_SOCKET)
-	return socket_to_fd (t);
+	{
+	  int fd = socket_to_fd (t);
+	  if (fd >= 0)
+	    pfn_WSAEventSelect (SOCK_HANDLE (fd), fd_info[fd].cp->char_avail, FD_READ | FD_CLOSE);
+	  return fd;
+	}
 
       set_errno ();
       return -1;