Merge pull request #2816 from xmcclure/profile-clean-0
[mono.git] / mono / metadata / threadpool-ms-io-poll.c
index a0a64b15820f093fe2bdde6d835f71f68133a0d5..4adae56791b40af7ffab900966b10c267a908000 100644 (file)
@@ -1,24 +1,5 @@
 
-#if defined(HAVE_POLL)
-
-#if defined(HAVE_POLL_H)
-#include <poll.h>
-#elif defined(HAVE_SYS_POLL_H)
-#include <sys/poll.h>
-#endif
-
-typedef struct pollfd mono_pollfd;
-
-#elif defined(HOST_WIN32)
-
-#include "mswsock.h"
-
-typedef WSAPOLLFD mono_pollfd;
-
-#else
-/* poll is not defined */
-#error
-#endif
+#include "utils/mono-poll.h"
 
 static mono_pollfd *poll_fds;
 static guint poll_fds_capacity;
@@ -42,7 +23,7 @@ poll_init (gint wakeup_pipe_fd)
 
        poll_fds = g_new0 (mono_pollfd, poll_fds_capacity);
 
-       POLL_INIT_FD (&poll_fds [0], wakeup_pipe_fd, POLLIN);
+       POLL_INIT_FD (&poll_fds [0], wakeup_pipe_fd, MONO_POLLIN);
 
        return TRUE;
 }
@@ -66,9 +47,9 @@ poll_register_fd (gint fd, gint events, gboolean is_new)
 
        poll_event = 0;
        if (events & EVENT_IN)
-               poll_event |= POLLIN;
+               poll_event |= MONO_POLLIN;
        if (events & EVENT_OUT)
-               poll_event |= POLLOUT;
+               poll_event |= MONO_POLLOUT;
 
        for (i = 0; i < poll_fds_size; ++i) {
                if (poll_fds [i].fd == fd) {
@@ -93,7 +74,7 @@ poll_register_fd (gint fd, gint events, gboolean is_new)
                poll_fds_capacity *= 2;
                g_assert (poll_fds_size <= poll_fds_capacity);
 
-               poll_fds = g_renew (mono_pollfd, poll_fds, poll_fds_capacity);
+               poll_fds = (mono_pollfd *)g_renew (mono_pollfd, poll_fds, poll_fds_capacity);
        }
 
        POLL_INIT_FD (&poll_fds [poll_fds_size - 1], fd, poll_event);
@@ -128,6 +109,36 @@ poll_remove_fd (gint fd)
                poll_fds_size -= 1;
 }
 
+static inline gint
+poll_mark_bad_fds (mono_pollfd *poll_fds, gint poll_fds_size)
+{
+       gint i, ready = 0;
+
+       for (i = 0; i < poll_fds_size; i++) {
+               if (poll_fds [i].fd == -1)
+                       continue;
+
+               switch (mono_poll (&poll_fds [i], 1, 0)) {
+               case 1:
+                       ready++;
+                       break;
+               case -1:
+#if !defined(HOST_WIN32)
+                       if (errno == EBADF)
+#else
+                       if (WSAGetLastError () == WSAEBADF)
+#endif
+                       {
+                               poll_fds [i].revents |= MONO_POLLNVAL;
+                               ready++;
+                       }
+                       break;
+               }
+       }
+
+       return ready;
+}
+
 static gint
 poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gpointer user_data)
 {
@@ -138,13 +149,7 @@ poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gp
 
        mono_gc_set_skip_thread (TRUE);
 
-#if !defined(HOST_WIN32)
-       ready = poll (poll_fds, poll_fds_size, -1);
-#else
-       ready = WSAPoll(poll_fds, poll_fds_size, -1);
-       if (ready == SOCKET_ERROR)
-               ready = -1;
-#endif
+       ready = mono_poll (poll_fds, poll_fds_size, -1);
 
        mono_gc_set_skip_thread (FALSE);
 
@@ -178,6 +183,15 @@ poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gp
                        ready = 0;
                        break;
                }
+#if !defined(HOST_WIN32)
+               case EBADF:
+#else
+               case WSAEBADF:
+#endif
+               {
+                       ready = poll_mark_bad_fds (poll_fds, poll_fds_size);
+                       break;
+               }
                default:
 #if !defined(HOST_WIN32)
                        g_error ("poll_event_wait: mono_poll () failed, error (%d) %s", errno, g_strerror (errno));
@@ -190,6 +204,10 @@ poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gp
 
        if (ready == -1)
                return -1;
+       if (ready == 0)
+               return 0;
+
+       g_assert (ready > 0);
 
        for (i = 0; i < poll_fds_size; ++i) {
                gint fd, events = 0;
@@ -200,10 +218,12 @@ poll_event_wait (void (*callback) (gint fd, gint events, gpointer user_data), gp
                        continue;
 
                fd = poll_fds [i].fd;
-               if (poll_fds [i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL))
+               if (poll_fds [i].revents & (MONO_POLLIN | MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
                        events |= EVENT_IN;
-               if (poll_fds [i].revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL))
+               if (poll_fds [i].revents & (MONO_POLLOUT | MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
                        events |= EVENT_OUT;
+               if (poll_fds [i].revents & (MONO_POLLERR | MONO_POLLHUP | MONO_POLLNVAL))
+                       events |= EVENT_ERR;
 
                callback (fd, events, user_data);