Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 3 | // http://ceres-solver.org/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Author: vitus@google.com (Michael Vitus) |
| 30 | |
| 31 | #ifndef CERES_INTERNAL_CONCURRENT_QUEUE_H_ |
| 32 | #define CERES_INTERNAL_CONCURRENT_QUEUE_H_ |
| 33 | |
| 34 | #include <condition_variable> |
| 35 | #include <mutex> |
| 36 | #include <queue> |
| 37 | #include <thread> |
| 38 | |
| 39 | #include "glog/logging.h" |
| 40 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 41 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 42 | |
| 43 | // A thread-safe multi-producer, multi-consumer queue for queueing items that |
| 44 | // are typically handled asynchronously by multiple threads. The ConcurrentQueue |
| 45 | // has two states which only affect the Wait call: |
| 46 | // |
| 47 | // (1) Waiters have been enabled (enabled by default or calling |
| 48 | // EnableWaiters). The call to Wait will block until an item is available. |
| 49 | // Push and pop will operate as expected. |
| 50 | // |
| 51 | // (2) StopWaiters has been called. All threads blocked in a Wait() call will |
| 52 | // be woken up and pop any available items from the queue. All future Wait |
| 53 | // requests will either return an element from the queue or return |
| 54 | // immediately if no element is present. Push and pop will operate as |
| 55 | // expected. |
| 56 | // |
| 57 | // A common use case is using the concurrent queue as an interface for |
| 58 | // scheduling tasks for a set of thread workers: |
| 59 | // |
| 60 | // ConcurrentQueue<Task> task_queue; |
| 61 | // |
| 62 | // [Worker threads]: |
| 63 | // Task task; |
| 64 | // while(task_queue.Wait(&task)) { |
| 65 | // ... |
| 66 | // } |
| 67 | // |
| 68 | // [Producers]: |
| 69 | // task_queue.Push(...); |
| 70 | // .. |
| 71 | // task_queue.Push(...); |
| 72 | // ... |
| 73 | // // Signal worker threads to stop blocking on Wait and terminate. |
| 74 | // task_queue.StopWaiters(); |
| 75 | // |
| 76 | template <typename T> |
| 77 | class ConcurrentQueue { |
| 78 | public: |
| 79 | // Defaults the queue to blocking on Wait calls. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 80 | ConcurrentQueue() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | |
| 82 | // Atomically push an element onto the queue. If a thread was waiting for an |
| 83 | // element, wake it up. |
| 84 | void Push(const T& value) { |
| 85 | std::lock_guard<std::mutex> lock(mutex_); |
| 86 | queue_.push(value); |
| 87 | work_pending_condition_.notify_one(); |
| 88 | } |
| 89 | |
| 90 | // Atomically pop an element from the queue. If an element is present, return |
| 91 | // true. If the queue was empty, return false. |
| 92 | bool Pop(T* value) { |
| 93 | CHECK(value != nullptr); |
| 94 | |
| 95 | std::lock_guard<std::mutex> lock(mutex_); |
| 96 | return PopUnlocked(value); |
| 97 | } |
| 98 | |
| 99 | // Atomically pop an element from the queue. Blocks until one is available or |
| 100 | // StopWaiters is called. Returns true if an element was successfully popped |
| 101 | // from the queue, otherwise returns false. |
| 102 | bool Wait(T* value) { |
| 103 | CHECK(value != nullptr); |
| 104 | |
| 105 | std::unique_lock<std::mutex> lock(mutex_); |
| 106 | work_pending_condition_.wait(lock, |
| 107 | [&]() { return !(wait_ && queue_.empty()); }); |
| 108 | |
| 109 | return PopUnlocked(value); |
| 110 | } |
| 111 | |
| 112 | // Unblock all threads waiting to pop a value from the queue, and they will |
| 113 | // exit Wait() without getting a value. All future Wait requests will return |
| 114 | // immediately if no element is present until EnableWaiters is called. |
| 115 | void StopWaiters() { |
| 116 | std::lock_guard<std::mutex> lock(mutex_); |
| 117 | wait_ = false; |
| 118 | work_pending_condition_.notify_all(); |
| 119 | } |
| 120 | |
| 121 | // Enable threads to block on Wait calls. |
| 122 | void EnableWaiters() { |
| 123 | std::lock_guard<std::mutex> lock(mutex_); |
| 124 | wait_ = true; |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | // Pops an element from the queue. If an element is present, return |
| 129 | // true. If the queue was empty, return false. Not thread-safe. Must acquire |
| 130 | // the lock before calling. |
| 131 | bool PopUnlocked(T* value) { |
| 132 | if (queue_.empty()) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | *value = queue_.front(); |
| 137 | queue_.pop(); |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | // The mutex controls read and write access to the queue_ and stop_ |
| 143 | // variables. It is also used to block the calling thread until an element is |
| 144 | // available to pop from the queue. |
| 145 | std::mutex mutex_; |
| 146 | std::condition_variable work_pending_condition_; |
| 147 | |
| 148 | std::queue<T> queue_; |
| 149 | // If true, signals that callers of Wait will block waiting to pop an |
| 150 | // element off the queue. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 151 | bool wait_{true}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 154 | } // namespace ceres::internal |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 155 | |
| 156 | #endif // CERES_INTERNAL_CONCURRENT_QUEUE_H_ |