CALLBACK_LOCK_POLL use len to differentiate locking on pollfd change
authorAndy Green <andy.green@linaro.org>
Fri, 20 Nov 2015 01:51:18 +0000 (09:51 +0800)
committerAndy Green <andy.green@linaro.org>
Fri, 20 Nov 2015 01:51:18 +0000 (09:51 +0800)
Signed-off-by: Andy Green <andy.green@linaro.org>
lib/libwebsockets.h
lib/pollfd.c

index c1285e8..ec17571 100644 (file)
@@ -954,6 +954,14 @@ struct libwebsocket_extension;
  *     LWS_CALLBACK_UNLOCK_POLL: These allow the external poll changes driven
  *             by libwebsockets to participate in an external thread locking
  *             scheme around the changes, so the whole thing is threadsafe.
+ *             These are called around three activities in the library,
+ *                     - inserting a new wsi in the wsi / fd table (len=1)
+ *                     - deleting a wsi from the wsi / fd table (len=1)
+ *                     - changing a wsi's POLLIN/OUT state (len=0)
+ *             Locking and unlocking external synchronization objects when
+ *             len == 1 allows external threads to be synchronized against
+ *             wsi lifecycle changes if it acquires the same lock for the
+ *             duration of wsi dereference from the other thread context.
  */
 LWS_VISIBLE LWS_EXTERN int callback(struct libwebsocket_context *context,
                        struct libwebsocket *wsi,
index 08fc405..b38df3d 100644 (file)
@@ -47,7 +47,7 @@ insert_wsi_socket_into_fds(struct libwebsocket_context *context,
 //                                         wsi, wsi->sock, context->fds_count);
 
        if (context->protocols[0].callback(context, wsi,
-           LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *) &pa, 0))
+           LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *) &pa, 1))
                return -1;
 
        insert_wsi(context, wsi);
@@ -63,7 +63,7 @@ insert_wsi_socket_into_fds(struct libwebsocket_context *context,
                return -1;
 
        if (context->protocols[0].callback(context, wsi,
-           LWS_CALLBACK_UNLOCK_POLL, wsi->user_space, (void *)&pa, 0))
+           LWS_CALLBACK_UNLOCK_POLL, wsi->user_space, (void *)&pa, 1))
                return -1;
 
        return 0;
@@ -92,7 +92,7 @@ remove_wsi_socket_from_fds(struct libwebsocket_context *context,
                                    wsi, wsi->sock, wsi->position_in_fds_table);
 
        if (context->protocols[0].callback(context, wsi,
-           LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *)&pa, 0))
+           LWS_CALLBACK_LOCK_POLL, wsi->user_space, (void *)&pa, 1))
                return -1;
 
        m = wsi->position_in_fds_table; /* replace the contents for this */
@@ -123,7 +123,7 @@ remove_wsi_socket_from_fds(struct libwebsocket_context *context,
        }
        if (context->protocols[0].callback(context, wsi,
                                       LWS_CALLBACK_UNLOCK_POLL,
-                                      wsi->user_space, (void *) &pa, 0))
+                                      wsi->user_space, (void *) &pa, 1))
                return -1;
 
        return 0;