From 88ee328b7840e66dc9293cf4a0d34172748851e0 Mon Sep 17 00:00:00 2001 From: Samy Al Bahra Date: Sun, 19 Jun 2011 11:38:42 -0400 Subject: [PATCH] ck_ring: Add CK_RING_SIZE/CAPACITY and ck_ring_size/capacity. ck_ring_size will return the current size of the ring while ck_ring_capacity will return the capacity of the specified ring. --- include/ck_ring.h | 36 +++++++++++++++++++ regressions/ck_ring/validate/ck_ring_spsc.c | 20 ++++++++++- .../ck_ring/validate/ck_ring_spsc_template.c | 16 ++++++++- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/include/ck_ring.h b/include/ck_ring.h index 808240c..317bf76 100644 --- a/include/ck_ring.h +++ b/include/ck_ring.h @@ -60,6 +60,21 @@ ck_pr_store_ptr(&ring->ring, buffer); \ return; \ } \ + CK_CC_INLINE static unsigned int \ + ck_ring_size_##name(struct ck_ring_##name *ring) \ + { \ + unsigned int c, p; \ + \ + c = ck_pr_load_uint(&ring->c_head); \ + p = ck_pr_load_uint(&ring->p_tail); \ + return (p - c); \ + } \ + CK_CC_INLINE static unsigned int \ + ck_ring_capacity_##name(struct ck_ring_##name *ring) \ + { \ + \ + return ck_pr_load_uint(&ring->size); \ + } \ CK_CC_INLINE static bool \ ck_ring_enqueue_spsc_##name(struct ck_ring_##name *ring, \ struct type *entry) \ @@ -103,6 +118,10 @@ struct ck_ring_##name #define CK_RING_INIT(name, object, buffer, size) \ ck_ring_init_##name(object, buffer, size) +#define CK_RING_SIZE(name, object) \ + ck_ring_size_##name(object) +#define CK_RING_CAPACITY(name, object) \ + ck_ring_capacity_##name(object) #define CK_RING_ENQUEUE_SPSC(name, object, value) \ ck_ring_enqueue_spsc_##name(object, value) #define CK_RING_DEQUEUE_SPSC(name, object, value) \ @@ -122,6 +141,23 @@ typedef struct ck_ring ck_ring_t; /* * Single consumer and single producer ring buffer enqueue (producer). */ +CK_CC_INLINE static unsigned int +ck_ring_size(struct ck_ring *ring) +{ + unsigned int c, p; + + c = ck_pr_load_uint(&ring->c_head); + p = ck_pr_load_uint(&ring->p_tail); + return (p - c); +} + +CK_CC_INLINE static unsigned int +ck_ring_capacity(struct ck_ring *ring) +{ + + return ck_pr_load_uint(&ring->size); +} + CK_CC_INLINE static bool ck_ring_enqueue_spsc(struct ck_ring *ring, void *entry) { diff --git a/regressions/ck_ring/validate/ck_ring_spsc.c b/regressions/ck_ring/validate/ck_ring_spsc.c index 09e5937..d6e786c 100644 --- a/regressions/ck_ring/validate/ck_ring_spsc.c +++ b/regressions/ck_ring/validate/ck_ring_spsc.c @@ -72,11 +72,29 @@ test(void *c) entries = malloc(sizeof(struct entry) * size); assert(entries != NULL); + if (ck_ring_size(ring) != 0) { + fprintf(stderr, "Less entries than expected: %u > 0\n", + ck_ring_size(ring)); + exit(EXIT_FAILURE); + } + for (i = 0; i < size; i++) { entries[i].value = i; entries[i].tid = 0; - r = ck_ring_enqueue_spsc(ring + context->tid, entries + i); + r = ck_ring_enqueue_spsc(ring, entries + i); + } + + if (ck_ring_size(ring) != (unsigned int)size) { + fprintf(stderr, "Less entries than expected: %u < %d\n", + ck_ring_size(ring), size); + exit(EXIT_FAILURE); + } + + if (ck_ring_capacity(ring) != ck_ring_size(ring) + 1) { + fprintf(stderr, "Capacity less than expected: %u < %u\n", + ck_ring_size(ring), ck_ring_capacity(ring)); + exit(EXIT_FAILURE); } barrier = 1; diff --git a/regressions/ck_ring/validate/ck_ring_spsc_template.c b/regressions/ck_ring/validate/ck_ring_spsc_template.c index a989f92..f512879 100644 --- a/regressions/ck_ring/validate/ck_ring_spsc_template.c +++ b/regressions/ck_ring/validate/ck_ring_spsc_template.c @@ -73,11 +73,25 @@ test(void *c) entries = malloc(sizeof(struct entry) * size); assert(entries != NULL); + if (CK_RING_SIZE(entry_ring, ring) != 0) { + fprintf(stderr, "Ring should be empty: %u\n", + CK_RING_SIZE(entry_ring, ring)); + exit(EXIT_FAILURE); + } + for (i = 0; i < size; i++) { entries[i].value = i; entries[i].tid = 0; - r = CK_RING_ENQUEUE_SPSC(entry_ring, ring + context->tid, entries + i); + r = CK_RING_ENQUEUE_SPSC(entry_ring, ring, entries + i); + } + + if (CK_RING_SIZE(entry_ring, ring) != + CK_RING_CAPACITY(entry_ring, ring) - 1) { + fprintf(stderr, "Ring has incorrect size or capacity: %u != %u\n", + CK_RING_SIZE(entry_ring, ring), + CK_RING_CAPACITY(entry_ring, ring)); + exit(EXIT_FAILURE); } barrier = 1;