OSDN Git Service

Revert "use poll() instead of select() where possible"
authorIvailo Monev <xakepa10@gmail.com>
Tue, 2 Feb 2021 00:51:34 +0000 (02:51 +0200)
committerIvailo Monev <xakepa10@gmail.com>
Tue, 2 Feb 2021 00:51:34 +0000 (02:51 +0200)
This reverts commit cad91a72b60c68b80b648b49b12199fc2094315f.

src/core/kernel/qcore_unix_p.h
src/network/socket/qlocalserver_unix.cpp
src/network/socket/qlocalsocket_unix.cpp
src/network/socket/qnativesocketengine_unix.cpp

index 0c7f366..82794bc 100644 (file)
@@ -48,7 +48,6 @@
 #include "qplatformdefs.h"
 #include "qatomic.h"
 
-#include <poll.h>
 #include <string.h>
 #include <sys/wait.h>
 #include <errno.h>
@@ -258,14 +257,6 @@ static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
     return ret;
 }
 
-static inline int qt_safe_poll(int fd, int events, int timeout)
-{
-    struct pollfd pd;
-    pd.fd = fd;
-    pd.events = events;
-    return ::poll(&pd, 1, timeout);
-}
-
 timeval qt_gettime(); // in qelapsedtimer_unix.cpp
 
 Q_CORE_EXPORT int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept,
index a9b72ce..708e2df 100644 (file)
@@ -174,8 +174,16 @@ void QLocalServerPrivate::_q_onNewConnection()
 
 void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut)
 {
-    int result = qt_safe_poll(listenSocket, POLLIN, (msec == -1) ? 0 : msec);
-    if (result == -1) {
+    fd_set readfds;
+    FD_ZERO(&readfds);
+    FD_SET(listenSocket, &readfds);
+
+    timeval timeout;
+    timeout.tv_sec = msec / 1000;
+    timeout.tv_usec = (msec % 1000) * 1000;
+
+    int result = qt_safe_select(listenSocket + 1, &readfds, 0, 0, (msec == -1) ? 0 : &timeout);
+    if (-1 == result) {
         setError(QLatin1String("QLocalServer::waitForNewConnection"));
         closeServer();
     }
index 718cf53..041c450 100644 (file)
@@ -503,12 +503,20 @@ bool QLocalSocket::waitForConnected(int msec)
     if (state() != ConnectingState)
         return (state() == ConnectedState);
 
+    fd_set fds;
+    FD_ZERO(&fds);
+    FD_SET(d->connectingSocket, &fds);
+
+    timeval timeout;
+    timeout.tv_sec = msec / 1000;
+    timeout.tv_usec = (msec % 1000) * 1000;
+
     // timeout can not be 0 or else select will return an error.
     if (msec == 0)
-        msec = 1000;
+        timeout.tv_usec = 1000;
 
     while (state() == ConnectingState && msec == -1) {
-        int result = qt_safe_poll(d->connectingSocket, POLLIN, msec);
+        int result = qt_safe_select(d->connectingSocket + 1, &fds, 0, 0, &timeout);
         if (result == -1 && errno != EINTR) {
             d->errorOccurred( QLocalSocket::UnknownSocketError,
                     QLatin1String("QLocalSocket::waitForConnected"));
index a11880c..6379731 100644 (file)
@@ -1039,27 +1039,48 @@ qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxSize)
 
 int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) const
 {
-    return qt_safe_poll(socketDescriptor, selectForRead ? POLLIN : POLLOUT, timeout < 0 ? 0 : timeout);
+    fd_set fds;
+    FD_ZERO(&fds);
+    FD_SET(socketDescriptor, &fds);
+
+    struct timeval tv;
+    tv.tv_sec = timeout / 1000;
+    tv.tv_usec = (timeout % 1000) * 1000;
+
+    int retval;
+    if (selectForRead)
+        retval = qt_safe_select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv);
+    else
+        retval = qt_safe_select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv);
+
+    return retval;
 }
 
 int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite,
                        bool *selectForRead, bool *selectForWrite) const
 {
-    struct pollfd pd;
-    pd.fd = socketDescriptor;
-    if (checkRead) {
-        pd.events |= POLLIN;
-    }
-    if (checkWrite) {
-        pd.events |= POLLOUT;
-    }
+    fd_set fdread;
+    FD_ZERO(&fdread);
+    if (checkRead)
+        FD_SET(socketDescriptor, &fdread);
 
-    int ret = ::poll(&pd, 1, timeout < 0 ? 0 : timeout); 
+    fd_set fdwrite;
+    FD_ZERO(&fdwrite);
+    if (checkWrite)
+        FD_SET(socketDescriptor, &fdwrite);
+
+    struct timeval tv;
+    tv.tv_sec = timeout / 1000;
+    tv.tv_usec = (timeout % 1000) * 1000;
+
+    int ret;
+    ret = qt_safe_select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv);
+
+    if (ret <= 0)
+        return ret;
+    *selectForRead = FD_ISSET(socketDescriptor, &fdread);
+    *selectForWrite = FD_ISSET(socketDescriptor, &fdwrite);
 
-    if (ret > 0) {
-        *selectForRead = (pd.revents & POLLIN);
-        *selectForWrite = (pd.revents & POLLOUT);
-    }
     return ret;
 }