2#include "internal/gc.h"
3#include "internal/ractor_safe_set.h"
4#include "ruby_atomic.h"
8enum ractor_safe_set_special_values {
9 RACTOR_SAFE_TABLE_EMPTY,
10 RACTOR_SAFE_TABLE_DELETED,
11 RACTOR_SAFE_TABLE_MOVED,
12 RACTOR_SAFE_TABLE_SPECIAL_VALUE_COUNT
22 unsigned int capacity;
23 unsigned int deleted_entries;
29ractor_safe_set_free(
void *ptr)
36ractor_safe_set_size(
const void *ptr)
47 .dfree = ractor_safe_set_free,
48 .dsize = ractor_safe_set_size,
50 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
60 set->capacity = capacity;
73 RUBY_ASSERT((set->capacity & (set->capacity - 1)) == 0);
75 probe->mask = set->capacity - 1;
76 probe->idx = hash & probe->mask;
84 probe->idx = (probe->idx + probe->d) & probe->mask;
89ractor_safe_set_try_resize_without_locking(
VALUE old_set_obj,
VALUE *set_obj_ptr)
92 if (RUBY_ATOMIC_VALUE_LOAD(*set_obj_ptr) != old_set_obj) {
100 int expected_size =
RUBY_ATOMIC_LOAD(old_set->size) - old_set->deleted_entries;
103 int old_capacity = old_set->capacity;
104 int new_capacity = old_capacity * 2;
105 if (new_capacity > expected_size * 8) {
106 new_capacity = old_capacity / 2;
108 else if (new_capacity > expected_size * 4) {
109 new_capacity = old_capacity;
113 VALUE new_set_obj = rb_ractor_safe_set_new(old_set->funcs, new_capacity);
116 for (
int i = 0; i < old_capacity; i++) {
121 if (key < RACTOR_SAFE_TABLE_SPECIAL_VALUE_COUNT)
continue;
122 if (rb_objspace_garbage_object_p(key))
continue;
124 VALUE hash = RUBY_ATOMIC_VALUE_LOAD(entry->hash);
128 hash = old_set->funcs->hash(key);
134 int idx = ractor_safe_set_probe_start(&probe, new_set, hash);
139 if (entry->key == RACTOR_SAFE_TABLE_EMPTY) {
142 RUBY_ASSERT(new_set->size < new_set->capacity / 2);
150 RUBY_ASSERT(entry->key >= RACTOR_SAFE_TABLE_SPECIAL_VALUE_COUNT);
153 idx = ractor_safe_set_probe_next(&probe);
163ractor_safe_set_try_resize(
VALUE old_set_obj,
VALUE *set_obj_ptr)
166 ractor_safe_set_try_resize_without_locking(old_set_obj, set_obj_ptr);
171rb_ractor_safe_set_find_or_insert(
VALUE *set_obj_ptr,
VALUE key,
void *data)
173 RUBY_ASSERT(key >= RACTOR_SAFE_TABLE_SPECIAL_VALUE_COUNT);
175 bool inserting =
false;
179 set_obj = RUBY_ATOMIC_VALUE_LOAD(*set_obj_ptr);
184 VALUE hash = set->funcs->hash(key);
185 int idx = ractor_safe_set_probe_start(&probe, set, hash);
189 VALUE curr_key = RUBY_ATOMIC_VALUE_LOAD(entry->key);
192 case RACTOR_SAFE_TABLE_EMPTY: {
195 key = set->funcs->create(key, data);
202 if (UNLIKELY(prev_size > set->capacity / 2)) {
203 ractor_safe_set_try_resize(set_obj, set_obj_ptr);
209 if (curr_key == RACTOR_SAFE_TABLE_EMPTY) {
223 case RACTOR_SAFE_TABLE_DELETED:
225 case RACTOR_SAFE_TABLE_MOVED:
231 VALUE curr_hash = RUBY_ATOMIC_VALUE_LOAD(entry->hash);
232 if ((curr_hash == hash || curr_hash == 0) && set->funcs->cmp(key, curr_key)) {
234 if (UNLIKELY(rb_objspace_garbage_object_p(curr_key))) {
251 idx = ractor_safe_set_probe_next(&probe);
256rb_ractor_safe_set_delete_by_identity(
VALUE set_obj,
VALUE key)
263 VALUE hash = set->funcs->hash(key);
266 int idx = ractor_safe_set_probe_start(&probe, set, hash);
270 VALUE curr_key = RUBY_ATOMIC_VALUE_LOAD(entry->key);
273 case RACTOR_SAFE_TABLE_EMPTY:
276 case RACTOR_SAFE_TABLE_DELETED:
278 case RACTOR_SAFE_TABLE_MOVED:
279 rb_bug(
"rb_ractor_safe_set_delete_by_identity: moved entry");
282 if (key == curr_key) {
283 entry->key = RACTOR_SAFE_TABLE_DELETED;
284 set->deleted_entries++;
290 idx = ractor_safe_set_probe_next(&probe);
295rb_ractor_safe_set_foreach_with_replace(
VALUE set_obj,
int (*callback)(
VALUE *key,
void *data),
void *data)
302 for (
unsigned int i = 0; i < set->capacity; i++) {
303 VALUE key = set->entries[i].key;
306 case RACTOR_SAFE_TABLE_EMPTY:
307 case RACTOR_SAFE_TABLE_DELETED:
309 case RACTOR_SAFE_TABLE_MOVED:
310 rb_bug(
"rb_ractor_safe_set_foreach_with_replace: moved entry");
313 int ret = callback(&set->entries[i].key, data);
318 set->entries[i].key = RACTOR_SAFE_TABLE_DELETED;
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ATOMIC_VALUE_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are VALUE.
#define RUBY_ATOMIC_VALUE_SET(var, val)
Identical to RUBY_ATOMIC_SET, except it expects its arguments are VALUE.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define RUBY_ATOMIC_VALUE_EXCHANGE(var, val)
Identical to RUBY_ATOMIC_EXCHANGE, except it expects its arguments are VALUE.
#define RUBY_ATOMIC_DEC(var)
Atomically decrements the value pointed by var.
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
#define xfree
Old name of ruby_xfree.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
uintptr_t VALUE
Type that represents a Ruby object.