From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id B77CB6FC86; Mon, 22 Mar 2021 19:48:05 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B77CB6FC86 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616431685; bh=hQ3Uib9APZda8BbFg5WuZxBdoKYxSZ/bUt0QiQBIUho=; h=To:References:Date:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=JjdWynbV6JEf6d7Q+MeDj68sQVwVYApR7gLUUFxF4rU7jERyTWtQ6qZBuRNPDsxfe JZZoaZ+dX14mn7YofDO8PnoFz90HvaVoQuDlrGPIr8PsQ70MHwTUwXtE3/jgPganBJ fudjoWQrMAH8EX9dOuQWN7dpg7VZ1E3jJlLPeWG0= Received: from smtp17.mail.ru (smtp17.mail.ru [94.100.176.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id ED9AD6FC86 for ; Mon, 22 Mar 2021 19:48:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org ED9AD6FC86 Received: by smtp17.mail.ru with esmtpa (envelope-from ) id 1lONim-0006CN-3f; Mon, 22 Mar 2021 19:48:04 +0300 To: Vladislav Shpilevoy , tarantool-patches@dev.tarantool.org, gorcunov@gmail.com References: Message-ID: Date: Mon, 22 Mar 2021 19:48:03 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F179C48A9DDACBFB6F5347129BC2C9341C182A05F5380850407FBEB9AE4F01334E078E8DE6D850339B4A41B93061DD6121A4FDAD29B206382D X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE743AE26858062A689EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006371E8C7BD479B346ED8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75EC4A87B75C7B575C52A213A8AD6FED450A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE7328B01A8D746D8839FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C34964A708C60C975A117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE14079BD4B6F7A4D31EC0BEC64975D915A344093EC92FD9297F6718AA50765F7900637E52AE9B802B2390DA7F4EDE966BC389F395957E7521B51C24C7702A67D5C33162DBA43225CD8A89FCBE33A7817EDC1EF57739F23D657EF2BB5C8C57E37DE458B4C7702A67D5C3316FA3894348FB808DB48C21F01D89DB561574AF45C6390F7469DAA53EE0834AAEE X-C1DE0DAB: 0D63561A33F958A5E346C6ED6A425CADC2CF2E4DFC2A4A0BC3FBB0ED4E8953D3D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34103A2CD2CFC42C678354E7D3F2E4376E920E10367808235AD6EA8E2D57BE8A0833B44A56B103FF6D1D7E09C32AA3244CC24D87205CB159FA99CD603FEFA61AFC1DD47778AE04E04DFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UgJ/q7WNyuQLw== X-Mailru-Sender: 3B9A0136629DC9125D61937A2360A4466DF1A44E41B33A1BD68D64EA1A5247531068CCE1DAF2B76B424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH 06/16] cord_buf: introduce ownership management X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Serge Petrenko via Tarantool-patches Reply-To: Serge Petrenko Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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