Tarantool development patches archive
 help / color / mirror / Atom feed
From: Oleg Babin via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org, imun@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v4 3/4] fiber: correctly initialize storage_ref value
Date: Wed,  8 Sep 2021 21:20:01 +0300	[thread overview]
Message-ID: <450a1c00942e91454f91848291f30f7d2fbb8f8f.1631124536.git.babinoleg@mail.ru> (raw)
In-Reply-To: <cover.1631124536.git.babinoleg@mail.ru>

From: Oleg Babin <babinoleg@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


  parent reply	other threads:[~2021-09-08 18:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-08 18:19 [Tarantool-patches] [PATCH v4 0/4] fiber: keep reference to userdata if fiber created once Oleg Babin via Tarantool-patches
2021-09-08 18:19 ` [Tarantool-patches] [PATCH v4 1/4] fiber: rename ref to storage_ref Oleg Babin via Tarantool-patches
2021-09-08 18:20 ` [Tarantool-patches] [PATCH v4 2/4] fiber: pass struct fiber into lbox_pushfiber instead of id Oleg Babin via Tarantool-patches
2021-09-08 18:20 ` Oleg Babin via Tarantool-patches [this message]
2021-09-08 18:20 ` [Tarantool-patches] [PATCH v4 4/4] fiber: keep reference to userdata if fiber created once Oleg Babin via Tarantool-patches
2021-09-08 20:45 ` [Tarantool-patches] [PATCH v4 0/4] " Vladislav Shpilevoy via Tarantool-patches
2021-09-09 10:21 ` Vladimir Davydov via Tarantool-patches
2021-09-09 10:45   ` Oleg Babin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=450a1c00942e91454f91848291f30f7d2fbb8f8f.1631124536.git.babinoleg@mail.ru \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imun@tarantool.org \
    --cc=olegrok@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v4 3/4] fiber: correctly initialize storage_ref value' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox