Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiPseudoStack.h 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. MultiPseudoStack.h - Multiple-Writer Lock Free Datastructure
  4. Copyright (C) 2016 Mark McCurry
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. */
  10. #pragma once
  11. #include <atomic>
  12. #include <cassert>
  13. //XXX rename this thing
  14. typedef struct QueueListItem qli_t;
  15. struct QueueListItem
  16. {
  17. QueueListItem(void);
  18. char *memory;
  19. uint32_t size;
  20. };
  21. //Many reader many writer
  22. class LockFreeQueue
  23. {
  24. qli_t *const data;
  25. const int elms;
  26. std::atomic<uint32_t> *tag;
  27. std::atomic<int32_t> next_r;
  28. std::atomic<int32_t> next_w;
  29. std::atomic<int32_t> avail;
  30. public:
  31. LockFreeQueue(qli_t *data_, int n);
  32. ~LockFreeQueue(void);
  33. qli_t *read(void);
  34. void write(qli_t *Q);
  35. };
  36. /*
  37. * Many reader Many writer capiable queue
  38. * - lock free
  39. * - allocation free (post initialization)
  40. */
  41. class MultiQueue
  42. {
  43. qli_t *pool;
  44. LockFreeQueue m_free;
  45. LockFreeQueue m_msgs;
  46. public:
  47. MultiQueue(void);
  48. ~MultiQueue(void);
  49. void dump(void);
  50. qli_t *alloc(void) { return m_free.read(); }
  51. void free(qli_t *q) { m_free.write(q); }
  52. void write(qli_t *q) { m_msgs.write(q); }
  53. qli_t *read(void) { return m_msgs.read(); }
  54. };