ruby-changes:72485
From: Nobuyoshi <ko1@a...>
Date: Sun, 10 Jul 2022 16:38:40 +0900 (JST)
Subject: [ruby-changes:72485] 233054a609 (master): Cycle `obj_info_buffers_index` atomically
https://git.ruby-lang.org/ruby.git/commit/?id=233054a609 From 233054a609a36cc441d26f9064bc3d16ff83b686 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Thu, 7 Jul 2022 00:28:45 +0900 Subject: Cycle `obj_info_buffers_index` atomically --- gc.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/gc.c b/gc.c index 80e7fb596e..f487cb88ed 100644 --- a/gc.c +++ b/gc.c @@ -13942,19 +13942,26 @@ rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj) https://github.com/ruby/ruby/blob/trunk/gc.c#L13942 #if RGENGC_OBJ_INFO #define OBJ_INFO_BUFFERS_NUM 10 #define OBJ_INFO_BUFFERS_SIZE 0x100 -static int obj_info_buffers_index = 0; +static rb_atomic_t obj_info_buffers_index = 0; static char obj_info_buffers[OBJ_INFO_BUFFERS_NUM][OBJ_INFO_BUFFERS_SIZE]; -static const char * -obj_info(VALUE obj) +static char * +obj_info_next_buffer(void) { - const int index = obj_info_buffers_index++; - char *const buff = &obj_info_buffers[index][0]; + const rb_atomic_t index = RUBY_ATOMIC_FETCH_ADD(obj_info_buffers_index, 1); - if (obj_info_buffers_index >= OBJ_INFO_BUFFERS_NUM) { - obj_info_buffers_index = 0; + rb_atomic_t next_index = (index + 1); + if (UNLIKELY(next_index >= OBJ_INFO_BUFFERS_NUM)) { + rb_atomic_t reset_index = next_index % OBJ_INFO_BUFFERS_NUM; + RUBY_ATOMIC_CAS(obj_info_buffers_index, next_index, reset_index); } + return obj_info_buffers[index % OBJ_INFO_BUFFERS_NUM]; +} +static const char * +obj_info(VALUE obj) +{ + char *const buff = obj_info_next_buffer(); return rb_raw_obj_info(buff, OBJ_INFO_BUFFERS_SIZE, obj); } #else -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/