guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: should accept be protected with fport_wait_for_input?


From: Ludovic Courtès
Subject: Re: should accept be protected with fport_wait_for_input?
Date: Tue, 04 Dec 2007 18:32:09 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)

Hi,

dskr <address@hidden> writes:

> the fport fill input code provides a way to avoid some threaded
> deadlocks with fport_wait_for_input. I appreciate this tremendously!
>
> Should this protection be extended to 'accept' in the socket code to
> prevent the entire runtime from blocking if a thread blocks on accept?

You're right, we should leave "guile mode" before blocking in
`accept(2)'.  I committed the attached patch, let me know if it works
for you.

Thanks!

Ludovic.

--- orig/libguile/socket.c
+++ mod/libguile/socket.c
@@ -36,6 +36,8 @@
 #include "libguile/validate.h"
 #include "libguile/socket.h"
 
+#include "libguile/iselect.h"
+
 #ifdef __MINGW32__
 #include "win32-socket.h"
 #endif
@@ -1321,16 +1323,30 @@
            "connection and will continue to accept new requests.")
 #define FUNC_NAME s_scm_accept
 {
-  int fd;
+  int fd, selected;
   int newfd;
   SCM address;
   SCM newsock;
+  SELECT_TYPE readfds, exceptfds;
   socklen_t addr_size = MAX_ADDR_SIZE;
   scm_t_max_sockaddr addr;
 
   sock = SCM_COERCE_OUTPORT (sock);
   SCM_VALIDATE_OPFPORT (1, sock);
   fd = SCM_FPORT_FDES (sock);
+
+  FD_ZERO (&readfds);
+  FD_ZERO (&exceptfds);
+  FD_SET (fd, &readfds);
+  FD_SET (fd, &exceptfds);
+
+  /* Block until something happens on FD, leaving guile mode while
+     waiting.  */
+  selected = scm_std_select (fd + 1, &readfds, NULL, &exceptfds,
+                            NULL);
+  if (selected < 0)
+    SCM_SYSERROR;
+
   newfd = accept (fd, (struct sockaddr *) &addr, &addr_size);
   if (newfd == -1)
     SCM_SYSERROR;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]