From c12b61b7fb7e87b3854864a7ac21d2a94bf11f2d Mon Sep 17 00:00:00 2001
From: KO Myung-Hun <komh78@gmail.com>
Date: Mon, 7 Sep 2015 15:56:39 +0900
Subject: [PATCH] os2: thread: improve vlc_poll_os2()

If non-sockets, set revents to POLLNVAL;
---
 src/os2/thread.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 64 insertions(+), 13 deletions(-)

diff --git a/src/os2/thread.c b/src/os2/thread.c
index e221632..fa677ad 100644
--- a/src/os2/thread.c
+++ b/src/os2/thread.c
@@ -784,6 +784,8 @@ int vlc_poll_os2( struct pollfd *fds, unsigned nfds, int timeout )
 {
     fd_set rdset, wrset, exset;
 
+    int non_sockets = 0;
+
     struct timeval tv = { 0, 0 };
 
     int val = -1;
@@ -794,6 +796,17 @@ int vlc_poll_os2( struct pollfd *fds, unsigned nfds, int timeout )
     for( unsigned i = 0; i < nfds; i++ )
     {
         int fd = fds[ i ].fd;
+
+        fds[ i ].revents = 0;
+        if( getsockopt( fd, SOL_SOCKET, SO_TYPE,
+                        &( int ){ 0 }, &( int ){ sizeof( int )}) == -1 &&
+            ( errno == ENOTSOCK || errno == EBADF ))
+        {
+            fds[ i ].revents = POLLNVAL;
+            non_sockets++;
+            continue;
+        }
+
         if( val < fd )
             val = fd;
 
@@ -811,24 +824,62 @@ int vlc_poll_os2( struct pollfd *fds, unsigned nfds, int timeout )
             FD_SET( fd, &exset );
     }
 
-    if( timeout >= 0 )
+    /* Sockets included ? */
+    if( val != -1 )
     {
-        div_t d    = div( timeout, 1000 );
-        tv.tv_sec  = d.quot;
-        tv.tv_usec = d.rem * 1000;
-    }
+        fd_set saved_rdset = rdset;
+        fd_set saved_wrset = wrset;
+        fd_set saved_exset = exset;
 
-    val = vlc_select( val + 1, &rdset, &wrset, &exset,
-                      ( timeout >= 0 ) ? &tv : NULL );
-    if( val == -1 )
-        return -1;
+        /* Check pending sockets */
+        switch( vlc_select( val + 1, &rdset, &wrset, &exset, &tv ))
+        {
+            case -1 :   /* Error */
+                return -1;
+
+            case 0 :    /* Timeout */
+                /* Socket only ? */
+                if( non_sockets == 0 )
+                {
+                    struct timeval *ptv = NULL;
+
+                    if( timeout >= 0 )
+                    {
+                        div_t d    = div( timeout, 1000 );
+                        tv.tv_sec  = d.quot;
+                        tv.tv_usec = d.rem * 1000;
+
+                        ptv = &tv;
+                    }
+
+                    rdset = saved_rdset;
+                    wrset = saved_wrset;
+                    exset = saved_exset;
+
+                    if( vlc_select( val + 1, &rdset, &wrset, &exset, ptv )
+                            == -1 )
+                        return -1;
+                }
+                break;
+
+            default:    /* Ready */
+                break;
+        }
+    }
 
+    val = 0;
     for( unsigned i = 0; i < nfds; i++ )
     {
-        int fd = fds[ i ].fd;
-        fds[ i ].revents = ( FD_ISSET( fd, &rdset ) ? POLLIN  : 0 )
-                         | ( FD_ISSET( fd, &wrset ) ? POLLOUT : 0 )
-                         | ( FD_ISSET( fd, &exset ) ? POLLPRI : 0 );
+        if( fds[ i ].revents == 0 )
+        {
+            int fd = fds[ i ].fd;
+            fds[ i ].revents = ( FD_ISSET( fd, &rdset ) ? POLLIN  : 0 )
+                             | ( FD_ISSET( fd, &wrset ) ? POLLOUT : 0 )
+                             | ( FD_ISSET( fd, &exset ) ? POLLPRI : 0 );
+        }
+
+        if( fds[ i ].revents != 0 )
+            val++;
     }
 
     return val;
-- 
2.5.2

