unify all pollfd lock management
authorAndy Green <andy.green@linaro.org>
Sat, 21 Dec 2013 03:18:34 +0000 (11:18 +0800)
committerAndy Green <andy.green@linaro.org>
Sat, 21 Dec 2013 03:18:34 +0000 (11:18 +0800)
This provides a single place for pollfd event changing,
external locking for that and extpoll management.

It saves about 85 lines of duplication and simplifies the callers.

Signed-off-by: Andy Green <andy.green@linaro.org>
lib/client-handshake.c
lib/client.c
lib/libwebsockets.c
lib/private-libwebsockets.h
lib/server.c

index 31099b8..fc8ffb7 100644 (file)
@@ -82,13 +82,7 @@ struct libwebsocket *libwebsocket_client_connect_2(
                         * must do specifically a POLLOUT poll to hear
                         * about the connect completion
                         */
-
-                       context->fds[wsi->position_in_fds_table].events |= POLLOUT;
-
-                       /* external POLL support via protocol 0 */
-                       context->protocols[0].callback(context, wsi,
-                               LWS_CALLBACK_SET_MODE_POLL_FD,
-                               wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
+                       lws_change_pollfd(wsi, 0, POLLOUT);
 
                        return wsi;
                }
index 243757b..0481aaa 100644 (file)
@@ -119,20 +119,7 @@ int lws_client_socket_service(struct libwebsocket_context *context,
                 * happening at a time when there's no real connection yet
                 */
 
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_LOCK_POLL,
-                       wsi->user_space, (void *)(long)wsi->sock, 0);
-
-               pollfd->events &= ~POLLOUT;
-
-               /* external POLL support via protocol 0 */
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_CLEAR_MODE_POLL_FD,
-                       wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
-
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_UNLOCK_POLL,
-                       wsi->user_space, (void *)(long)wsi->sock, 0);
+               lws_change_pollfd(wsi, POLLOUT, 0);
 
                /* we can retry this... just cook the SSL BIO the first time */
 
index 5562573..687bad6 100644 (file)
@@ -808,23 +808,9 @@ user_service:
 #endif
        /* one shot */
 
-       if (pollfd) {
+       if (pollfd)
+               lws_change_pollfd(wsi, POLLOUT, 0);
 
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_LOCK_POLL,
-                       wsi->user_space, (void *)(long)wsi->sock, 0);
-
-               pollfd->events &= ~POLLOUT;
-
-               /* external POLL support via protocol 0 */
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_CLEAR_MODE_POLL_FD,
-                       wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
-
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_UNLOCK_POLL,
-                       wsi->user_space, (void *)(long)wsi->sock, 0);
-       }
 #ifndef LWS_NO_EXTENSIONS
 notify_action:
 #endif
@@ -1057,8 +1043,8 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
                if ((pollfd->revents & POLLOUT) &&
                        wsi->state == WSI_STATE_ESTABLISHED &&
                           lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) {
-                               lwsl_info("libwebsocket_service_fd: closing\n");
-                               goto close_and_handled;
+                       lwsl_info("libwebsocket_service_fd: closing\n");
+                       goto close_and_handled;
                }
 
                if (wsi->u.ws.rxflow_buffer &&
@@ -1418,6 +1404,35 @@ lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
 }
 #endif
 
+void
+lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or)
+{
+       struct libwebsocket_context *context = wsi->protocol->owning_server;
+
+       context->protocols[0].callback(context, wsi,
+               LWS_CALLBACK_LOCK_POLL,
+               wsi->user_space, (void *)(long)wsi->sock, 0);
+
+       context->fds[wsi->position_in_fds_table].events &= ~_and;
+       context->fds[wsi->position_in_fds_table].events |= _or;
+
+       /* external POLL support via protocol 0 */
+       if (_and)
+               context->protocols[0].callback(context, wsi,
+                       LWS_CALLBACK_CLEAR_MODE_POLL_FD,
+                       wsi->user_space, (void *)(long)wsi->sock, _and);
+
+       if (_or)
+               context->protocols[0].callback(context, wsi,
+                       LWS_CALLBACK_SET_MODE_POLL_FD,
+                       wsi->user_space, (void *)(long)wsi->sock, _or);
+
+       context->protocols[0].callback(context, wsi,
+               LWS_CALLBACK_UNLOCK_POLL,
+               wsi->user_space, (void *)(long)wsi->sock, 0);
+}
+
+
 /**
  * libwebsocket_callback_on_writable() - Request a callback when this socket
  *                                      becomes able to be written to without
@@ -1456,20 +1471,7 @@ libwebsocket_callback_on_writable(struct libwebsocket_context *context,
                return -1;
        }
 
-       context->protocols[0].callback(context, wsi,
-               LWS_CALLBACK_LOCK_POLL,
-               wsi->user_space, (void *)(long)wsi->sock, 0);
-
-       context->fds[wsi->position_in_fds_table].events |= POLLOUT;
-
-       /* external POLL support via protocol 0 */
-       context->protocols[0].callback(context, wsi,
-               LWS_CALLBACK_SET_MODE_POLL_FD,
-               wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
-
-       context->protocols[0].callback(context, wsi,
-               LWS_CALLBACK_UNLOCK_POLL,
-               wsi->user_space, (void *)(long)wsi->sock, 0);
+       lws_change_pollfd(wsi, 0, POLLOUT);
 
        return 1;
 }
