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.

67 lines
1.5KB

  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. namespace zyncarla {
  14. //XXX rename this thing
  15. typedef struct QueueListItem qli_t;
  16. struct QueueListItem
  17. {
  18. QueueListItem(void);
  19. char *memory;
  20. uint32_t size;
  21. };
  22. //Many reader many writer
  23. class LockFreeQueue
  24. {
  25. qli_t *const data;
  26. const int elms;
  27. std::atomic<uint32_t> *tag;
  28. std::atomic<int32_t> next_r;
  29. std::atomic<int32_t> next_w;
  30. std::atomic<int32_t> avail;
  31. public:
  32. LockFreeQueue(qli_t *data_, int n);
  33. ~LockFreeQueue(void);
  34. qli_t *read(void);
  35. void write(qli_t *Q);
  36. };
  37. /*
  38. * Many reader Many writer capiable queue
  39. * - lock free
  40. * - allocation free (post initialization)
  41. */
  42. class MultiQueue
  43. {
  44. qli_t *pool;
  45. LockFreeQueue m_free;
  46. LockFreeQueue m_msgs;
  47. public:
  48. MultiQueue(void);
  49. ~MultiQueue(void);
  50. void dump(void);
  51. qli_t *alloc(void) { return m_free.read(); }
  52. void free(qli_t *q) { m_free.write(q); }
  53. void write(qli_t *q) { m_msgs.write(q); }
  54. qli_t *read(void) { return m_msgs.read(); }
  55. };
  56. }