context_spinlock_queue.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright Oliver Kowalke 2015.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. #ifndef BOOST_FIBERS_DETAIL_SPINLOCK_QUEUE_H
  7. #define BOOST_FIBERS_DETAIL_SPINLOCK_QUEUE_H
  8. #include <cstddef>
  9. #include <cstring>
  10. #include <mutex>
  11. #include <boost/config.hpp>
  12. #include <boost/fiber/context.hpp>
  13. #include <boost/fiber/detail/config.hpp>
  14. #include <boost/fiber/detail/spinlock.hpp>
  15. #ifdef BOOST_HAS_ABI_HEADERS
  16. # include BOOST_ABI_PREFIX
  17. #endif
  18. namespace boost {
  19. namespace fibers {
  20. namespace detail {
  21. class context_spinlock_queue {
  22. private:
  23. typedef context * slot_type;
  24. mutable spinlock splk_{};
  25. std::size_t pidx_{ 0 };
  26. std::size_t cidx_{ 0 };
  27. std::size_t capacity_;
  28. slot_type * slots_;
  29. void resize_() {
  30. slot_type * old_slots = slots_;
  31. slots_ = new slot_type[2*capacity_];
  32. std::size_t offset = capacity_ - cidx_;
  33. std::memcpy( slots_, old_slots + cidx_, offset * sizeof( slot_type) );
  34. if ( 0 < cidx_) {
  35. std::memcpy( slots_ + offset, old_slots, pidx_ * sizeof( slot_type) );
  36. }
  37. cidx_ = 0;
  38. pidx_ = capacity_ - 1;
  39. capacity_ *= 2;
  40. delete [] old_slots;
  41. }
  42. bool is_full_() const noexcept {
  43. return cidx_ == ((pidx_ + 1) % capacity_);
  44. }
  45. bool is_empty_() const noexcept {
  46. return cidx_ == pidx_;
  47. }
  48. public:
  49. context_spinlock_queue( std::size_t capacity = 4096) :
  50. capacity_{ capacity } {
  51. slots_ = new slot_type[capacity_];
  52. }
  53. ~context_spinlock_queue() {
  54. delete [] slots_;
  55. }
  56. context_spinlock_queue( context_spinlock_queue const&) = delete;
  57. context_spinlock_queue & operator=( context_spinlock_queue const&) = delete;
  58. bool empty() const noexcept {
  59. spinlock_lock lk{ splk_ };
  60. return is_empty_();
  61. }
  62. void push( context * c) {
  63. spinlock_lock lk{ splk_ };
  64. if ( is_full_() ) {
  65. resize_();
  66. }
  67. slots_[pidx_] = c;
  68. pidx_ = (pidx_ + 1) % capacity_;
  69. }
  70. context * pop() {
  71. spinlock_lock lk{ splk_ };
  72. context * c = nullptr;
  73. if ( ! is_empty_() ) {
  74. c = slots_[cidx_];
  75. cidx_ = (cidx_ + 1) % capacity_;
  76. }
  77. return c;
  78. }
  79. context * steal() {
  80. spinlock_lock lk{ splk_ };
  81. context * c = nullptr;
  82. if ( ! is_empty_() ) {
  83. c = slots_[cidx_];
  84. if ( c->is_context( type::pinned_context) ) {
  85. return nullptr;
  86. }
  87. cidx_ = (cidx_ + 1) % capacity_;
  88. }
  89. return c;
  90. }
  91. };
  92. }}}
  93. #ifdef BOOST_HAS_ABI_HEADERS
  94. # include BOOST_ABI_SUFFIX
  95. #endif
  96. #endif // BOOST_FIBERS_DETAIL_SPINLOCK_QUEUE_H