X-Git-Url: https://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=rculfhash.c;h=600feecfa8f4f8230eede1683cf79cff14a0333f;hp=d89c0cb6635ead96de12a9bdbb82d7c2b8e0a0b2;hb=5f74363e048fb82d7ad97a780eadbeb063840787;hpb=ab65b890d706250367f5b66d86fd0527b57f7b85 diff --git a/rculfhash.c b/rculfhash.c index d89c0cb..600feec 100644 --- a/rculfhash.c +++ b/rculfhash.c @@ -1796,13 +1796,9 @@ void do_resize_cb(struct rcu_head *head) } static -void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth) +void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht) { struct rcu_resize_work *work; - unsigned long target_size = size << growth; - - if (resize_target_grow(ht, target_size) >= target_size) - return; /* Store resize_target before read resize_initiated */ cmm_smp_mb(); @@ -1820,27 +1816,47 @@ void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int grow } } +static +void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth) +{ + unsigned long target_size = size << growth; + + if (resize_target_grow(ht, target_size) >= target_size) + return; + + __cds_lfht_resize_lazy_launch(ht); +} + +/* + * We favor grow operations over shrink. A shrink operation never occurs + * if a grow operation is queued for lazy execution. A grow operation + * cancels any pending shrink lazy execution. + */ static void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size, unsigned long count) { - struct rcu_resize_work *work; - if (!(ht->flags & CDS_LFHT_AUTO_RESIZE)) return; - resize_target_update_count(ht, count); - /* Store resize_target before read resize_initiated */ - cmm_smp_mb(); - if (!CMM_LOAD_SHARED(ht->t.resize_initiated)) { - uatomic_inc(&ht->in_progress_resize); - cmm_smp_mb(); /* increment resize count before load destroy */ - if (CMM_LOAD_SHARED(ht->in_progress_destroy)) { - uatomic_dec(&ht->in_progress_resize); + count = max(count, ht->min_alloc_size); + if (count == size) + return; /* Already the right size, no resize needed */ + if (count > size) { /* lazy grow */ + if (resize_target_grow(ht, count) >= count) return; + } else { /* lazy shrink */ + for (;;) { + unsigned long s; + + s = uatomic_cmpxchg(&ht->t.resize_target, size, count); + if (s == size) + break; /* no resize needed */ + if (s > size) + return; /* growing is/(was just) in progress */ + if (s <= count) + return; /* some other thread do shrink */ + size = s; } - work = malloc(sizeof(*work)); - work->ht = ht; - ht->cds_lfht_call_rcu(&work->head, do_resize_cb); - CMM_STORE_SHARED(ht->t.resize_initiated, 1); } + __cds_lfht_resize_lazy_launch(ht); }