[Tarantool-patches] [PATCH 06/16] cord_buf: introduce ownership management

Serge Petrenko sergepetrenko at tarantool.org
Mon Mar 22 19:48:03 MSK 2021



20.03.2021 03:42, Vladislav Shpilevoy пишет:
> The global ibuf used for hot Lua and Lua C code didn't have
> ownership management. As a result, it could be reused in some
> unexpected ways during Lua GC via __gc handlers, even if it was
> currently in use in some code below the stack.
>
> The patch makes cord_ibuf_take() steal the global buffer from its
> global stash, and assign to the current fiber. cord_ibuf_put()
> puts it back to the stash, and detaches from the fiber. If yield
> happens before cord_ibuf_put(), the buffer is detached
> automatically.
>
> Fiber attach/detach is done via on_yield/on_stop triggers. The
> buffer is not supposed to survive a yield, so this allows to
> free/put the buffer back to the stash even if the owner didn't do
> that. For instance, if a Lua exception was raised before
> cord_ibuf_put() was called.
>
> This makes cord buffer being safe to use in any yield-free code,
> even if Lua GC might be started. And in non-Lua code as well.
>
> Part of #5632
> ---
>   src/lib/core/cord_buf.c      | 150 +++++++++++++++++++++++++++++++----
>   src/lib/core/cord_buf.h      |   6 +-
>   test/app-tap/buffer.test.lua |  59 ++++++++++++++
>   3 files changed, 199 insertions(+), 16 deletions(-)
>   create mode 100755 test/app-tap/buffer.test.lua
>
> diff --git a/src/lib/core/cord_buf.c b/src/lib/core/cord_buf.c
> index cac508c3d..9450d75bc 100644
> --- a/src/lib/core/cord_buf.c
> +++ b/src/lib/core/cord_buf.c
> @@ -5,6 +5,7 @@
>    */
>   #include "cord_buf.h"
>   #include "fiber.h"
> +#include "trigger.h"
>   
>   #include "small/ibuf.h"
>   
> @@ -13,35 +14,154 @@ enum {
>   	CORD_IBUF_START_CAPACITY = 16384,
>   };
>   
> -static struct ibuf *cord_buf_global = NULL;
> +/** Global buffer with automatic collection on fiber yield. */
> +struct cord_buf {
> +	/** Base buffer. */
> +	struct ibuf base;
> +	/**
> +	 * Triggers on fiber stop/yield when the buffer is either destroyed or
> +	 * cached to the global stash for later reuse.
> +	 */
> +	struct trigger on_stop;
> +	struct trigger on_yield;
> +#if !NDEBUG

Shouldn't it be `#if !defined(NDEBUG)`? I'm not sure, just asking.


-- 
Serge Petrenko



More information about the Tarantool-patches mailing list