Parcourir la source

Atomic functions for shared Lua (experimental)

bel2125 il y a 7 ans
Parent
commit
b71b444228
3 fichiers modifiés avec 77 ajouts et 6 suppressions
  1. 2 2
      src/civetweb.c
  2. 55 3
      src/mod_lua_shared.inl
  3. 20 1
      test/page_shared.lua

+ 2 - 2
src/civetweb.c

@@ -6501,8 +6501,8 @@ mg_read_inner(struct mg_connection *conn, void *buf, size_t len)
 	int64_t n, buffered_len, nread;
 	int64_t len64 =
 	    (int64_t)((len > INT_MAX) ? INT_MAX : len); /* since the return value is
-	                                               * int, we may not read more
-	                                               * bytes */
+	                                                 * int, we may not read more
+	                                                 * bytes */
 	const char *body;
 
 	if (conn == NULL) {

+ 55 - 3
src/mod_lua_shared.inl

@@ -55,7 +55,7 @@ lua_shared_exit(void)
 	pthread_mutex_destroy(&lua_shared_lock);
 }
 
-
+#if defined(MG_EXPERIMENTAL_INTERFACES)
 double
 shared_locked_add(const char *name, size_t namlen, double value, int op)
 {
@@ -136,6 +136,46 @@ lua_shared_exchange(struct lua_State *L)
 	return 1;
 }
 
+/*
+static int
+lua_shared_push(struct lua_State *L)
+{
+	int val_type = lua_type(L, 1);
+
+	if ((val_type != LUA_TNUMBER) && (val_type != LUA_TSTRING)
+	    && (val_type != LUA_TBOOLEAN)) {
+		return luaL_error(L, "shared value must be string, number or boolean");
+	}
+
+	pthread_mutex_lock(&lua_shared_lock);
+
+	lua_getglobal(L_shared, "shared");
+	lua_pushnumber(L_shared, num);
+
+	if (val_type == LUA_TNUMBER) {
+		double num = lua_tonumber(L, 3);
+		lua_pushnumber(L_shared, num);
+
+	} else if (val_type == LUA_TBOOLEAN) {
+		int i = lua_toboolean(L, 3);
+		lua_pushboolean(L_shared, i);
+
+	} else {
+		size_t len = 0;
+		const char *str = lua_tolstring(L, 3, &len);
+		lua_pushlstring(L_shared, str, len);
+	}
+
+	lua_rawset(L_shared, -3);
+	lua_pop(L_shared, 1);
+
+	pthread_mutex_unlock(&lua_shared_lock);
+
+	return 0;
+}
+*/
+#endif
+
 
 /* Read access to shared element (x = shared.element) */
 static int
@@ -169,6 +209,7 @@ lua_shared_index(struct lua_State *L)
 		const char *str = lua_tolstring(L, 2, &len);
 
 		if ((len > 1) && (0 == memcmp(str, "__", 2))) {
+#if defined(MG_EXPERIMENTAL_INTERFACES)
 			/* Return functions */
 			if (0 == strcmp(str, "__add")) {
 				lua_pushcclosure(L, lua_shared_add, 0);
@@ -178,7 +219,15 @@ lua_shared_index(struct lua_State *L)
 				lua_pushcclosure(L, lua_shared_dec, 0);
 			} else if (0 == strcmp(str, "__exchange")) {
 				lua_pushcclosure(L, lua_shared_exchange, 0);
-			} else {
+                /*
+			} else if (0 == strcmp(str, "__push")) {
+				lua_pushcclosure(L, lua_shared_push, 0);
+			} else if (0 == strcmp(str, "__pop")) {
+				lua_pushcclosure(L, lua_shared_pop, 0);
+                */
+			} else
+#endif
+			{
 				/* Unknown reserved index */
 				lua_pushnil(L);
 			}
@@ -232,7 +281,7 @@ lua_shared_newindex(struct lua_State *L)
 		return luaL_error(L, "shared index must be string, number or boolean");
 	}
 	if ((val_type != LUA_TNUMBER) && (val_type != LUA_TSTRING)
-	    && (val_type != LUA_TBOOLEAN)) {
+	    && (val_type != LUA_TBOOLEAN) && (val_type != LUA_TNIL)) {
 		return luaL_error(L, "shared value must be string, number or boolean");
 	}
 
@@ -271,6 +320,9 @@ lua_shared_newindex(struct lua_State *L)
 		int i = lua_toboolean(L, 3);
 		lua_pushboolean(L_shared, i);
 
+	} else if (val_type == LUA_TNIL) {
+		lua_pushnil(L_shared);
+
 	} else {
 		size_t len = 0;
 		const char *str = lua_tolstring(L, 3, &len);

+ 20 - 1
test/page_shared.lua

@@ -82,7 +82,16 @@ mg.write("New elements are "
          .. tostring(y) .. " (type " .. type(y) .. ")\n")
 
 
--- Test elements functions
+-- Check if experimental functions (starting with __) are available
+if not shared.__inc then
+  mg.write("\nExperimental functions not available\n")
+  return
+else
+  mg.write("\nTesting experimental functions\n")
+end
+
+
+-- Test __inc/__dec functions
 if not shared.x then
   shared.x = 0
   shared.y = 0
@@ -90,4 +99,14 @@ end
 mg.write("__inc(x) = " .. shared.__inc("x") .. "\n")
 mg.write("__dec(y) = " .. shared.__dec("y") .. "\n")
 
+
+-- Test __add function
+if not shared.x then
+  shared.x = 0
+  shared.y = 0
+end
+mg.write("__add(x, 10) = " .. shared.__add("x", 10) .. "\n")
+mg.write("__add(y, -10) = " .. shared.__add("y", -10) .. "\n")
+
+
 -- end