LCOV - code coverage report
Current view: top level - port - cpl_threadsafe_queue.hpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 29 29 100.0 %
Date: 2024-04-28 23:18:46 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  CPL
       4             :  * Purpose:  Implementation of a thread-safe queue
       5             :  * Author:   Even Rouault <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2022, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * Permission is hereby granted, free of charge, to any person obtaining a
      11             :  * copy of this software and associated documentation files (the "Software"),
      12             :  * to deal in the Software without restriction, including without limitation
      13             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      14             :  * and/or sell copies of the Software, and to permit persons to whom the
      15             :  * Software is furnished to do so, subject to the following conditions:
      16             :  *
      17             :  * The above copyright notice and this permission notice shall be included
      18             :  * in all copies or substantial portions of the Software.
      19             :  *
      20             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      21             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      22             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      23             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      24             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      25             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      26             :  * DEALINGS IN THE SOFTWARE.
      27             :  ****************************************************************************/
      28             : 
      29             : #ifndef CPL_THREADSAFE_QUEUE_INCLUDED
      30             : #define CPL_THREADSAFE_QUEUE_INCLUDED
      31             : 
      32             : #include <condition_variable>
      33             : #include <mutex>
      34             : #include <queue>
      35             : 
      36             : namespace cpl
      37             : {
      38             : template <class T> class ThreadSafeQueue
      39             : {
      40             :   private:
      41             :     mutable std::mutex m_mutex{};
      42             :     std::condition_variable m_cv{};
      43             :     std::queue<T> m_queue{};
      44             : 
      45             :   public:
      46        3432 :     ThreadSafeQueue() = default;
      47             : 
      48           5 :     void clear()
      49             :     {
      50          10 :         std::lock_guard<std::mutex> lock(m_mutex);
      51         191 :         while (!m_queue.empty())
      52         186 :             m_queue.pop();
      53           5 :     }
      54             : 
      55          17 :     bool empty() const
      56             :     {
      57          34 :         std::lock_guard<std::mutex> lock(m_mutex);
      58          34 :         return m_queue.empty();
      59             :     }
      60             : 
      61      227123 :     size_t size() const
      62             :     {
      63      454246 :         std::lock_guard<std::mutex> lock(m_mutex);
      64      454246 :         return m_queue.size();
      65             :     }
      66             : 
      67          12 :     void push(const T &value)
      68             :     {
      69          24 :         std::lock_guard<std::mutex> lock(m_mutex);
      70          12 :         m_queue.push(value);
      71          12 :         m_cv.notify_one();
      72          12 :     }
      73             : 
      74        1089 :     void push(T &&value)
      75             :     {
      76        2178 :         std::lock_guard<std::mutex> lock(m_mutex);
      77        1089 :         m_queue.push(std::move(value));
      78        1089 :         m_cv.notify_one();
      79        1089 :     }
      80             : 
      81         885 :     T get_and_pop_front()
      82             :     {
      83        1769 :         std::unique_lock<std::mutex> lock(m_mutex);
      84        1260 :         while (m_queue.empty())
      85             :         {
      86         375 :             m_cv.wait(lock);
      87             :         }
      88         885 :         T val = m_queue.front();
      89         885 :         m_queue.pop();
      90        1770 :         return val;
      91             :     }
      92             : };
      93             : 
      94             : }  // namespace cpl
      95             : 
      96             : #endif  // CPL_THREADSAFE_QUEUE_INCLUDED

Generated by: LCOV version 1.14