changeset 2151:9a9f406374c6

- Update ring buffer code to newer version
author Ralf Ertzinger <ralf@skytale.net>
date Sun, 04 Nov 2007 14:04:37 +0100
parents 04421592e6a3
children 8a5231ff9c4f
files src/neon/rb.c src/neon/rb.h
diffstat 2 files changed, 93 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/neon/rb.c	Sat Nov 03 01:23:56 2007 +0200
+++ b/src/neon/rb.c	Sun Nov 04 14:04:37 2007 +0100
@@ -93,7 +93,7 @@
 
     _ENTER;
 
-    pthread_mutex_lock(&rb->lock);
+    _RB_LOCK(rb->lock);
 
     rb->wp = rb->buf;
     rb->rp = rb->buf;
@@ -101,7 +101,7 @@
     rb->used = 0;
     rb->end = rb->buf+(rb->size-1);
 
-    pthread_mutex_unlock(&rb->lock);
+    _RB_UNLOCK(rb->lock);
 
     _LEAVE;
 }
@@ -120,9 +120,52 @@
         _LEAVE -1;
     }
 
-    if (0 != pthread_mutex_init(&rb->lock, NULL)) {
+    if (NULL == (rb->buf = malloc(size))) {
+        _LEAVE -1;
+    }
+    rb->size = size;
+
+    #ifdef _RB_USE_GLIB
+    if (NULL == (rb->lock = g_mutex_new())) {
+        _LEAVE -1;
+    }
+    #else
+    if (NULL == (rb->lock = malloc(sizeof(pthread_mutex_t)))) {
+        _LEAVE -1;
+    }
+
+    if (0 != pthread_mutex_init(rb->lock, NULL)) {
+        free(rb->lock);
         _LEAVE -1;
     }
+    #endif
+    rb->_free_lock = 1;
+
+    reset_rb(rb);
+
+    ASSERT_RB(rb);
+
+    _LEAVE 0;
+}
+
+/* 
+ * Initialize a ringbuffer structure (including
+ * memory allocation.
+ * The mutex to be used is passed in the function call.
+ * The mutex must not be held while calling this function.
+ *
+ * Return -1 on error
+ */
+int init_rb_with_lock(struct ringbuf* rb, unsigned int size, rb_mutex_t* lock) {
+
+    _ENTER;
+
+    if (0 == size) {
+        _LEAVE -1;
+    }
+
+    rb->lock = lock;
+    rb->_free_lock = 0;
 
     if (NULL == (rb->buf = malloc(size))) {
         _LEAVE -1;
@@ -146,7 +189,7 @@
 
     _ENTER;
 
-    pthread_mutex_lock(&rb->lock);
+    _RB_LOCK(rb->lock);
 
     ASSERT_RB(rb);
 
@@ -186,7 +229,7 @@
 
 out:
     ASSERT_RB(rb);
-    pthread_mutex_unlock(&rb->lock);
+    _RB_UNLOCK(rb->lock);
 
     _LEAVE ret;
 }
@@ -201,9 +244,9 @@
 
     _ENTER;
 
-    pthread_mutex_lock(&rb->lock);
+    _RB_LOCK(rb->lock);
     ret = read_rb_locked(rb, buf, size);
-    pthread_mutex_unlock(&rb->lock);
+    _RB_UNLOCK(rb->lock);
 
     _LEAVE ret;
 }
@@ -228,7 +271,7 @@
 
     if (rb->rp < rb->wp) {
         /*
-        Read pointer is behind write pointer, all the data is available in one cunk
+        Read pointer is behind write pointer, all the data is available in one chunk
         */
         memcpy(buf, rb->rp, size);
         rb->rp += size;
@@ -271,13 +314,24 @@
 
     _ENTER;
 
-    pthread_mutex_lock(&rb->lock);
-    f = rb->free;
-    pthread_mutex_unlock(&rb->lock);
+    _RB_LOCK(rb->lock);
+    f = free_rb_locked(rb);
+    _RB_UNLOCK(rb->lock);
 
     _LEAVE f;
 }
 
+/*
+ * Return the amount of free space currently in the rb.
+ * Assume the rb lock is already being held.
+ */
+unsigned int free_rb_locked(struct ringbuf* rb) {
+
+    _ENTER;
+
+    _LEAVE rb->free;
+}
+
 
 /*
  * Return the amount of used space currently in the rb
@@ -288,9 +342,9 @@
 
     _ENTER;
 
-    pthread_mutex_lock(&rb->lock);
+    _RB_LOCK(rb->lock);
     u = rb->used;
-    pthread_mutex_unlock(&rb->lock);
+    _RB_UNLOCK(rb->lock);
 
     _LEAVE u;
 }
@@ -302,9 +356,15 @@
 void destroy_rb(struct ringbuf* rb) {
 
     _ENTER;
-    pthread_mutex_lock(&rb->lock);
     free(rb->buf);
-    pthread_mutex_unlock(&rb->lock);
+    if (rb->_free_lock) {
+#ifdef _RB_USE_GLIB
+        g_mutex_free(rb->lock);
+#else
+        pthread_mutex_destroy(rb->lock);
+        free(rb->lock);
+#endif
+    }
 
     _LEAVE;
 }
--- a/src/neon/rb.h	Sat Nov 03 01:23:56 2007 +0200
+++ b/src/neon/rb.h	Sun Nov 04 14:04:37 2007 +0100
@@ -17,7 +17,21 @@
 #ifndef _RB_H
 #define _RB_H
 
+#ifdef _RB_USE_GLIB
+#include <glib.h>
+
+typedef GMutex rb_mutex_t;
+#define _RB_LOCK(L) g_mutex_lock(L)
+#define _RB_UNLOCK(L) g_mutex_unlock(L)
+
+#else
 #include <pthread.h>
+
+typedef pthread_mutex_t rb_mutex_t;
+#define _RB_LOCK(L) pthread_mutex_lock(L)
+#define _RB_UNLOCK(L) pthread_mutex_unlock(L)
+#endif
+
 #include <stdlib.h>
 
 #ifdef RB_DEBUG
@@ -27,7 +41,8 @@
 #endif
 
 struct ringbuf {
-    pthread_mutex_t lock;
+    rb_mutex_t* lock;
+    char _free_lock;
     char* buf;
     char* end;
     char* wp;
@@ -38,11 +53,13 @@
 };
 
 int init_rb(struct ringbuf* rb, unsigned int size);
+int init_rb_with_lock(struct ringbuf* rb, unsigned int size, rb_mutex_t* lock);
 int write_rb(struct ringbuf* rb, void* buf, unsigned int size);
 int read_rb(struct ringbuf* rb, void* buf, unsigned int size);
 int read_rb_locked(struct ringbuf* rb, void* buf, unsigned int size);
 void reset_rb(struct ringbuf* rb);
 unsigned int free_rb(struct ringbuf* rb);
+unsigned int free_rb_locked(struct ringbuf* rb);
 unsigned int used_rb(struct ringbuf* rb);
 void destroy_rb(struct ringbuf* rb);