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(const std::string& name, const 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 static void setExecutionMethod(bool useTimer) {
92 executePtr = useTimer ? &Job::executeTimer : &Job::executeNoTimer;
93 }
94 void executeTimer() const;
95 void executeNoTimer() const;
96 void execute() {(this->*executePtr)();}
97 void addDependency(const std::shared_ptr<Job>& dependency);
98 bool isReady() const;
99 void wait();
100 void complete();
101 std::string getName() const { return name_; }
103 void setState(const threadqueue_job_state& state) { state_ = state; }
104
105 mutable std::mutex mtx_;
106 std::vector<std::shared_ptr<Job>> reverseDependencies_;
107 std::string name_;
110 std::atomic<int> dependencies_;
111 std::atomic<size_t> priority;
112
113 private:
114 std::condition_variable cv_;
115 std::atomic<bool> completed_;
116 static void (Job::*executePtr)() const;
117};
118
120 public:
121 ThreadQueue(int numThreads);
122 ~ThreadQueue();
123
124 void submitJob(const std::shared_ptr<Job>& job);
125 void pushJob(const std::shared_ptr<Job>& job);
126 void stop();
127 static void waitForJob(const std::shared_ptr<Job>& job);
128
129 private:
130 void workerThread();
131 std::mutex mtx_;
132 std::condition_variable jobAvailable_;
133 std::condition_variable jobDone_;
134 std::vector<std::thread> threads_;
135 std::array<std::deque<std::shared_ptr<Job>>, 6> jobs_;
136 std::atomic<bool> stop_;
137};
138
140
141} // namespace uvgvpcc_enc
Definition threadqueue.hpp:79
void executeNoTimer() const
Definition threadqueue.cpp:84
void executeTimer() const
Definition threadqueue.cpp:88
std::string name_
Definition threadqueue.hpp:107
std::atomic< size_t > priority
Definition threadqueue.hpp:111
std::function< void()> JobFunction
Definition threadqueue.hpp:81
std::string getName() const
Definition threadqueue.hpp:101
static void setExecutionMethod(bool useTimer)
Definition threadqueue.hpp:91
void complete()
Definition threadqueue.cpp:103
threadqueue_job_state getState() const
Definition threadqueue.hpp:102
std::mutex mtx_
Definition threadqueue.hpp:105
std::vector< std::shared_ptr< Job > > reverseDependencies_
Definition threadqueue.hpp:106
void setState(const threadqueue_job_state &state)
Definition threadqueue.hpp:103
void execute()
Definition threadqueue.hpp:96
Job(const std::string &name, const size_t &priority, Func &&func, Args &&... args)
Definition threadqueue.hpp:84
void wait()
Definition threadqueue.cpp:95
bool isReady() const
Definition threadqueue.cpp:82
JobFunction func_
Definition threadqueue.hpp:108
threadqueue_job_state state_
Definition threadqueue.hpp:109
void addDependency(const std::shared_ptr< Job > &dependency)
Definition threadqueue.cpp:63
std::atomic< int > dependencies_
Definition threadqueue.hpp:110
Definition threadqueue.hpp:119
void stop()
Definition threadqueue.cpp:141
static void waitForJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:154
void submitJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:126
void pushJob(const std::shared_ptr< Job > &job)
Definition threadqueue.cpp:118
~ThreadQueue()
Definition threadqueue.cpp:116
Definition log.hpp:43
std::string jobStateToStr(threadqueue_job_state s)
Definition threadqueue.cpp:51
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