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.
ck_pring
Samy Al Bahra 14 years ago
parent 85099afb85
commit 88ee328b78

@ -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)
{

@ -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;

@ -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;

Loading…
Cancel
Save