Initial threadpool rewrite/cleanup
This commit is contained in:
committed by
Bennett Anderson
parent
5f13a83009
commit
11ea7fcb71
+26
-55
@@ -21,75 +21,46 @@
|
||||
#ifndef THREADPOOL_H
|
||||
#define THREADPOOL_H
|
||||
|
||||
#include <QThread>
|
||||
|
||||
#include "common/cancelableobject.h"
|
||||
#include "threading/threadticket.h"
|
||||
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace olive {
|
||||
|
||||
class ThreadPoolThread;
|
||||
enum class RenderTicketPriority { kHigh = 0, kNormal };
|
||||
|
||||
class ThreadPool : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ThreadPool(QThread::Priority priority = QThread::InheritPriority, int threads = 0, QObject* parent = nullptr);
|
||||
using TaskType = RenderTicketPtr;
|
||||
ThreadPool(unsigned threads, QObject *parent);
|
||||
|
||||
ThreadPool(const ThreadPool &) = delete;
|
||||
ThreadPool(ThreadPool &&) = delete;
|
||||
ThreadPool & operator=(const ThreadPool&) = delete;
|
||||
ThreadPool & operator=(ThreadPool &&) = delete;
|
||||
|
||||
virtual void RunTicket(RenderTicketPtr ticket) const = 0;
|
||||
void AddTicket(RenderTicketPtr ticket, RenderTicketPriority priority = RenderTicketPriority::kNormal);
|
||||
bool RemoveTicket(RenderTicketPtr ticket);
|
||||
|
||||
virtual ~ThreadPool() override;
|
||||
|
||||
RenderTicketPtr Queue();
|
||||
|
||||
virtual void RunTicket(RenderTicketPtr ticket) const = 0;
|
||||
|
||||
bool RemoveTicket(RenderTicketPtr ticket);
|
||||
|
||||
public slots:
|
||||
void AddTicket(olive::RenderTicketPtr ticket, bool prioritize = false);
|
||||
|
||||
private:
|
||||
void RunNext();
|
||||
|
||||
QVector<ThreadPoolThread*> all_threads_;
|
||||
|
||||
std::list<ThreadPoolThread*> available_threads_;
|
||||
|
||||
std::list<RenderTicketPtr> ticket_queue_;
|
||||
|
||||
private slots:
|
||||
void ThreadDone();
|
||||
void thread_exec();
|
||||
|
||||
std::vector<std::thread> worker_threads_;
|
||||
std::deque<TaskType> tasks_;
|
||||
std::mutex task_mutex_;
|
||||
std::condition_variable cond_;
|
||||
std::atomic_bool end_threadp_{false};
|
||||
};
|
||||
|
||||
class ThreadPoolThread : public QThread, public CancelableObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ThreadPoolThread(ThreadPool* parent);
|
||||
} // namespace olive
|
||||
|
||||
virtual ~ThreadPoolThread() override;
|
||||
|
||||
void RunTicket(RenderTicketPtr ticket);
|
||||
|
||||
protected:
|
||||
virtual void run() override;
|
||||
|
||||
virtual void CancelEvent() override;
|
||||
|
||||
signals:
|
||||
void Done();
|
||||
|
||||
private:
|
||||
ThreadPool* pool_;
|
||||
|
||||
RenderTicketPtr ticket_;
|
||||
|
||||
QMutex mutex_;
|
||||
|
||||
QWaitCondition wait_cond_;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // THREADPOOL_H
|
||||
#endif // THREADPOOL_H
|
||||
|
||||
Reference in New Issue
Block a user