diff options
author | tompng <tomoyapenguin@gmail.com> | 2020-10-07 17:51:19 +0900 |
---|---|---|
committer | Koichi Sasada <ko1@atdot.net> | 2020-10-12 14:20:58 +0900 |
commit | e8d03c9a2a58054221393e39f511fa76fe9b9770 (patch) | |
tree | fcfb5f4d60d91f49e5d1de13d18c2b8986f09605 | |
parent | c6652f223c6103a4d4d909d6b770cdfc5d140124 (diff) | |
download | ruby-e8d03c9a2a58054221393e39f511fa76fe9b9770.tar.gz |
change rb_ractor_queue to ring buffer
-rw-r--r-- | ractor.c | 20 | ||||
-rw-r--r-- | ractor.h | 1 |
2 files changed, 12 insertions, 9 deletions
@@ -159,8 +159,9 @@ static void ractor_queue_mark(struct rb_ractor_queue *rq) { for (int i=0; i<rq->cnt; i++) { - rb_gc_mark(rq->baskets[i].v); - rb_gc_mark(rq->baskets[i].sender); + int idx = (rq->start + i) % rq->size; + rb_gc_mark(rq->baskets[idx].v); + rb_gc_mark(rq->baskets[idx].sender); } } @@ -293,6 +294,7 @@ ractor_queue_setup(struct rb_ractor_queue *rq) { rq->size = 2; rq->cnt = 0; + rq->start = 0; rq->baskets = malloc(sizeof(struct rb_ractor_basket) * rq->size); } @@ -311,12 +313,9 @@ ractor_queue_deq(rb_ractor_t *r, struct rb_ractor_queue *rq, struct rb_ractor_ba RACTOR_LOCK(r); { if (!ractor_queue_empty_p(r, rq)) { - // TODO: use good Queue data structure - *basket = rq->baskets[0]; + *basket = rq->baskets[rq->start]; rq->cnt--; - for (int i=0; i<rq->cnt; i++) { - rq->baskets[i] = rq->baskets[i+1]; - } + rq->start = (rq->start + 1) % rq->size; b = true; } else { @@ -334,10 +333,13 @@ ractor_queue_enq(rb_ractor_t *r, struct rb_ractor_queue *rq, struct rb_ractor_ba ASSERT_ractor_locking(r); if (rq->size <= rq->cnt) { + rq->baskets = realloc(rq->baskets, sizeof(struct rb_ractor_basket) * rq->size * 2); + for (int i=rq->size - rq->start; i<rq->cnt; i++) { + rq->baskets[i + rq->start] = rq->baskets[i + rq->start - rq->size]; + } rq->size *= 2; - rq->baskets = realloc(rq->baskets, sizeof(struct rb_ractor_basket) * rq->size); } - rq->baskets[rq->cnt++] = *basket; + rq->baskets[(rq->start + rq->cnt++) % rq->size] = *basket; // fprintf(stderr, "%s %p->cnt:%d\n", __func__, rq, rq->cnt); } @@ -25,6 +25,7 @@ struct rb_ractor_basket { struct rb_ractor_queue { struct rb_ractor_basket *baskets; + int start; int cnt; int size; }; |