GNOME Bugzilla – Bug 732439
GSocket: avoid unnecessary g_socket_wait_condition() for blocking sockets
Last modified: 2015-01-17 14:08:58 UTC
Created attachment 279550 [details] [review] gsocket: don't check socket state on blocking sockets on the first attempt For GSockets set to be blocking we simulate the blocking with a pattern like this: while (TRUE) { if (socket->priv->blocking && !g_socket_condition_wait (socket, G_IO_IN, cancellable, error)) return -1; if ((res = do_something (socket, ...)) < 0) { if (socket->priv->blocking && error == EAGAIN) continue; ... } } This means we run the g_socket_condition_wait() even in the first loop iteration before even trying to do anything on the socket. This seems unnecessary and should only be done once we got back an EAGAIN after trying. The attached patch skips the wait in the first loop iteration. It uses the results variable to check whether it's the first iteration. I don't know if you would prefer that or if you would prefer a separate gboolean first variable for clarity.
i think it would be clearer if you rearranged the code more. maybe something like: while ((res = do_something (socket, ...)) < 0) { if (errno == EINTR) continue; if (socket->priv->blocking && errno == EAGAIN) { if (g_socket_condition_wait (socket, G_IO_IN, cancellable, error)) continue; else return -1; } g_set_error (...); return -1; }
Comment on attachment 279550 [details] [review] gsocket: don't check socket state on blocking sockets on the first attempt Thanks for the review, will fix as you suggest.
This is basically a dupe of #741707 which has an updated patch. As described in the other bug, this is not only an optimization, but it is also required on win32 when you create the gsocked from an existing fd, since the fd is not notified to be writable until you try to write into it
Marking as duplicate since the other patch was pushed *** This bug has been marked as a duplicate of bug 741707 ***