瀏覽代碼

Export global mutex

bel 11 年之前
父節點
當前提交
811f4d29f0
共有 3 個文件被更改,包括 36 次插入16 次删除
  1. 12 2
      include/civetweb.h
  2. 21 11
      src/civetweb.c
  3. 3 3
      src/mod_lua.inl

+ 12 - 2
include/civetweb.h

@@ -330,8 +330,18 @@ CIVETWEB_API int mg_websocket_write(struct mg_connection* conn, int opcode,
    Invoke this before mg_write or mg_printf when communicating with a
    websocket if your code has server-initiated communication as well as
    communication in direct response to a message. */
-CIVETWEB_API void mg_lock(struct mg_connection* conn);
-CIVETWEB_API void mg_unlock(struct mg_connection* conn);
+CIVETWEB_API void mg_lock_connection(struct mg_connection* conn);
+CIVETWEB_API void mg_unlock_connection(struct mg_connection* conn);
+
+#if defined(MG_LEGACY_INTERFACE)
+#define mg_lock mg_lock_connection
+#define mg_unlock mg_unlock_connection
+#endif
+
+/* Lock server context.  This lock may be used to protect ressources
+   that are shared between different connection/worker threads. */
+CIVETWEB_API void mg_lock_context(struct mg_context* ctx);
+CIVETWEB_API void mg_unlock_context(struct mg_context* ctx);
 
 
 /* Opcodes, from http://tools.ietf.org/html/rfc6455 */

+ 21 - 11
src/civetweb.c

@@ -504,16 +504,16 @@ static __inline void   mg_free(void * a)               {free(a);}
 
 /* This following lines are just meant as a reminder to use the mg-functions for memory management */
 #ifdef malloc
-    #undef malloc 
+    #undef malloc
 #endif
 #ifdef calloc
-    #undef calloc 
+    #undef calloc
 #endif
 #ifdef realloc
-    #undef realloc 
+    #undef realloc
 #endif
 #ifdef free
-    #undef free 
+    #undef free
 #endif
 #define malloc  DO_NOT_USE_THIS_FUNCTION__USE_mg_malloc
 #define calloc  DO_NOT_USE_THIS_FUNCTION__USE_mg_calloc
@@ -835,7 +835,7 @@ struct mg_connection {
     int throttle;                   /* Throttling, bytes/sec. <= 0 means no throttle */
     time_t last_throttle_time;      /* Last time throttled data was sent */
     int64_t last_throttle_bytes;    /* Bytes sent this second */
-    pthread_mutex_t mutex;          /* Used by mg_lock/mg_unlock to ensure atomic transmissions for websockets */
+    pthread_mutex_t mutex;          /* Used by mg_lock_connection/mg_unlock_connection to ensure atomic transmissions for websockets */
 #if defined(USE_LUA) && defined(USE_WEBSOCKET)
     void * lua_websocket_state;     /* Lua_State for a websocket connection */
 #endif
@@ -4840,16 +4840,26 @@ static void handle_propfind(struct mg_connection *conn, const char *path,
     conn->num_bytes_sent += mg_printf(conn, "%s\n", "</d:multistatus>");
 }
 
-void mg_lock(struct mg_connection* conn)
+void mg_lock_connection(struct mg_connection* conn)
 {
     (void) pthread_mutex_lock(&conn->mutex);
 }
 
-void mg_unlock(struct mg_connection* conn)
+void mg_unlock_connection(struct mg_connection* conn)
 {
     (void) pthread_mutex_unlock(&conn->mutex);
 }
 
+void mg_lock_context(struct mg_context* ctx)
+{
+    (void) pthread_mutex_lock(&ctx->nonce_mutex);
+}
+
+void mg_unlock_context(struct mg_context* ctx)
+{
+    (void) pthread_mutex_unlock(&ctx->nonce_mutex);
+}
+
 #if defined(USE_TIMERS)
 #include "timer.inl"
 #endif /* USE_TIMERS */
@@ -5257,10 +5267,10 @@ int mg_websocket_write(struct mg_connection* conn, int opcode, const char* data,
        but mongoose's mg_printf/mg_write is not (because of the loop in
        push(), although that is only a problem if the packet is large or
        outgoing buffer is full). */
-    (void) mg_lock(conn);
+    (void) mg_lock_connection(conn);
     retval = mg_write(conn, header, headerLen);
     retval = mg_write(conn, data, dataLen);
-    mg_unlock(conn);
+    mg_unlock_connection(conn);
 
     return retval;
 }
@@ -6254,7 +6264,7 @@ static void close_connection(struct mg_connection *conn)
     if (conn->ctx->callbacks.connection_close != NULL)
         conn->ctx->callbacks.connection_close(conn);
 
-    mg_lock(conn);
+    mg_lock_connection(conn);
 
     conn->must_close = 1;
 
@@ -6271,7 +6281,7 @@ static void close_connection(struct mg_connection *conn)
         conn->client.sock = INVALID_SOCKET;
     }
 
-    mg_unlock(conn);
+    mg_unlock_connection(conn);
 }
 
 void mg_close_connection(struct mg_connection *conn)

+ 3 - 3
src/mod_lua.inl

@@ -1180,7 +1180,7 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
     assert(conn->lua_websocket_state == NULL);
 
     /* lock list (mg_context global) */
-    (void)pthread_mutex_lock(&conn->ctx->nonce_mutex);
+    mg_lock_context(conn->ctx);
     while (*shared_websock_list) {
         /* check if ws already in list */
         if (0==strcmp(script,(*shared_websock_list)->ws.script)) {
@@ -1192,7 +1192,7 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
         /* add ws to list */
         *shared_websock_list = mg_calloc(sizeof(struct mg_shared_lua_websocket_list), 1);
         if (*shared_websock_list == NULL) {
-            (void)pthread_mutex_unlock(&conn->ctx->nonce_mutex);
+            mg_unlock_context(conn->ctx);
             mg_cry(conn, "Cannot create shared websocket struct, OOM");
             return NULL;
         }
@@ -1219,7 +1219,7 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
         (void)pthread_mutex_lock(&(ws->ws_mutex));
         (*shared_websock_list)->ws.conn[(ws->references)++] = conn;
     }
-    (void)pthread_mutex_unlock(&conn->ctx->nonce_mutex);
+    mg_unlock_context(conn->ctx);
 
     /* call add */
     lua_getglobal(ws->state, "open");