uvgVPCCenc 1.0.0
uvgVPCCenc is an open-source real-time V-PCC encoder library written in C++ from scratch.
Loading...
Searching...
No Matches
threadqueue.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 * This file is part of uvgVPCCenc V-PCC encoder.
3 *
4 * Copyright (c) 2024, Tampere University, ITU/ISO/IEC, project contributors
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice, this
14 * list of conditions and the following disclaimer in the documentation and/or
15 * other materials provided with the distribution.
16 *
17 * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
28 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ****************************************************************************/
32
33#pragma once
34
36
37
38#include <atomic>
39#include <cassert>
40#include <condition_variable>
41#include <deque>
42#include <functional>
43#include <memory>
44#include <mutex>
45#include <thread>
46#include <vector>
47
48namespace uvgvpcc_enc {
49
76
77class ThreadQueue;
78
79class Job : public std::enable_shared_from_this<Job> {
80 public:
81 using JobFunction = std::function<void()>;
82 // Variadic template constructor
83 template <typename Func, typename... Args>
84 Job(std::string name, size_t priority, Func&& func, Args&&... args)
85 : name_(name),
86 func_(std::bind(std::forward<Func>(func), std::forward<Args>(args)...)),
90 completed_(false) {}
91 void execute() const;
92 void addDependency(const std::shared_ptr<Job>& dependency);
93 bool isReady() const;
94 void wait();
95 void complete();
96 std::string getName() const { return name_; }
98 void setState(threadqueue_job_state state) { state_ = state; }
99
100 mutable std::mutex mtx_;
101 std::vector<std::shared_ptr<Job>> reverseDependencies_;
102 std::string name_;
105 std::atomic<int> dependencies_;
106 std::atomic<size_t> priority;
107
108 private:
109 std::condition_variable cv_;
110 std::atomic<bool> completed_;
111};
112
114 public:
115 ThreadQueue(int numThreads);
116 ~ThreadQueue();
117
118 void submitJob(const std::shared_ptr<Job>& job);
119 void pushJob(const std::shared_ptr<Job>& job);
120 void stop();
121 static void waitForJob(const std::shared_ptr<Job>& job);
122
123 private:
124 void workerThread();
125 std::mutex mtx_;
126 std::condition_variable jobAvailable_;
127 std::condition_variable jobDone_;
128 std::vector<std::thread> threads_;
129 std::array<std::deque<std::shared_ptr<Job>>, 6> jobs_;
130 std::atomic<bool> stop_;
131};
132
134
135} // namespace uvgvpcc_enc
Definition threadqueue.hpp:79
std::string name_
Definition threadqueue.hpp:102
std::atomic< size_t > priority
Definition threadqueue.hpp:106
std::function< void()> JobFunction
Definition threadqueue.hpp:81
std::string getName() const
Definition threadqueue.hpp:96
void complete()
Definition threadqueue.cpp:93
threadqueue_job_state getState() const
Definition threadqueue.hpp:97
void execute() const
Definition threadqueue.cpp:83
void setState(threadqueue_job_state state)
Definition threadqueue.hpp:98
Job(std::string name, size_t priority, Func &&func, Args &&... args)
Definition threadqueue.hpp:84
std::mutex mtx_
Definition threadqueue.hpp:100
std::vector< std::shared_ptr< Job > > reverseDependencies_
Definition threadqueue.hpp:101
void wait()
Definition threadqueue.cpp:85
bool isReady() const
Definition threadqueue.cpp:81
JobFunction func_
Definition threadqueue.hpp:103
threadqueue_job_state state_
Definition threadqueue.hpp:104
void addDependency(const std::shared_ptr< Job > &dependency)
Definition threadqueue.cpp:62
std::atomic< int > dependencies_
Definition threadqueue.hpp:105
Definition threadqueue.hpp:113
void stop()
Definition threadqueue.cpp:131
static void waitForJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:144
void submitJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:116
void pushJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:108
~ThreadQueue()
Definition threadqueue.cpp:106
Definition log.hpp:43
std::string jobStateToStr(threadqueue_job_state s)
Definition threadqueue.cpp:50
threadqueue_job_state
Definition threadqueue.hpp:50
@ THREADQUEUE_JOB_STATE_READY
Job is ready to run.
Definition threadqueue.hpp:64
@ THREADQUEUE_JOB_STATE_RUNNING
Job is running.
Definition threadqueue.hpp:69
@ THREADQUEUE_JOB_STATE_PAUSED
Job has been submitted, but is not allowed to run yet.
Definition threadqueue.hpp:54
@ THREADQUEUE_JOB_STATE_WAITING
Job is waiting for dependencies.
Definition threadqueue.hpp:59
@ THREADQUEUE_JOB_STATE_DONE
Job is completed.
Definition threadqueue.hpp:74