mpsc_intrusive_unordered.h 868 B

1234567891011121314151617181920212223242526272829303132333435
  1. #pragma once
  2. /*
  3. Simple almost-wait-free unordered queue for low contention operations.
  4. It's wait-free for producers.
  5. Hanging producer can hide some items from consumer.
  6. */
  7. #include <util/system/types.h>
  8. namespace NThreading {
  9. struct TIntrusiveNode {
  10. TIntrusiveNode* Next;
  11. };
  12. class TMPSCIntrusiveUnordered {
  13. public:
  14. static constexpr ui32 NUMBER_OF_TRIES_FOR_CAS = 3;
  15. void Push(TIntrusiveNode* node) noexcept;
  16. TIntrusiveNode* PopMany() noexcept;
  17. TIntrusiveNode* Pop() noexcept;
  18. void Push(void* node) noexcept {
  19. Push(reinterpret_cast<TIntrusiveNode*>(node));
  20. }
  21. private:
  22. TIntrusiveNode* HeadForCaS = nullptr;
  23. TIntrusiveNode* HeadForSwap = nullptr;
  24. TIntrusiveNode* NotReadyChain = nullptr;
  25. TIntrusiveNode* PopOneQueue = nullptr;
  26. };
  27. }