File size: 965 Bytes
3fd1a35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
#pragma once

#include <array>
#include <condition_variable>
#include <cstddef>
#include <exception>
#include <functional>
#include <mutex>
#include <span>
#include <thread>

namespace ling3 {

class CoreWorkers {
public:
    static CoreWorkers & Instance();

    CoreWorkers(const CoreWorkers &) = delete;
    CoreWorkers & operator=(const CoreWorkers &) = delete;

    void Run(std::span<const int> cores, const std::function<void(int)> & task);

private:
    CoreWorkers();
    ~CoreWorkers();

    void Worker(int core);

    std::array<std::thread, 4> workers_;
    std::array<bool, 4> active_ {};
    std::array<std::exception_ptr, 4> errors_ {};
    std::function<void(int)> task_;
    std::mutex run_mutex_;
    std::mutex state_mutex_;
    std::condition_variable start_;
    std::condition_variable done_;
    std::size_t generation_ = 0;
    std::size_t active_count_ = 0;
    std::size_t completed_ = 0;
    bool stop_ = false;
};

} // namespace ling3