nifi-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #746: MINIFICPP-1185 - Remove moodycamel::concurrentqueue from threadpool
Date Mon, 30 Mar 2020 09:45:59 GMT
szaszm commented on a change in pull request #746: MINIFICPP-1185 - Remove moodycamel::concurrentqueue
from threadpool
URL: https://github.com/apache/nifi-minifi-cpp/pull/746#discussion_r400043152
 
 

 ##########
 File path: libminifi/include/utils/MinifiConcurrentQueue.h
 ##########
 @@ -0,0 +1,160 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef LIBMINIFI_INCLUDE_CONCURRENT_QUEUE_H
+#define LIBMINIFI_INCLUDE_CONCURRENT_QUEUE_H
+
+#include <chrono>
+#include <deque>
+#include <mutex>
+#include <condition_variable>
+#include <stdexcept>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template <typename T>
+class ConcurrentQueue {
+ public:    
+  ConcurrentQueue() = default;
+  virtual ~ConcurrentQueue() = default;
+
+  ConcurrentQueue(const ConcurrentQueue& other) = delete;
+  ConcurrentQueue& operator=(const ConcurrentQueue& other) = delete;
+  ConcurrentQueue(ConcurrentQueue&& other)
+    : ConcurrentQueue(std::move(other), std::lock_guard<std::mutex>(other.mutex_))
{}
+
+  ConcurrentQueue& operator=(ConcurrentQueue&& other) {
+    if (this != &other) {
+      std::lock(mtx_, other.mtx_);
+      std::lock_guard<std::mutex> lk1(mtx_, std::adopt_lock);
+      std::lock_guard<std::mutex> lk2(other.mtx_, std::adopt_lock);
+      queue_.swap(other.queue_);
+    }
+    return *this;
+  }
+
+  bool tryDequeue(T& out) {
+    std::unique_lock<std::mutex> lck(mtx_);
+    return tryDequeue(lck, out);
+  }
+
+  bool empty() const {
+    std::lock_guard<std::mutex> guard(mtx_);
+    return queue_.empty();
+  }
+
+  size_t size() const {
+    std::lock_guard<std::mutex> guard(mtx_);
+    return queue_.size();
+  }
+
+  void clear() {
+    std::lock_guard<std::mutex> guard(mtx_);
+    queue_.clear();
+  }
+
+  template <typename... Args>
+  void enqueue(Args&&... args) {
+    std::lock_guard<std::mutex> guard(mtx_);
+    queue_.emplace_back(std::forward<Args>(args)...);
+  }
+
+ private:
+   ConcurrentQueue(ConcurrentQueue&& other, std::lock_guard<std::mutex>&)
+    : queue_( std::move(other.queue_) ) {}
+
+ protected:
+  bool tryDequeue(std::unique_lock<std::mutex>& lck, T& out) {
+    if (!lck.owns_lock()) {
+      throw std::logic_error("Caller of protected ConcurrentQueue::tryDequeue should own
the lock!");
+    }
+    if (queue_.empty()) {
+      return false;
+    }
+    out = std::move(queue_.front());
+    queue_.pop_front();
+    return true;
+  }
+  std::deque<T> queue_;
+  mutable std::mutex mtx_;
 
 Review comment:
   It's fine and expected that we use it in libminifi. What I'd like is that we only support
dependency on public members for libminifi API users and treat `protected`/`private` as "internal".
The language doesn't provide tools for that (like package-private in Java), so that's an API
documentation issue. We could also use pimpl, but that would mean a lot of boilerplate, an
extra level of indirection and no inlining, so I'm against that.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message