[PATCH] Only lock send buffer in sopoll() if needed

John Baldwin jhb at freebsd.org
Tue Sep 30 18:01:02 UTC 2014


Right now sopoll() always locks both socket buffers.  The receive socket 
buffer lock is always needed, but the send socket buffer lock is only needed 
while polling for writing (there is a potential test of SBS_CANTSENDMORE 
without the lock, but I think this might be ok).  What do folks think?

Index: uipc_socket.c
===================================================================
--- uipc_socket.c       (revision 272305)
+++ uipc_socket.c       (working copy)
@@ -3003,7 +3003,12 @@ sopoll_generic(struct socket *so, int events, stru
 {
        int revents = 0;
 
-       SOCKBUF_LOCK(&so->so_snd);
+       if (events & (POLLOUT | POLLWRNORM))
+               SOCKBUF_LOCK(&so->so_snd);
+       /*
+        * The so_rcv lock doubles as SOCK_LOCK so it it is needed for
+        * all requests.
+        */
        SOCKBUF_LOCK(&so->so_rcv);
        if (events & (POLLIN | POLLRDNORM))
                if (soreadabledata(so))
@@ -3038,7 +3043,8 @@ sopoll_generic(struct socket *so, int events, stru
        }
 
        SOCKBUF_UNLOCK(&so->so_rcv);
-       SOCKBUF_UNLOCK(&so->so_snd);
+       if (events & (POLLOUT | POLLWRNORM))
+               SOCKBUF_UNLOCK(&so->so_snd);
        return (revents);
 }

-- 
John Baldwin


More information about the freebsd-net mailing list