On 08/09/2010 12:00 AM, Angus Salkeld wrote:
This is to make it easier to to see if there is
a chunk available to be read.
Why not just retrieve the current value of the semaphore?
Signed-off-by: Angus Salkeld<asalkeld(a)redhat.com>
---
include/qb/qbrb.h | 7 +++++++
lib/ringbuffer.c | 18 +++++++++++++++++-
lib/ringbuffer_int.h | 2 +-
tests/check_rb.c | 1 +
4 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/include/qb/qbrb.h b/include/qb/qbrb.h
index a944e10..72c1513 100644
--- a/include/qb/qbrb.h
+++ b/include/qb/qbrb.h
@@ -224,6 +224,13 @@ ssize_t qb_rb_space_free(qb_ringbuffer_t * rb);
ssize_t qb_rb_space_used(qb_ringbuffer_t * rb);
/**
+ * The total number of chunks in the buffer.
+ *
+ * @param rb ringbuffer instance
+ */
+ssize_t qb_rb_chunks_used(qb_ringbuffer_t * rb);
+
+/**
* Write the contents of the Ring Buffer to file.
* @param fd open file to write the ringbuffer data to.
* @param rb ringbuffer instance
diff --git a/lib/ringbuffer.c b/lib/ringbuffer.c
index 0210a12..1ff2dd9 100644
--- a/lib/ringbuffer.c
+++ b/lib/ringbuffer.c
@@ -315,6 +315,22 @@ ssize_t qb_rb_space_used(qb_ringbuffer_t * rb)
return used;
}
+ssize_t qb_rb_chunks_used(qb_ringbuffer_t * rb)
+{
+ ssize_t count = -1;
+ if (rb->lock_fn(rb) == -1) {
+ return -1;
+ }
+ count = rb->shared_hdr->count;
+ if (rb->unlock_fn(rb) == -1) {
+ /* aarg stuck locked! */
+ qb_util_log(LOG_ERR, "failed to unlock ringbuffer lock %s",
+ strerror(errno));
+ return -1;
+ }
+ return count;
+}
+
void *qb_rb_chunk_alloc(qb_ringbuffer_t * rb, size_t len)
{
uint32_t write_pt;
@@ -462,7 +478,7 @@ ssize_t qb_rb_chunk_peek(qb_ringbuffer_t * rb, void **data_out,
int32_t timeout)
res = rb->sem_timedwait_fn(rb, timeout);
if (res == -1&& errno == ETIMEDOUT&& rb->shared_hdr->count>
0) {
qb_util_log(LOG_ERR,
- "sem timedout but count is %d",
+ "sem timedout but count is %zu",
rb->shared_hdr->count);
} else if (res == -1&& errno != EIDRM) {
if (errno != ETIMEDOUT) {
diff --git a/lib/ringbuffer_int.h b/lib/ringbuffer_int.h
index 68c3269..ab9da90 100644
--- a/lib/ringbuffer_int.h
+++ b/lib/ringbuffer_int.h
@@ -57,7 +57,7 @@ struct qb_ringbuffer_shared_s {
volatile uint32_t write_pt;
volatile uint32_t read_pt;
uint32_t size;
- uint32_t count;
+ size_t count;
char hdr_path[PATH_MAX];
char data_path[PATH_MAX];
int32_t ref_count;
diff --git a/tests/check_rb.c b/tests/check_rb.c
index e262876..7c5cd64 100644
--- a/tests/check_rb.c
+++ b/tests/check_rb.c
@@ -71,6 +71,7 @@ START_TEST(test_ring_buffer1)
for (i = 0; i< 15; i++) {
actual = qb_rb_chunk_read(rb, hdr, 512, 0);
if (actual == -1) {
+ ck_assert_int_eq(0, qb_rb_chunks_used(rb));
break;
}
str[actual - sizeof(qb_ipc_request_header_t)] = '\0';