Coverage Report

Created: 2025-11-15 08:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/port/cpl_threadsafe_queue.hpp
Line
Count
Source
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
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#ifndef CPL_THREADSAFE_QUEUE_INCLUDED
14
#define CPL_THREADSAFE_QUEUE_INCLUDED
15
16
#include <condition_variable>
17
#include <mutex>
18
#include <queue>
19
20
namespace cpl
21
{
22
template <class T> class ThreadSafeQueue
23
{
24
  private:
25
    mutable std::mutex m_mutex{};
26
    std::condition_variable m_cv{};
27
    std::queue<T> m_queue{};
28
29
  public:
30
7.39k
    ThreadSafeQueue() = default;
31
32
    void clear()
33
0
    {
34
0
        std::lock_guard<std::mutex> lock(m_mutex);
35
0
        while (!m_queue.empty())
36
0
            m_queue.pop();
37
0
    }
38
39
    bool empty() const
40
0
    {
41
0
        std::lock_guard<std::mutex> lock(m_mutex);
42
0
        return m_queue.empty();
43
0
    }
44
45
    size_t size() const
46
75
    {
47
75
        std::lock_guard<std::mutex> lock(m_mutex);
48
75
        return m_queue.size();
49
75
    }
50
51
    void push(const T &value)
52
0
    {
53
0
        std::lock_guard<std::mutex> lock(m_mutex);
54
0
        m_queue.push(value);
55
0
        m_cv.notify_one();
56
0
    }
57
58
    void push(T &&value)
59
0
    {
60
0
        std::lock_guard<std::mutex> lock(m_mutex);
61
0
        m_queue.push(std::move(value));
62
0
        m_cv.notify_one();
63
0
    }
64
65
    T get_and_pop_front()
66
0
    {
67
0
        std::unique_lock<std::mutex> lock(m_mutex);
68
0
        while (m_queue.empty())
69
0
        {
70
0
            m_cv.wait(lock);
71
0
        }
72
0
        T val = m_queue.front();
73
0
        m_queue.pop();
74
0
        return val;
75
0
    }
76
};
77
78
}  // namespace cpl
79
80
#endif  // CPL_THREADSAFE_QUEUE_INCLUDED