You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

blocking_queue.h 3.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_BLOCKING_QUEUE_H_
  17. #define MINDSPORE_BLOCKING_QUEUE_H_
  18. #include <stdint.h>
  19. #include <condition_variable>
  20. #include <list>
  21. #include <mutex>
  22. #include <utility>
  23. static const int kDefaultMaxQueueSize = 2048;
  24. namespace mindspore {
  25. namespace profiler {
  26. namespace ascend {
  27. template <typename T>
  28. class BlockingQueue {
  29. public:
  30. explicit BlockingQueue(uint32_t max_size = kDefaultMaxQueueSize) : max_size_(max_size), is_stoped_(false) {}
  31. ~BlockingQueue() {}
  32. bool Pop(T *item) {
  33. std::unique_lock<std::mutex> lock(mutex_);
  34. while (queue_.empty() && !is_stoped_) {
  35. empty_cond_.wait(lock);
  36. }
  37. if (is_stoped_) {
  38. return false;
  39. }
  40. *item = std::move(queue_.front());
  41. queue_.pop_front();
  42. full_cond_.notify_one();
  43. return true;
  44. }
  45. bool Push(const T &item, bool is_wait = true) {
  46. std::unique_lock<std::mutex> lock(mutex_);
  47. while (queue_.size() >= max_size_ && !is_stoped_) {
  48. if (!is_wait) {
  49. return false;
  50. }
  51. full_cond_.wait(lock);
  52. }
  53. if (is_stoped_) {
  54. return false;
  55. }
  56. queue_.push_back(item);
  57. empty_cond_.notify_one();
  58. return true;
  59. }
  60. bool Push(T &&item, bool is_wait = true) {
  61. std::unique_lock<std::mutex> lock(mutex_);
  62. while (queue_.size() >= max_size_ && !is_stoped_) {
  63. if (!is_wait) {
  64. return false;
  65. }
  66. full_cond_.wait(lock);
  67. }
  68. if (is_stoped_) {
  69. return false;
  70. }
  71. queue_.emplace_back(std::move(item));
  72. empty_cond_.notify_one();
  73. return true;
  74. }
  75. void Stop() {
  76. {
  77. std::unique_lock<std::mutex> lock(mutex_);
  78. is_stoped_ = true;
  79. }
  80. full_cond_.notify_all();
  81. empty_cond_.notify_all();
  82. }
  83. void Restart() {
  84. std::unique_lock<std::mutex> lock(mutex_);
  85. is_stoped_ = false;
  86. }
  87. // if the queue is stoped ,need call this function to release the unprocessed items
  88. std::list<T> GetRemainItems() {
  89. std::unique_lock<std::mutex> lock(mutex_);
  90. if (!is_stoped_) {
  91. return std::list<T>();
  92. }
  93. return queue_;
  94. }
  95. bool IsFull() {
  96. std::unique_lock<std::mutex> lock(mutex_);
  97. return queue_.size() >= max_size_;
  98. }
  99. void Clear() {
  100. std::unique_lock<std::mutex> lock(mutex_);
  101. queue_.clear();
  102. }
  103. private:
  104. std::list<T> queue_;
  105. std::mutex mutex_;
  106. std::condition_variable empty_cond_;
  107. std::condition_variable full_cond_;
  108. uint32_t max_size_;
  109. bool is_stoped_;
  110. };
  111. } // namespace ascend
  112. } // namespace profiler
  113. } // namespace mindspore
  114. #endif // MINDSPORE_BLOCKING_QUEUE_H_