unify all pollfd lock management

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>
diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index 31099b8..fc8ffb7 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -82,13 +82,7 @@
 			 * 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;
 		}
diff --git a/lib/client.c b/lib/client.c
index 243757b..0481aaa 100644
--- a/lib/client.c
+++ b/lib/client.c
@@ -119,20 +119,7 @@
 		 * 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 */
 
diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index 5562573..687bad6 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -808,23 +808,9 @@
 #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 @@
 		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 @@
 }
 #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 @@
 		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 @@
 
 	/* 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;
 }
diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h
index ea68f4b..a7236c6 100644
--- a/lib/private-libwebsockets.h
+++ b/lib/private-libwebsockets.h
@@ -543,6 +543,9 @@
 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);
diff --git a/lib/server.c b/lib/server.c
index 3eb0ffe..ad7ba42 100644
--- a/lib/server.c
+++ b/lib/server.c
@@ -202,7 +202,6 @@
 				return 0;
 
 			/* hum he may have used up the writability above */
-
 			break;
 		}
 
@@ -212,21 +211,7 @@
 			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 @@
 
 	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 @@
 						  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",