uvgVPCCenc 1.1.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-present, 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
34
35#pragma once
36
37#include <atomic>
38#include <cassert>
39#include <condition_variable>
40#include <deque>
41#include <functional>
42#include <memory>
43#include <mutex>
44#include <thread>
45#include <vector>
46
47namespace uvgvpcc_enc {
48
75
76class ThreadQueue;
77
78class Job : public std::enable_shared_from_this<Job> {
79 public:
80 using JobFunction = std::function<void()>;
81 Job(std::string name, std::size_t priority, JobFunction func)
82 : name_(name),
83 func_(func),
87 completed_(false) {}
88 // Variadic template constructor
89 template <typename Func, typename... Args>
90 Job(std::string name, std::size_t priority, Func&& func, Args&&... args)
91 : name_(name),
92 func_(std::bind(std::forward<Func>(func), std::forward<Args>(args)...)),
96 completed_(false) {}
97 void execute() const;
98 void addDependency(const std::shared_ptr<Job>& dependency);
99 bool isReady() const;
100 void wait();
101 void complete();
102 std::string getName() const { return name_; }
104 void setState(threadqueue_job_state state) { state_ = state; }
105
106 mutable std::mutex mtx_;
107 std::vector<std::shared_ptr<Job>> reverseDependencies_;
108 std::string name_;
111 std::atomic<int> dependencies_;
112 std::atomic<std::size_t> priority;
113
114 private:
115 std::condition_variable cv_;
116 std::atomic<bool> completed_;
117};
118
120 public:
121 ThreadQueue() : stop_(false) {};
122 void initThreadQueue(int numThreads);
123 ~ThreadQueue();
124
125 void submitJob(const std::shared_ptr<Job>& job);
126 void pushJob(const std::shared_ptr<Job>& job);
127 void stop();
128 static void waitForJob(const std::shared_ptr<Job>& job);
129
130 private:
131 void workerThread();
132 std::mutex mtx_;
133 std::condition_variable jobAvailable_;
134 std::condition_variable jobDone_;
135 std::vector<std::thread> threads_;
136 std::array<std::deque<std::shared_ptr<Job>>, 6> jobs_;
137 std::atomic<bool> stop_;
138};
139
141
142} // namespace uvgvpcc_enc
Definition threadqueue.hpp:78
std::string name_
Definition threadqueue.hpp:108
Job(std::string name, std::size_t priority, Func &&func, Args &&... args)
Definition threadqueue.hpp:90
std::atomic< std::size_t > priority
Definition threadqueue.hpp:112
std::function< void()> JobFunction
Definition threadqueue.hpp:80
std::string getName() const
Definition threadqueue.hpp:102
void complete()
Definition threadqueue.cpp:104
threadqueue_job_state getState() const
Definition threadqueue.hpp:103
void execute() const
Definition threadqueue.cpp:87
void setState(threadqueue_job_state state)
Definition threadqueue.hpp:104
std::mutex mtx_
Definition threadqueue.hpp:106
std::vector< std::shared_ptr< Job > > reverseDependencies_
Definition threadqueue.hpp:107
void wait()
Definition threadqueue.cpp:96
bool isReady() const
Definition threadqueue.cpp:85
JobFunction func_
Definition threadqueue.hpp:109
threadqueue_job_state state_
Definition threadqueue.hpp:110
void addDependency(const std::shared_ptr< Job > &dependency)
Definition threadqueue.cpp:62
Job(std::string name, std::size_t priority, JobFunction func)
Definition threadqueue.hpp:81
std::atomic< int > dependencies_
Definition threadqueue.hpp:111
Definition threadqueue.hpp:119
void stop()
Definition threadqueue.cpp:142
void initThreadQueue(int numThreads)
Definition threadqueue.cpp:111
static void waitForJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:155
void submitJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:127
void pushJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:119
~ThreadQueue()
Definition threadqueue.cpp:117
ThreadQueue()
Definition threadqueue.hpp:121
Definition jobManagement.cpp:168
Definition log.hpp:48
std::string jobStateToStr(threadqueue_job_state s)
Definition threadqueue.cpp:50
threadqueue_job_state
Definition threadqueue.hpp:49
@ THREADQUEUE_JOB_STATE_READY
Job is ready to run.
Definition threadqueue.hpp:63
@ THREADQUEUE_JOB_STATE_RUNNING
Job is running.
Definition threadqueue.hpp:68
@ THREADQUEUE_JOB_STATE_PAUSED
Job has been submitted, but is not allowed to run yet.
Definition threadqueue.hpp:53
@ THREADQUEUE_JOB_STATE_WAITING
Job is waiting for dependencies.
Definition threadqueue.hpp:58
@ THREADQUEUE_JOB_STATE_DONE
Job is completed.
Definition threadqueue.hpp:73