LockFreeRingBuffer.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Created by Ben Allison on 11/10/07.
  3. * Copyright 2007-2011 Nullsoft, Inc. All rights reserved.
  4. *
  5. * Ring Buffer class
  6. * Thread safety:
  7. * This class can be used from exactly two simultaneous threads without locking
  8. * as long as one thread only writes and the other thread only reads
  9. * the writer thread may call empty(), avail(), size(), write(), fill(
  10. * the reader thread my call empty(), avail(), size(), read(), peek(), advance()
  11. *
  12. * two (or more) readers or two (or more) writers requires external locking
  13. *
  14. * Reset(), reserve(), clear(), LockBuffer(), UnlockBuffer() are not thread-safe
  15. */
  16. #pragma once
  17. #include <stddef.h>
  18. #include "foundation/types.h"
  19. class LockFreeRingBuffer
  20. {
  21. public:
  22. LockFreeRingBuffer();
  23. ~LockFreeRingBuffer();
  24. void Reset();
  25. int expand(size_t bytes); // like reserve, but only expands upward. non-destructive. returns an NError
  26. bool reserve(size_t bytes);
  27. bool empty() const;
  28. size_t avail() const; // how much available for writing
  29. size_t size() const; // how much available for reading
  30. void clear();
  31. size_t read(void *dest, size_t len); // returns amount actually read
  32. size_t advance(size_t len); // same as read() but doesn't write the data any where.
  33. size_t peek(void *dest, size_t len) const; // same as read() but doesn't advance the read pointer
  34. size_t write(const void *src, size_t len);
  35. size_t update(size_t len); // same as write() but doesn't write the data, usually used after a get_buffer call
  36. size_t at(size_t offset, void *dest, size_t len) const; // peeks() from offset. returns bytes read
  37. size_t write_position() const; // returns an integer representing a write position
  38. size_t read_position() const; // returns an integer representing the read position
  39. size_t advance_to(size_t position); // moves the read pointer to a position previously returned from write_position(). returns bytes advanced
  40. void get_write_buffer(size_t bytes, void **buffer, size_t *bytes_available); /* returns a pointer that you can write data to, you MUST call update() when you are done */
  41. void get_read_buffer(size_t bytes, const void **buffer, size_t *bytes_available); /* returns a pointer that you can read data from, call advance() when you are done */
  42. private:
  43. volatile size_t ringBufferUsed;
  44. size_t ringBufferSize;
  45. char *ringBuffer;
  46. char *ringWritePosition;
  47. char *ringReadPosition;
  48. };