File size: 1,818 Bytes
be94e5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2024 Intel Corporation
#ifndef OPENCV_GAPI_THREAD_POOL_HPP
#define OPENCV_GAPI_THREAD_POOL_HPP
#include <functional>
#include <vector>
#include <thread>
#include <mutex>
#include <atomic>
#include <condition_variable>
#include <opencv2/gapi/own/exports.hpp> // GAPI_EXPORTS
#if defined(HAVE_TBB)
# include <tbb/concurrent_queue.h> // FIXME: drop it from here!
template<typename T> using QueueClass = tbb::concurrent_bounded_queue<T>;
#else
# include "executor/conc_queue.hpp"
template<typename T> using QueueClass = cv::gapi::own::concurrent_bounded_queue<T>;
#endif // TBB
namespace cv {
namespace gapi {
namespace own {
// NB: Only for tests
class GAPI_EXPORTS Latch {
public:
explicit Latch(const uint64_t expected);
Latch(const Latch&) = delete;
Latch& operator=(const Latch&) = delete;
void count_down();
void wait();
private:
uint64_t m_expected;
std::mutex m_mutex;
std::condition_variable m_all_done;
};
// NB: Only for tests
class GAPI_EXPORTS ThreadPool {
public:
using Task = std::function<void()>;
explicit ThreadPool(const uint32_t num_workers);
ThreadPool(const ThreadPool&) = delete;
ThreadPool& operator=(const ThreadPool&) = delete;
void schedule(Task&& task);
~ThreadPool();
private:
static void worker(QueueClass<Task>& queue);
void shutdown();
private:
std::vector<std::thread> m_workers;
QueueClass<Task> m_queue;
};
}}} // namespace cv::gapi::own
#endif // OPENCV_GAPI_THREAD_POOL_HPP
|