@@ -1615,29 +1617,10 @@ _libwebsocket_rx_flow_control(struct libwebsocket *wsi)
 
        /* adjust the pollfd for this wsi */
 
-       context->protocols[0].callback(context, wsi,
-               LWS_CALLBACK_LOCK_POLL,
-               wsi->user_space, (void *)(long)wsi->sock, 0);
-
        if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
-               context->fds[wsi->position_in_fds_table].events |= POLLIN;
+               lws_change_pollfd(wsi, 0, POLLIN);
        else
-               context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
-
-       if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)
-               /* external POLL support via protocol 0 */
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_SET_MODE_POLL_FD,
-                       wsi->user_space, (void *)(long)wsi->sock, POLLIN);
-       else
-               /* external POLL support via protocol 0 */
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_CLEAR_MODE_POLL_FD,
-                       wsi->user_space, (void *)(long)wsi->sock, POLLIN);
-
-       context->protocols[0].callback(context, wsi,
-               LWS_CALLBACK_UNLOCK_POLL,
-               wsi->user_space, (void *)(long)wsi->sock, 0);
+               lws_change_pollfd(wsi, POLLIN, 0);
 
        return 1;
 }
index ea68f4b..a7236c6 100644 (file)
@@ -543,6 +543,9 @@ lws_hdr_simple_create(struct libwebsocket *wsi,
 LWS_EXTERN int
 libwebsocket_ensure_user_space(struct libwebsocket *wsi);
 
+LWS_EXTERN void
+lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or);
+
 #ifndef LWS_NO_SERVER
 LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
                                                      struct libwebsocket *wsi);
index 3eb0ffe..ad7ba42 100644 (file)
@@ -202,7 +202,6 @@ int lws_server_socket_service(struct libwebsocket_context *context,
                                return 0;
 
                        /* hum he may have used up the writability above */
-
                        break;
                }
 
@@ -212,21 +211,7 @@ int lws_server_socket_service(struct libwebsocket_context *context,
                        break;
 
                /* one shot */
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_LOCK_POLL,
-                       wsi->user_space, (void *)(long)wsi->sock, 0);
-
-               pollfd->events &= ~POLLOUT;
-
-               /* external POLL support via protocol 0 */
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_CLEAR_MODE_POLL_FD,
-                       wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
-
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_UNLOCK_POLL,
-                       wsi->user_space, (void *)(long)wsi->sock, 0);
-
+               lws_change_pollfd(wsi, POLLOUT, 0);
 
                if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE) {
                        n = user_callback_handle_rxflow(
@@ -360,20 +345,7 @@ int lws_server_socket_service(struct libwebsocket_context *context,
 
        case LWS_CONNMODE_SSL_ACK_PENDING:
 
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_LOCK_POLL,
-                       wsi->user_space, (void *)(long)wsi->sock, 0);
-
-               pollfd->events &= ~POLLOUT;
-
-               /* external POLL support via protocol 0 */
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_CLEAR_MODE_POLL_FD,
-                       wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
-
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_UNLOCK_POLL,
-                       wsi->user_space, (void *)(long)wsi->sock, 0);
+               lws_change_pollfd(wsi, POLLOUT, 0);
 
                lws_latency_pre(context, wsi);
 
@@ -417,37 +389,12 @@ int lws_server_socket_service(struct libwebsocket_context *context,
                                                  m, ERR_error_string(m, NULL));
 
                        if (m == SSL_ERROR_WANT_READ) {
-                               context->protocols[0].callback(context, wsi,
-                                       LWS_CALLBACK_LOCK_POLL,
-                                       wsi->user_space, (void *)(long)wsi->sock, 0);
-                               context->fds[
-                                  wsi->position_in_fds_table].events |= POLLIN;
-
-                               /* external POLL support via protocol 0 */
-                               context->protocols[0].callback(context, wsi,
-                                       LWS_CALLBACK_SET_MODE_POLL_FD,
-                                       wsi->user_space,
-                                       (void *)(long)wsi->sock, POLLIN);
-                               context->protocols[0].callback(context, wsi,
-                                       LWS_CALLBACK_UNLOCK_POLL,
-                                       wsi->user_space, (void *)(long)wsi->sock, 0);
+                               lws_change_pollfd(wsi, 0, POLLIN);
                                lwsl_info("SSL_ERROR_WANT_READ\n");
                                break;
                        }
                        if (m == SSL_ERROR_WANT_WRITE) {
-                               context->protocols[0].callback(context, wsi,
-                                       LWS_CALLBACK_LOCK_POLL,
-                                       wsi->user_space, (void *)(long)wsi->sock, 0);
-                               context->fds[
-                                 wsi->position_in_fds_table].events |= POLLOUT;
-                               /* external POLL support via protocol 0 */
-                               context->protocols[0].callback(context, wsi,
-                                       LWS_CALLBACK_SET_MODE_POLL_FD,
-                                       wsi->user_space,
-                                       (void *)(long)wsi->sock, POLLOUT);
-                               context->protocols[0].callback(context, wsi,
-                                       LWS_CALLBACK_UNLOCK_POLL,
-                                       wsi->user_space, (void *)(long)wsi->sock, 0);
+                               lws_change_pollfd(wsi, 0, POLLOUT);
                                break;
                        }
                        lwsl_debug("SSL_accept failed skt %u: %s\n",