< Summary

Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 64
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

C:\Users\Kremenchuk\actions-runner\_work\Holoflow\Holoflow\src\holoflow_event\include\holoflow_event\bounded_mpmc.hh

#LineLine coverage
 1// Copyright 2025 Digital Holography Foundation
 2//
 3// Licensed under the Apache License, Version 2.0 (the "License");
 4// you may not use this file except in compliance with the License.
 5// You may obtain a copy of the License at
 6//
 7//     http://www.apache.org/licenses/LICENSE-2.0
 8//
 9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15#pragma once
 16
 17#include <deque>
 18#include <mutex>
 19#include <optional>
 20
 21namespace holoflow_event {
 22
 23template <typename T> class BoundedMPMC {
 24public:
 125  explicit BoundedMPMC(size_t capacity) : capacity_(capacity) {}
 26
 127  [[nodiscard]] bool try_push(T &&v) {
 128    std::scoped_lock lock(mutex_);
 129    if (queue_.size() >= capacity_) {
 130      return false;
 31    }
 132    queue_.emplace_back(std::move(v));
 133    return true;
 134  }
 35
 136  [[nodiscard]] std::optional<T> try_pop() {
 137    std::scoped_lock lock(mutex_);
 138    if (queue_.empty()) {
 139      return std::nullopt;
 40    }
 141    T v = std::move(queue_.front());
 142    queue_.pop_front();
 143    return v;
 144  }
 45
 46  [[nodiscard]] size_t size() const {
 47    std::scoped_lock lock(mutex_);
 48    return queue_.size();
 49  }
 50
 51  [[nodiscard]] bool empty() const {
 52    std::scoped_lock lock(mutex_);
 53    return queue_.empty();
 54  }
 55
 56  [[nodiscard]] size_t capacity() const { return capacity_; }
 57
 58private:
 59  size_t             capacity_;
 60  mutable std::mutex mutex_;
 61  std::deque<T>      queue_;
 62};
 63
 64} // namespace holoflow_event

Methods/Properties