Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2018 Google Inc. All rights reserved. |
| 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 | |
| 41 | namespace ceres { |
| 42 | namespace internal { |
| 43 | |
| 44 | // A thread-safe multi-producer, multi-consumer queue for queueing items that |
| 45 | // are typically handled asynchronously by multiple threads. The ConcurrentQueue |
| 46 | // has two states which only affect the Wait call: |
| 47 | // |
| 48 | // (1) Waiters have been enabled (enabled by default or calling |
| 49 | // EnableWaiters). The call to Wait will block until an item is available. |
| 50 | // Push and pop will operate as expected. |
| 51 | // |
| 52 | // (2) StopWaiters has been called. All threads blocked in a Wait() call will |
| 53 | // be woken up and pop any available items from the queue. All future Wait |
| 54 | // requests will either return an element from the queue or return |
| 55 | // immediately if no element is present. Push and pop will operate as |
| 56 | // expected. |
| 57 | // |
| 58 | // A common use case is using the concurrent queue as an interface for |
| 59 | // scheduling tasks for a set of thread workers: |
| 60 | // |
| 61 | // ConcurrentQueue<Task> task_queue; |
| 62 | // |
| 63 | // [Worker threads]: |
| 64 | // Task task; |
| 65 | // while(task_queue.Wait(&task)) { |
| 66 | // ... |
| 67 | // } |
| 68 | // |
| 69 | // [Producers]: |
| 70 | // task_queue.Push(...); |
| 71 | // .. |
| 72 | // task_queue.Push(...); |
| 73 | // ... |
| 74 | // // Signal worker threads to stop blocking on Wait and terminate. |
| 75 | // task_queue.StopWaiters(); |
| 76 | // |
| 77 | template <typename T> |
| 78 | class ConcurrentQueue { |
| 79 | public: |
| 80 | // Defaults the queue to blocking on Wait calls. |
| 81 | ConcurrentQueue() : wait_(true) {} |
| 82 | |
| 83 | // Atomically push an element onto the queue. If a thread was waiting for an |
| 84 | // element, wake it up. |
| 85 | void Push(const T& value) { |
| 86 | std::lock_guard<std::mutex> lock(mutex_); |
| 87 | queue_.push(value); |
| 88 | work_pending_condition_.notify_one(); |
| 89 | } |
| 90 | |
| 91 | // Atomically pop an element from the queue. If an element is present, return |
| 92 | // true. If the queue was empty, return false. |
| 93 | bool Pop(T* value) { |
| 94 | CHECK(value != nullptr); |
| 95 | |
| 96 | std::lock_guard<std::mutex> lock(mutex_); |
| 97 | return PopUnlocked(value); |
| 98 | } |
| 99 | |
| 100 | // Atomically pop an element from the queue. Blocks until one is available or |
| 101 | // StopWaiters is called. Returns true if an element was successfully popped |
| 102 | // from the queue, otherwise returns false. |
| 103 | bool Wait(T* value) { |
| 104 | CHECK(value != nullptr); |
| 105 | |
| 106 | std::unique_lock<std::mutex> lock(mutex_); |
| 107 | work_pending_condition_.wait(lock, |
| 108 | [&]() { return !(wait_ && queue_.empty()); }); |
| 109 | |
| 110 | return PopUnlocked(value); |
| 111 | } |
| 112 | |
| 113 | // Unblock all threads waiting to pop a value from the queue, and they will |
| 114 | // exit Wait() without getting a value. All future Wait requests will return |
| 115 | // immediately if no element is present until EnableWaiters is called. |
| 116 | void StopWaiters() { |
| 117 | std::lock_guard<std::mutex> lock(mutex_); |
| 118 | wait_ = false; |
| 119 | work_pending_condition_.notify_all(); |
| 120 | } |
| 121 | |
| 122 | // Enable threads to block on Wait calls. |
| 123 | void EnableWaiters() { |
| 124 | std::lock_guard<std::mutex> lock(mutex_); |
| 125 | wait_ = true; |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | // Pops an element from the queue. If an element is present, return |
| 130 | // true. If the queue was empty, return false. Not thread-safe. Must acquire |
| 131 | // the lock before calling. |
| 132 | bool PopUnlocked(T* value) { |
| 133 | if (queue_.empty()) { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | *value = queue_.front(); |
| 138 | queue_.pop(); |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | // The mutex controls read and write access to the queue_ and stop_ |
| 144 | // variables. It is also used to block the calling thread until an element is |
| 145 | // available to pop from the queue. |
| 146 | std::mutex mutex_; |
| 147 | std::condition_variable work_pending_condition_; |
| 148 | |
| 149 | std::queue<T> queue_; |
| 150 | // If true, signals that callers of Wait will block waiting to pop an |
| 151 | // element off the queue. |
| 152 | bool wait_; |
| 153 | }; |
| 154 | |
| 155 | |
| 156 | } // namespace internal |
| 157 | } // namespace ceres |
| 158 | |
| 159 | #endif // CERES_INTERNAL_CONCURRENT_QUEUE_H_ |