[mini] Add new JIT flag JIT_FLAG_DISCARD_RESULTS to reduce the noise when profiling...
[mono.git] / mono / io-layer / sockets.c
index aaa53ffe16d157b16554960ba51f2ab35c430ab8..d98ac677a2e22047432a89a717ad9b1a849f37ed 100644 (file)
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
 #include <mono/io-layer/socket-private.h>
-#include <mono/io-layer/handles-private.h>
 #include <mono/io-layer/socket-wrappers.h>
 #include <mono/io-layer/io-trace.h>
 #include <mono/utils/mono-poll.h>
 #include <mono/utils/mono-once.h>
 #include <mono/utils/mono-logger-internals.h>
+#include <mono/metadata/w32handle.h>
 
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 static guint32 in_cleanup = 0;
 
 static void socket_close (gpointer handle, gpointer data);
+static void socket_details (gpointer data);
+static const gchar* socket_typename (void);
+static gsize socket_typesize (void);
 
-struct _WapiHandleOps _wapi_socket_ops = {
+static MonoW32HandleOps _wapi_socket_ops = {
        socket_close,           /* close */
        NULL,                   /* signal */
        NULL,                   /* own */
        NULL,                   /* is_owned */
        NULL,                   /* special_wait */
-       NULL                    /* prewait */
+       NULL,                   /* prewait */
+       socket_details, /* details */
+       socket_typename,        /* typename */
+       socket_typesize,        /* typesize */
 };
 
-static mono_once_t socket_ops_once=MONO_ONCE_INIT;
-
-static void socket_ops_init (void)
+void
+_wapi_socket_init (void)
 {
-       /* No capabilities to register */
+       mono_w32handle_register_ops (MONO_W32HANDLE_SOCKET, &_wapi_socket_ops);
 }
 
 static void socket_close (gpointer handle, gpointer data)
 {
        int ret;
        struct _WapiHandle_socket *socket_handle = (struct _WapiHandle_socket *)data;
+       MonoThreadInfo *info = mono_thread_info_current ();
 
        MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: closing socket handle %p", __func__, handle);
 
@@ -91,7 +97,7 @@ static void socket_close (gpointer handle, gpointer data)
        do {
                ret = close (GPOINTER_TO_UINT(handle));
        } while (ret == -1 && errno == EINTR &&
-                !_wapi_thread_cur_apc_pending ());
+                !mono_thread_info_is_interrupt_state (info));
        
        if (ret == -1) {
                gint errnum = errno;
@@ -105,11 +111,28 @@ static void socket_close (gpointer handle, gpointer data)
                socket_handle->saved_error = 0;
 }
 
+static void socket_details (gpointer data)
+{
+       /* FIXME: do something */
+}
+
+static const gchar* socket_typename (void)
+{
+       return "Socket";
+}
+
+static gsize socket_typesize (void)
+{
+       return sizeof (struct _WapiHandle_socket);
+}
+
 static gboolean
-cleanup_close (gpointer handle, gpointer data)
+cleanup_close (gpointer handle, gpointer data, gpointer user_data)
 {
-       _wapi_handle_ops_close (handle, NULL);
-       return TRUE;
+       if (mono_w32handle_get_type (handle) == MONO_W32HANDLE_SOCKET)
+               mono_w32handle_force_close (handle, data);
+
+       return FALSE;
 }
 
 void _wapi_cleanup_networking(void)
@@ -117,7 +140,7 @@ void _wapi_cleanup_networking(void)
        MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: cleaning up", __func__);
 
        in_cleanup = 1;
-       _wapi_handle_foreach (WAPI_HANDLE_SOCKET, cleanup_close, NULL);
+       mono_w32handle_foreach (cleanup_close, NULL);
        in_cleanup = 0;
 }
 
@@ -135,12 +158,12 @@ int closesocket(guint32 fd)
 {
        gpointer handle = GUINT_TO_POINTER (fd);
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(0);
        }
        
-       _wapi_handle_unref (handle);
+       mono_w32handle_unref (handle);
        return(0);
 }
 
@@ -152,18 +175,19 @@ guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
        struct _WapiHandle_socket new_socket_handle = {0};
        gboolean ok;
        int new_fd;
