summaryrefslogtreecommitdiffstats
path: root/src/common/thread_worker.h
blob: 12bbf5fef460e16071edf967c6bd218c9228ad30 (plain) (blame)
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
// Copyright 2020 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <atomic>
#include <functional>
#include <mutex>
#include <string>
#include <vector>
#include <queue>

#include "common/common_types.h"
#include "common/unique_function.h"

namespace Common {

class ThreadWorker final {
public:
    explicit ThreadWorker(std::size_t num_workers, const std::string& name);
    ~ThreadWorker();
    void QueueWork(UniqueFunction<void> work);
    void WaitForRequests();

private:
    std::vector<std::thread> threads;
    std::queue<UniqueFunction<void>> requests;
    std::mutex queue_mutex;
    std::condition_variable condition;
    std::condition_variable wait_condition;
    std::atomic_bool stop{};
    std::atomic<u64> work_scheduled{};
    std::atomic<u64> work_done{};
    std::atomic<u64> workers_stopped{};
    std::atomic<u64> workers_queued{};
};

} // namespace Common