Skip to content

Commit 8cf4f37

Browse files
committed
thread_sync.c: declare queue_data_type as parent of szqueue_data_type.
Allows to remove some duplicated code like szqueue_length, etc.
1 parent bbc684d commit 8cf4f37

File tree

2 files changed

+32
-45
lines changed

2 files changed

+32
-45
lines changed

include/ruby/internal/core/rtypeddata.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,13 +615,24 @@ RBIMPL_ATTR_ARTIFICIAL()
615615
* directly.
616616
*/
617617
static inline void *
618-
rbimpl_check_typeddata(VALUE obj, const rb_data_type_t *type)
618+
rbimpl_check_typeddata(VALUE obj, const rb_data_type_t *expected_type)
619619
{
620-
if (RB_LIKELY(RB_TYPE_P(obj, T_DATA) && RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == type)) {
621-
return RTYPEDDATA_GET_DATA(obj);
620+
if (RB_LIKELY(RB_TYPE_P(obj, T_DATA) && RTYPEDDATA_P(obj))) {
621+
const rb_data_type_t *actual_type = RTYPEDDATA_TYPE(obj);
622+
void *data = RTYPEDDATA_GET_DATA(obj);
623+
if (RB_LIKELY(actual_type == expected_type)) {
624+
return data;
625+
}
626+
627+
while (actual_type) {
628+
actual_type = actual_type->parent;
629+
if (actual_type == expected_type) {
630+
return data;
631+
}
632+
}
622633
}
623634

624-
return rb_check_typeddata(obj, type);
635+
return rb_check_typeddata(obj, expected_type);
625636
}
626637

627638

thread_sync.c

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,14 @@ queue_memsize(const void *ptr)
728728
}
729729

730730
static const rb_data_type_t queue_data_type = {
731-
"queue",
732-
{queue_mark_and_move, RUBY_TYPED_DEFAULT_FREE, queue_memsize, queue_mark_and_move},
733-
0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
731+
.wrap_struct_name = "Thread::Queue",
732+
.function = {
733+
.dmark = queue_mark_and_move,
734+
.dfree = RUBY_TYPED_DEFAULT_FREE,
735+
.dsize = queue_memsize,
736+
.dcompact = queue_mark_and_move,
737+
},
738+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
734739
};
735740

736741
static VALUE
@@ -803,9 +808,15 @@ szqueue_memsize(const void *ptr)
803808
}
804809

805810
static const rb_data_type_t szqueue_data_type = {
806-
"sized_queue",
807-
{szqueue_mark_and_move, RUBY_TYPED_DEFAULT_FREE, szqueue_memsize, szqueue_mark_and_move},
808-
0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
811+
.wrap_struct_name = "Thread::SizedQueue",
812+
.function = {
813+
.dmark = szqueue_mark_and_move,
814+
.dfree = RUBY_TYPED_DEFAULT_FREE,
815+
.dsize = szqueue_memsize,
816+
.dcompact = szqueue_mark_and_move,
817+
},
818+
.parent = &queue_data_type,
819+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
809820
};
810821

811822
static VALUE
@@ -1382,23 +1393,6 @@ rb_szqueue_clear(VALUE self)
13821393
return self;
13831394
}
13841395

1385-
/*
1386-
* Document-method: Thread::SizedQueue#length
1387-
* call-seq:
1388-
* length
1389-
* size
1390-
*
1391-
* Returns the length of the queue.
1392-
*/
1393-
1394-
static VALUE
1395-
rb_szqueue_length(VALUE self)
1396-
{
1397-
struct rb_szqueue *sq = szqueue_ptr(self);
1398-
1399-
return LONG2NUM(queue_length(self, &sq->q));
1400-
}
1401-
14021396
/*
14031397
* Document-method: Thread::SizedQueue#num_waiting
14041398
*
@@ -1413,21 +1407,6 @@ rb_szqueue_num_waiting(VALUE self)
14131407
return INT2NUM(sq->q.num_waiting + sq->num_waiting_push);
14141408
}
14151409

1416-
/*
1417-
* Document-method: Thread::SizedQueue#empty?
1418-
* call-seq: empty?
1419-
*
1420-
* Returns +true+ if the queue is empty.
1421-
*/
1422-
1423-
static VALUE
1424-
rb_szqueue_empty_p(VALUE self)
1425-
{
1426-
struct rb_szqueue *sq = szqueue_ptr(self);
1427-
1428-
return RBOOL(queue_length(self, &sq->q) == 0);
1429-
}
1430-
14311410

14321411
/* ConditionalVariable */
14331412
struct rb_condvar {
@@ -1678,11 +1657,8 @@ Init_thread_sync(void)
16781657
rb_define_method(rb_cSizedQueue, "close", rb_szqueue_close, 0);
16791658
rb_define_method(rb_cSizedQueue, "max", rb_szqueue_max_get, 0);
16801659
rb_define_method(rb_cSizedQueue, "max=", rb_szqueue_max_set, 1);
1681-
rb_define_method(rb_cSizedQueue, "empty?", rb_szqueue_empty_p, 0);
16821660
rb_define_method(rb_cSizedQueue, "clear", rb_szqueue_clear, 0);
1683-
rb_define_method(rb_cSizedQueue, "length", rb_szqueue_length, 0);
16841661
rb_define_method(rb_cSizedQueue, "num_waiting", rb_szqueue_num_waiting, 0);
1685-
rb_define_alias(rb_cSizedQueue, "size", "length");
16861662

16871663
/* CVar */
16881664
DEFINE_CLASS(ConditionVariable, Object);

0 commit comments

Comments
 (0)