+       MonoThreadInfo *info = mono_thread_info_current ();
 
        if (addr != NULL && *addrlen < sizeof(struct sockaddr)) {
                WSASetLastError (WSAEFAULT);
                return(INVALID_SOCKET);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(INVALID_SOCKET);
        }
        
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
+       ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET,
                                  (gpointer *)&socket_handle);
        if (ok == FALSE) {
                g_warning ("%s: error looking up socket handle %p",
@@ -175,7 +199,7 @@ guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
        do {
                new_fd = accept (fd, addr, addrlen);
        } while (new_fd == -1 && errno == EINTR &&
-                !_wapi_thread_cur_apc_pending());
+                !mono_thread_info_is_interrupt_state (info));
 
        if (new_fd == -1) {
                gint errnum = errno;
@@ -187,7 +211,7 @@ guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
                return(INVALID_SOCKET);
        }
 
-       if (new_fd >= _wapi_fd_reserve) {
+       if (new_fd >= mono_w32handle_fd_reserve) {
                MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big", __func__);
 
                WSASetLastError (WSASYSCALLFAILURE);
@@ -202,9 +226,9 @@ guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
        new_socket_handle.protocol = socket_handle->protocol;
        new_socket_handle.still_readable = 1;
 
-       new_handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, new_fd,
+       new_handle = mono_w32handle_new_fd (MONO_W32HANDLE_SOCKET, new_fd,
                                          &new_socket_handle);
-       if(new_handle == _WAPI_HANDLE_INVALID) {
+       if(new_handle == INVALID_HANDLE_VALUE) {
                g_warning ("%s: error creating socket handle", __func__);
                WSASetLastError (ERROR_GEN_FAILURE);
                return(INVALID_SOCKET);
@@ -221,7 +245,7 @@ int _wapi_bind(guint32 fd, struct sockaddr *my_addr, socklen_t addrlen)
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -245,8 +269,9 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
        struct _WapiHandle_socket *socket_handle;
        gboolean ok;
        gint errnum;
-       
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       MonoThreadInfo *info = mono_thread_info_current ();
+
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -275,8 +300,8 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                         * But don't do this for EWOULDBLOCK (bug 317315)
                         */
                        if (errnum != WSAEWOULDBLOCK) {
-                               ok = _wapi_lookup_handle (handle,
-                                                         WAPI_HANDLE_SOCKET,
+                               ok = mono_w32handle_lookup (handle,
+                                                         MONO_W32HANDLE_SOCKET,
                                                          (gpointer *)&socket_handle);
                                if (ok == FALSE) {
                                        /* ECONNRESET means the socket was closed by another thread */
@@ -293,7 +318,7 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                fds.fd = fd;
                fds.events = MONO_POLLOUT;
                while (mono_poll (&fds, 1, -1) == -1 &&
-                      !_wapi_thread_cur_apc_pending ()) {
+                      !mono_thread_info_is_interrupt_state (info)) {
                        if (errno != EINTR) {
                                errnum = errno_to_WSA (errno, __func__);
 
@@ -321,7 +346,7 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                        errnum = errno_to_WSA (so_error, __func__);
 
                        /* Need to save this socket error */
-                       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
+                       ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET,
                                                  (gpointer *)&socket_handle);
                        if (ok == FALSE) {
                                g_warning ("%s: error looking up socket handle %p", __func__, handle);
@@ -345,7 +370,7 @@ int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -370,7 +395,7 @@ int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -400,7 +425,7 @@ int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
        struct _WapiHandle_socket *socket_handle;
        gboolean ok;
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -431,7 +456,7 @@ int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
        }
 
        if (optname == SO_ERROR) {
-               ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
+               ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET,
                                          (gpointer *)&socket_handle);
                if (ok == FALSE) {
                        g_warning ("%s: error looking up socket handle %p",
@@ -459,7 +484,7 @@ int _wapi_listen(guint32 fd, int backlog)
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -490,8 +515,9 @@ int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
        struct _WapiHandle_socket *socket_handle;
        gboolean ok;
        int ret;
+       MonoThreadInfo *info = mono_thread_info_current ();
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -499,7 +525,7 @@ int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
        do {
                ret = recvfrom (fd, buf, len, recv_flags, from, fromlen);
        } while (ret == -1 && errno == EINTR &&
-                !_wapi_thread_cur_apc_pending ());
+                !mono_thread_info_is_interrupt_state (info));
 
        if (ret == 0 && len > 0) {
                /* According to the Linux man page, recvfrom only
@@ -520,7 +546,7 @@ int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
                 * still_readable != 1 then shutdown
                 * (SHUT_RD|SHUT_RDWR) has been called locally.
                 */
-               ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
+               ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET,
                                          (gpointer *)&socket_handle);
                if (ok == FALSE || socket_handle->still_readable != 1) {
                        ret = -1;
@@ -547,8 +573,9 @@ _wapi_recvmsg(guint32 fd, struct msghdr *msg, int recv_flags)
        struct _WapiHandle_socket *socket_handle;
        gboolean ok;
        int ret;
+       MonoThreadInfo *info = mono_thread_info_current ();
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -556,11 +583,11 @@ _wapi_recvmsg(guint32 fd, struct msghdr *msg, int recv_flags)
        do {
                ret = recvmsg (fd, msg, recv_flags);
        } while (ret == -1 && errno == EINTR &&
-                !_wapi_thread_cur_apc_pending ());
+                !mono_thread_info_is_interrupt_state (info));
 
        if (ret == 0) {
                /* see _wapi_recvfrom */
-               ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
+               ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET,
                                          (gpointer *)&socket_handle);
                if (ok == FALSE || socket_handle->still_readable != 1) {
                        ret = -1;
@@ -584,8 +611,9 @@ int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
 {
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
+       MonoThreadInfo *info = mono_thread_info_current ();
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -593,7 +621,7 @@ int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
        do {
                ret = send (fd, msg, len, send_flags);
        } while (ret == -1 && errno == EINTR &&
-                !_wapi_thread_cur_apc_pending ());
+                !mono_thread_info_is_interrupt_state (info));
 
        if (ret == -1) {
                gint errnum = errno;
@@ -621,8 +649,9 @@ int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
 {
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
+       MonoThreadInfo *info = mono_thread_info_current ();
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -630,7 +659,7 @@ int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
        do {
                ret = sendto (fd, msg, len, send_flags, to, tolen);
        } while (ret == -1 && errno == EINTR &&
-                !_wapi_thread_cur_apc_pending ());
+                !mono_thread_info_is_interrupt_state (info));
 
        if (ret == -1) {
                gint errnum = errno;
@@ -649,8 +678,9 @@ _wapi_sendmsg(guint32 fd,  const struct msghdr *msg, int send_flags)
 {
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
+       MonoThreadInfo *info = mono_thread_info_current ();
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -658,7 +688,7 @@ _wapi_sendmsg(guint32 fd,  const struct msghdr *msg, int send_flags)
        do {
                ret = sendmsg (fd, msg, send_flags);
        } while (ret == -1 && errno == EINTR &&
-                !_wapi_thread_cur_apc_pending ());
+                !mono_thread_info_is_interrupt_state (info));
 
        if (ret == -1) {
                gint errnum = errno;
@@ -684,7 +714,7 @@ int _wapi_setsockopt(guint32 fd, int level, int optname,
 #endif
        struct timeval tv;
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -747,14 +777,14 @@ int _wapi_shutdown(guint32 fd, int how)
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
 
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
 
        if (how == SHUT_RD ||
            how == SHUT_RDWR) {
-               ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
+               ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET,
                                          (gpointer *)&socket_handle);
                if (ok == FALSE) {
                        g_warning ("%s: error looking up socket handle %p",
@@ -811,9 +841,9 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
                return(INVALID_SOCKET);
        }
 
-       if (fd >= _wapi_fd_reserve) {
+       if (fd >= mono_w32handle_fd_reserve) {
                MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: File descriptor is too big (%d >= %d)",
-                          __func__, fd, _wapi_fd_reserve);
+                          __func__, fd, mono_w32handle_fd_reserve);
 
                WSASetLastError (WSASYSCALLFAILURE);
                close (fd);
@@ -856,10 +886,8 @@ guint32 _wapi_socket(int domain, int type, int protocol, void *unused,
        }
        
        
-       mono_once (&socket_ops_once, socket_ops_init);
-       
-       handle = _wapi_handle_new_fd (WAPI_HANDLE_SOCKET, fd, &socket_handle);
-       if (handle == _WAPI_HANDLE_INVALID) {
+       handle = mono_w32handle_new_fd (MONO_W32HANDLE_SOCKET, fd, &socket_handle);
+       if (handle == INVALID_HANDLE_VALUE) {
                g_warning ("%s: error creating socket handle", __func__);
                WSASetLastError (WSASYSCALLFAILURE);
                close (fd);
@@ -878,7 +906,7 @@ static gboolean socket_disconnect (guint32 fd)
        gpointer handle = GUINT_TO_POINTER (fd);
        int newsock, ret;
        
-       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SOCKET,
+       ok = mono_w32handle_lookup (handle, MONO_W32HANDLE_SOCKET,
                                  (gpointer *)&socket_handle);
        if (ok == FALSE) {
                g_warning ("%s: error looking up socket handle %p", __func__,
@@ -947,6 +975,7 @@ static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped,
 static gint
 wapi_sendfile (guint32 socket, gpointer fd, guint32 bytes_to_write, guint32 bytes_per_send, guint32 flags)
 {
+       MonoThreadInfo *info = mono_thread_info_current ();
 #if defined(HAVE_SENDFILE) && (defined(__linux__) || defined(DARWIN))
        gint file = GPOINTER_TO_INT (fd);
        gint n;
@@ -969,7 +998,7 @@ wapi_sendfile (guint32 socket, gpointer fd, guint32 bytes_to_write, guint32 byte
                /* TODO: Might not send the entire file for non-blocking sockets */
                res = sendfile (file, socket, 0, &statbuf.st_size, NULL, 0);
 #endif
-       } while (res != -1 && (errno == EINTR || errno == EAGAIN) && !_wapi_thread_cur_apc_pending ());
+       } while (res != -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
        if (res == -1) {
                errnum = errno;
                errnum = errno_to_WSA (errnum, __func__);
@@ -986,7 +1015,7 @@ wapi_sendfile (guint32 socket, gpointer fd, guint32 bytes_to_write, guint32 byte
        do {
                do {
                        n = read (file, buffer, SF_BUFFER_SIZE);
-               } while (n == -1 && errno == EINTR && !_wapi_thread_cur_apc_pending ());
+               } while (n == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
                if (n == -1)
                        break;
                if (n == 0) {
@@ -995,8 +1024,8 @@ wapi_sendfile (guint32 socket, gpointer fd, guint32 bytes_to_write, guint32 byte
                }
                do {
                        n = send (socket, buffer, n, 0); /* short sends? enclose this in a loop? */
-               } while (n == -1 && errno == EINTR && !_wapi_thread_cur_apc_pending ());
-       } while (n != -1);
+               } while (n == -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
+       } while (n != -1 && errno == EINTR && !mono_thread_info_is_interrupt_state (info));
 
        if (n == -1) {
                gint errnum = errno;
@@ -1017,7 +1046,7 @@ TransmitFile (guint32 socket, gpointer file, guint32 bytes_to_write, guint32 byt
        gpointer sock = GUINT_TO_POINTER (socket);
        gint ret;
 
-       if (_wapi_handle_type (sock) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (sock) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return FALSE;
        }
@@ -1066,7 +1095,7 @@ WSAIoctl (guint32 fd, gint32 command,
        int ret;
        gchar *buffer = NULL;
 
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return SOCKET_ERROR;
        }
@@ -1204,7 +1233,7 @@ int ioctlsocket(guint32 fd, unsigned long command, gpointer arg)
        gpointer handle = GUINT_TO_POINTER (fd);
        int ret;
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -1275,6 +1304,7 @@ int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
                 fd_set *exceptfds, struct timeval *timeout)
 {
        int ret, maxfd;
+       MonoThreadInfo *info = mono_thread_info_current ();
        
        for (maxfd = FD_SETSIZE-1; maxfd >= 0; maxfd--) {
                if ((readfds && FD_ISSET (maxfd, readfds)) ||
@@ -1293,7 +1323,7 @@ int _wapi_select(int nfds G_GNUC_UNUSED, fd_set *readfds, fd_set *writefds,
                ret = select(maxfd + 1, readfds, writefds, exceptfds,
                             timeout);
        } while (ret == -1 && errno == EINTR &&
-                !_wapi_thread_cur_apc_pending ());
+                !mono_thread_info_is_interrupt_state (info));
 
        if (ret == -1) {
                gint errnum = errno;
@@ -1316,7 +1346,7 @@ void _wapi_FD_CLR(guint32 fd, fd_set *set)
                return;
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return;
        }
@@ -1333,7 +1363,7 @@ int _wapi_FD_ISSET(guint32 fd, fd_set *set)
                return(0);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return(0);
        }
@@ -1350,7 +1380,7 @@ void _wapi_FD_SET(guint32 fd, fd_set *set)
                return;
        }
 
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (mono_w32handle_get_type (handle) != MONO_W32HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return;
        }