[Tarantool-patches] [PATCH v4 3/4] fiber: correctly initialize storage_ref value

olegrok at tarantool.org olegrok at tarantool.org
Wed Sep 8 21:20:01 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]. This patch introduces special value in fiber
module that is equal to LUA_REFNIL to be sure that this value will
be correct in future static assert is added.

Also this patch introduces several usages of this value - let's
make fiber code more clearer and start to initialize 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 | 2 ++
 src/lib/core/fiber.h | 2 ++
 src/lua/fiber.c      | 7 +++----
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c
index 588b78504..b7699372a 100644
--- a/src/lib/core/fiber.c
+++ b/src/lib/core/fiber.c
@@ -895,6 +895,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 = FIBER_LUA_NOREF;
 	unregister_fid(fiber);
 	fiber->fid = 0;
 	region_free(&fiber->gc);
@@ -1236,6 +1237,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 = FIBER_LUA_NOREF;
 
 		if (fiber_stack_create(fiber, &cord()->slabc,
 				       fiber_attr->stack_size)) {
diff --git a/src/lib/core/fiber.h b/src/lib/core/fiber.h
index 593847df7..9584b1611 100644
--- a/src/lib/core/fiber.h
+++ b/src/lib/core/fiber.h
@@ -45,6 +45,8 @@
 
 #include <coro/coro.h>
 
+#define FIBER_LUA_NOREF (-2)
+
 /*
  * Fiber top doesn't work on ARM processors at the moment,
  * because we haven't chosen an alternative to rdtsc.
diff --git a/src/lua/fiber.c b/src/lua/fiber.c
index 5575f2079..96aa73b19 100644
--- a/src/lua/fiber.c
+++ b/src/lua/fiber.c
@@ -38,7 +38,7 @@
 
 #include <lua.h>
 #include <lauxlib.h>
-#include <lualib.h>
+static_assert(FIBER_LUA_NOREF == LUA_NOREF, "FIBER_LUA_NOREF is ok");
 
 void
 luaL_testcancel(struct lua_State *L)
@@ -683,9 +683,8 @@ 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;
+	f->storage.lua.storage_ref = FIBER_LUA_NOREF;
 	trigger_clear(trigger);
 	free(trigger);
 	return 0;
@@ -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 == FIBER_LUA_NOREF) {
 		struct trigger *t = (struct trigger *)
 			malloc(sizeof(*t));
 		if (t == NULL) {
-- 
2.32.0



More information about the Tarantool-patches mailing list