--- linuxsampler/trunk/src/common/RingBuffer.h 2005/02/09 01:22:18 361 +++ linuxsampler/trunk/src/common/RingBuffer.h 2008/11/02 12:05:00 1790 @@ -3,6 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005 - 2008 Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -23,17 +24,51 @@ #ifndef RINGBUFFER_H #define RINGBUFFER_H -#define DEFAULT_WRAP_ELEMENTS 1024 +#define DEFAULT_WRAP_ELEMENTS 0 #include -#include "atomic.h" +#include "lsatomic.h" -template +using LinuxSampler::atomic; +using LinuxSampler::memory_order_relaxed; +using LinuxSampler::memory_order_acquire; +using LinuxSampler::memory_order_release; + + +/** @brief Real-time safe and type safe RingBuffer implementation. + * + * This constant size buffer can be used to send data from exactly one + * sender / writing thread to exactly one receiver / reading thread. It is + * real-time safe due to the fact that data is only allocated when this + * RingBuffer is created and no system level mechanisms are used for + * ensuring thread safety of this class. + * + * Important: There are two distinct behaviors of this RingBuffer + * which has to be given as template argument @c T_DEEP_COPY, which is a + * boolean flag: + * + * - @c true: The RingBuffer will copy elements of type @c T by using type + * @c T's assignment operator. This behavior is mandatory for all data + * structures (classes) which additionally allocate memory on the heap. + * Type @c T's needs to have an assignment operator implementation though, + * otherwise this will cause a compilation error. This behavior is more + * safe, but usually slower (except for very small buffer sizes, where it + * might be even faster). + * - @c false: The RingBuffer will copy elements of type @c T by flatly + * copying their structural data ( i.e. with @c memcpy() ) in one piece. + * This will only work if class @c T (and all of its subelements) does not + * allocate any additional data on the heap by itself. So use this option + * with great care, because otherwise it will result in very ugly behavior + * and crashes! For larger buffer sizes, this behavior will most probably + * be faster. + */ +template class RingBuffer { public: - RingBuffer (int sz, int wrap_elements = DEFAULT_WRAP_ELEMENTS) { + RingBuffer (int sz, int wrap_elements = DEFAULT_WRAP_ELEMENTS) : + write_ptr(0), read_ptr(0) { int power_of_two; this->wrap_elements = wrap_elements; @@ -45,8 +80,6 @@ size = 1<= r) { - memset(get_buffer_begin(), 0, r - 1); + memset(get_buffer_begin(), 0, sizeof(T)*(r - 1)); } // set the wrap space elements to null - if (wrap_elements) memset(&buf[size], 0, wrap_elements); + if (wrap_elements) memset(&buf[size], 0, sizeof(T)*wrap_elements); } __inline int read (T *dest, int cnt); @@ -80,7 +117,7 @@ __inline T *get_buffer_begin(); __inline T *get_read_ptr(void) { - return(&buf[atomic_read(&read_ptr)]); + return(&buf[read_ptr.load(memory_order_relaxed)]); } /** @@ -88,7 +125,7 @@ * advanced by \a offset elements. */ /*inline T* get_read_ptr(int offset) { - int r = atomic_read(&read_ptr); + int r = read_ptr.load(memory_order_relaxed); r += offset; r &= size_mask; return &buf[r]; @@ -96,14 +133,14 @@ __inline T *get_write_ptr(); __inline void increment_read_ptr(int cnt) { - atomic_set(&read_ptr , (atomic_read(&read_ptr) + cnt) & size_mask); + read_ptr.store((read_ptr.load(memory_order_relaxed) + cnt) & size_mask, memory_order_release); } __inline void set_read_ptr(int val) { - atomic_set(&read_ptr , val); + read_ptr.store(val, memory_order_release); } __inline void increment_write_ptr(int cnt) { - atomic_set(&write_ptr, (atomic_read(&write_ptr) + cnt) & size_mask); + write_ptr.store((write_ptr.load(memory_order_relaxed) + cnt) & size_mask, memory_order_release); } /* this function increments the write_ptr by cnt, if the buffer wraps then @@ -118,14 +155,14 @@ and the write ptr incremented accordingly. */ __inline void increment_write_ptr_with_wrap(int cnt) { - int w=atomic_read(&write_ptr); + int w = write_ptr.load(memory_order_relaxed); w += cnt; if(w >= size) { w -= size; - memcpy(&buf[0], &buf[size], w*sizeof(T)); + copy(&buf[0], &buf[size], w); //printf("DEBUG !!!! increment_write_ptr_with_wrap: buffer wrapped, elements wrapped = %d (wrap_elements %d)\n",w,wrap_elements); } - atomic_set(&write_ptr, w); + write_ptr.store(w, memory_order_release); } /* this function returns the available write space in the buffer @@ -145,8 +182,8 @@ __inline int write_space_to_end_with_wrap() { int w, r; - w = atomic_read(&write_ptr); - r = atomic_read(&read_ptr); + w = write_ptr.load(memory_order_relaxed); + r = read_ptr.load(memory_order_acquire); //printf("write_space_to_end: w=%d r=%d\n",w,r); if(r > w) { //printf("DEBUG: write_space_to_end_with_wrap: r>w r=%d w=%d val=%d\n",r,w,r - w - 1); @@ -184,7 +221,7 @@ */ __inline int adjust_write_space_to_avoid_boundary(int cnt, int capped_cnt) { int w; - w = atomic_read(&write_ptr); + w = write_ptr.load(memory_order_relaxed); if((w+capped_cnt) >= size && (w+capped_cnt) < (size+wrap_elements)) { //printf("adjust_write_space_to_avoid_boundary returning cnt = %d\n",cnt); return(cnt); @@ -196,8 +233,8 @@ __inline int write_space_to_end() { int w, r; - w = atomic_read(&write_ptr); - r = atomic_read(&read_ptr); + w = write_ptr.load(memory_order_relaxed); + r = read_ptr.load(memory_order_acquire); //printf("write_space_to_end: w=%d r=%d\n",w,r); if(r > w) return(r - w - 1); if(r) return(size - w); @@ -207,22 +244,22 @@ __inline int read_space_to_end() { int w, r; - w = atomic_read(&write_ptr); - r = atomic_read(&read_ptr); + w = write_ptr.load(memory_order_acquire); + r = read_ptr.load(memory_order_relaxed); if(w >= r) return(w - r); return(size - r); } __inline void init() { - atomic_set(&write_ptr, 0); - atomic_set(&read_ptr, 0); + write_ptr.store(0, memory_order_relaxed); + read_ptr.store(0, memory_order_relaxed); // wrap=0; } int write_space () { int w, r; - w = atomic_read(&write_ptr); - r = atomic_read(&read_ptr); + w = write_ptr.load(memory_order_relaxed); + r = read_ptr.load(memory_order_acquire); if (w > r) { return ((r - w + size) & size_mask) - 1; @@ -236,8 +273,8 @@ int read_space () { int w, r; - w = atomic_read(&write_ptr); - r = atomic_read(&read_ptr); + w = write_ptr.load(memory_order_acquire); + r = read_ptr.load(memory_order_relaxed); if (w >= r) { return w - r; @@ -254,12 +291,12 @@ * allows to read from a RingBuffer without being forced to free read * data while reading / positioning. */ - template + template class _NonVolatileReader { public: int read_space() { int r = read_ptr; - int w = atomic_read(&pBuf->write_ptr); + int w = pBuf->write_ptr.load(memory_order_acquire); return (w >= r) ? w - r : (w - r + pBuf->size) & pBuf->size_mask; } @@ -268,8 +305,8 @@ * read position by one. */ inline void operator--() { - if (read_ptr == atomic_read(&pBuf->read_ptr)) return; //TODO: or should we react oh this case (e.g. force segfault), as this is a very odd case? - --read_ptr & pBuf->size_mask; + if (read_ptr == pBuf->read_ptr.load(memory_order_relaxed)) return; //TODO: or should we react oh this case (e.g. force segfault), as this is a very odd case? + read_ptr = (read_ptr-1) & pBuf->size_mask; } /** @@ -339,11 +376,11 @@ n2 = 0; } - memcpy(dest, &pBuf->buf[priv_read_ptr], n1 * sizeof(T)); + copy(dest, &pBuf->buf[priv_read_ptr], n1); priv_read_ptr = (priv_read_ptr + n1) & pBuf->size_mask; if (n2) { - memcpy(dest+n1, pBuf->buf, n2 * sizeof(T)); + copy(dest+n1, pBuf->buf, n2); priv_read_ptr = n2; } @@ -359,49 +396,54 @@ * @see RingBuffer::increment_read_ptr() */ void free() { - atomic_set(&pBuf->read_ptr, read_ptr); + pBuf->read_ptr.store(read_ptr, memory_order_release); } protected: - _NonVolatileReader(RingBuffer* pBuf) { + _NonVolatileReader(RingBuffer* pBuf) { this->pBuf = pBuf; - this->read_ptr = atomic_read(&pBuf->read_ptr); + this->read_ptr = pBuf->read_ptr.load(memory_order_relaxed); } - RingBuffer* pBuf; + RingBuffer* pBuf; int read_ptr; - friend class RingBuffer; + friend class RingBuffer; }; - typedef _NonVolatileReader NonVolatileReader; + typedef _NonVolatileReader NonVolatileReader; NonVolatileReader get_non_volatile_reader() { return NonVolatileReader(this); } protected: T *buf; - atomic_t write_ptr; - atomic_t read_ptr; + atomic write_ptr; + atomic read_ptr; int size_mask; - friend class _NonVolatileReader; + /** + * Copies \a n amount of elements from the buffer given by + * \a pSrc to the buffer given by \a pDst. + */ + inline static void copy(T* pDst, T* pSrc, int n); + + friend class _NonVolatileReader; }; -template T * -RingBuffer::get_write_ptr (void) { - return(&buf[atomic_read(&write_ptr)]); +template +T* RingBuffer::get_write_ptr (void) { + return(&buf[write_ptr.load(memory_order_relaxed)]); } -template T * -RingBuffer::get_buffer_begin (void) { +template +T* RingBuffer::get_buffer_begin (void) { return(buf); } -template int -RingBuffer::read (T *dest, int cnt) - +template +int RingBuffer::read(T* dest, int cnt) { int free_cnt; int cnt2; @@ -409,7 +451,7 @@ int n1, n2; int priv_read_ptr; - priv_read_ptr=atomic_read(&read_ptr); + priv_read_ptr = read_ptr.load(memory_order_relaxed); if ((free_cnt = read_space ()) == 0) { return 0; @@ -427,21 +469,20 @@ n2 = 0; } - memcpy (dest, &buf[priv_read_ptr], n1 * sizeof (T)); + copy(dest, &buf[priv_read_ptr], n1); priv_read_ptr = (priv_read_ptr + n1) & size_mask; if (n2) { - memcpy (dest+n1, buf, n2 * sizeof (T)); + copy(dest+n1, buf, n2); priv_read_ptr = n2; } - atomic_set(&read_ptr, priv_read_ptr); + read_ptr.store(priv_read_ptr, memory_order_release); return to_read; } -template int -RingBuffer::write (T *src, int cnt) - +template +int RingBuffer::write(T* src, int cnt) { int free_cnt; int cnt2; @@ -449,7 +490,7 @@ int n1, n2; int priv_write_ptr; - priv_write_ptr=atomic_read(&write_ptr); + priv_write_ptr = write_ptr.load(memory_order_relaxed); if ((free_cnt = write_space ()) == 0) { return 0; @@ -467,16 +508,24 @@ n2 = 0; } - memcpy (&buf[priv_write_ptr], src, n1 * sizeof (T)); + copy(&buf[priv_write_ptr], src, n1); priv_write_ptr = (priv_write_ptr + n1) & size_mask; if (n2) { - memcpy (buf, src+n1, n2 * sizeof (T)); + copy(buf, src+n1, n2); priv_write_ptr = n2; } - atomic_set(&write_ptr, priv_write_ptr); + write_ptr.store(priv_write_ptr, memory_order_release); return to_write; } +template +void RingBuffer::copy(T* pDst, T* pSrc, int n) { + if (T_DEEP_COPY) { // deep copy - won't work for data structures without assignment operator implementation + for (int i = 0; i < n; i++) pDst[i] = pSrc[i]; + } else { // flat copy - won't work for complex data structures ! + memcpy(pDst, pSrc, n * sizeof(T)); + } +} #endif /* RINGBUFFER_H */