[Tarantool-patches] [PATCH v3 3/4] lua/utils: make LUA_NOREF value public

olegrok at tarantool.org olegrok at tarantool.org
Fri Sep 3 23:49:17 MSK 2021


From: Oleg Babin <babinoleg at mail.ru>

Before this patch we used an assumption in lua fiber code that
all valid lua refs are positive numbers. However there are
special constants defined in lua header for such purpose LUA_NOREF
and LUA_REFNIL [1]. Previously there was introduced public value
for LUA_REFNIL - luaL_nil_ref. This patch introduces an analog
for LUA_NOREF - luaL_no_ref.

Also this patch introduces several usages of this value - let's
make fiber code more clearer and start to intialize ref values
with LUA_NOREF value. Also it will be needed for future changes.

  [1] https://pgl.yoyo.org/luai/i/luaL_ref
---
 src/lib/core/fiber.c        | 3 +++
 src/lua/fiber.c             | 3 +--
 src/lua/utils.c             | 1 +
 test/unit/core_test_utils.c | 2 ++
 4 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c
index 588b78504..21afde209 100644
--- a/src/lib/core/fiber.c
+++ b/src/lib/core/fiber.c
@@ -44,6 +44,7 @@
 #include "errinj.h"
 
 extern void cord_on_yield(void);
+extern int luaL_no_ref;
 
 #if ENABLE_FIBER_TOP
 #include <x86intrin.h> /* __rdtscp() */
@@ -895,6 +896,7 @@ fiber_recycle(struct fiber *fiber)
 	fiber->f = NULL;
 	fiber->wait_pad = NULL;
 	memset(&fiber->storage, 0, sizeof(fiber->storage));
+	fiber->storage.lua.storage_ref = luaL_no_ref;
 	unregister_fid(fiber);
 	fiber->fid = 0;
 	region_free(&fiber->gc);
@@ -1236,6 +1238,7 @@ fiber_new_ex(const char *name, const struct fiber_attr *fiber_attr,
 			return NULL;
 		}
 		memset(fiber, 0, sizeof(struct fiber));
+		fiber->storage.lua.storage_ref = luaL_no_ref;
 
 		if (fiber_stack_create(fiber, &cord()->slabc,
 				       fiber_attr->stack_size)) {
diff --git a/src/lua/fiber.c b/src/lua/fiber.c
index 5575f2079..6457a1428 100644
--- a/src/lua/fiber.c
+++ b/src/lua/fiber.c
@@ -683,7 +683,6 @@ lbox_fiber_on_stop(struct trigger *trigger, void *event)
 {
 	struct fiber *f = (struct fiber *) event;
 	int storage_ref = f->storage.lua.storage_ref;
-	assert(storage_ref > 0);
 	luaL_unref(tarantool_L, LUA_REGISTRYINDEX, storage_ref);
 	f->storage.lua.storage_ref = LUA_NOREF;
 	trigger_clear(trigger);
@@ -696,7 +695,7 @@ lbox_fiber_storage(struct lua_State *L)
 {
 	struct fiber *f = lbox_checkfiber(L, 1);
 	int storage_ref = f->storage.lua.storage_ref;
-	if (storage_ref <= 0) {
+	if (storage_ref == LUA_NOREF) {
 		struct trigger *t = (struct trigger *)
 			malloc(sizeof(*t));
 		if (t == NULL) {
diff --git a/src/lua/utils.c b/src/lua/utils.c
index c71cd4857..c252d7cdf 100644
--- a/src/lua/utils.c
+++ b/src/lua/utils.c
@@ -40,6 +40,7 @@
 #include "uuid/tt_uuid.h"
 
 int luaL_nil_ref = LUA_REFNIL;
+int luaL_no_ref = LUA_NOREF;
 
 static int luaT_newthread_ref = LUA_NOREF;
 
diff --git a/test/unit/core_test_utils.c b/test/unit/core_test_utils.c
index 23452bbfd..9b1789e75 100644
--- a/test/unit/core_test_utils.c
+++ b/test/unit/core_test_utils.c
@@ -35,3 +35,5 @@
 void cord_on_yield(void)
 {
 }
+
+int luaL_no_ref = -2;
-- 
2.32.0



More information about the Tarantool-patches mailing list